PageSize.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Common;
  11. using GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // Shows how to change page size and orientation in DsPdf.
  16. public class PageSize
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. const float in2mm = 72f / 25.4f;
  21. var colorOrig = Color.Red;
  22. var colorNew = Color.Blue;
  23. var doc = new GcPdfDocument();
  24. // The default page size is Letter (8 1/2" x 11") with portrait orientation:
  25. var page = doc.NewPage();
  26. var sOrigPageInfo = $"Original page size: {page.Size} pts ({page.Size.Width / 72f}\" * {page.Size.Height / 72f}\"),\r\n" +
  27. $"paper kind: {page.PaperKind}, landscape: {page.Landscape}.";
  28. // Save original page bounds:
  29. var rOrig = page.Bounds;
  30. // Change page parameters:
  31. page.Landscape = true;
  32. page.PaperKind = PaperKind.A4;
  33. var sNewPageInfo = $"New page size: {page.Size} pts ({page.Size.Width / in2mm}mm * {page.Size.Height / in2mm}mm),\r\n" +
  34. $"paper kind: {page.PaperKind}, landscape: {page.Landscape}.";
  35. // New page bounds:
  36. var rNew = page.Bounds;
  37. // Draw original and new page bounds:
  38. page.Graphics.DrawRectangle(rOrig, colorOrig, 6);
  39. page.Graphics.DrawRectangle(rNew, colorNew, 6);
  40. // Draw original and new page infos:
  41. var tf = new TextFormat()
  42. {
  43. Font = StandardFonts.Times,
  44. FontSize = 14,
  45. ForeColor = colorOrig
  46. };
  47. page.Graphics.DrawString(sOrigPageInfo, tf, new PointF(72, 72));
  48. tf.ForeColor = colorNew;
  49. page.Graphics.DrawString(sNewPageInfo, tf, new PointF(72, 72 * 2));
  50. // Done:
  51. doc.Save(stream);
  52. return doc.Pages.Count;
  53. }
  54. }
  55. }
  56.