SplitA3toA4x2.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 System.Numerics;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using GrapeCity.Documents.Pdf;
  12. using GrapeCity.Documents.Pdf.Annotations;
  13. using GrapeCity.Documents.Pdf.Graphics;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This sample shows how to convert a PDF with A3 landscape oriented pages to a PDF
  20. // in which each source PDF's A3 landscape page is split into two A4 portrait pages.
  21. //
  22. // For each of the source PDF's A3 landscape pages we create a FormXObject representing it,
  23. // add two A4 portrait pages to the target document, and then render the FormXObject
  24. // onto each of the A4 pages' graphics with left and right alignment and no scaling.
  25. // Effectively, this renders the left half of the source A3 page on the first A4 page,
  26. // and the right half of it on the second A4 page.
  27. public class SplitA3toA4x2
  28. {
  29. public int CreatePDF(Stream stream)
  30. {
  31. // A3 and A4 page sizes:
  32. // A3 is 11.69" � 16.54":
  33. var sizeA3 = new SizeF(11.69f * 72, 16.54f * 72);
  34. // A4 is 8.27" x 11.60":
  35. var sizeA4 = new SizeF(8.27f * 72, 11.60f * 72);
  36.  
  37. // Create a sample source PDF with A3 landscape oriented pages:
  38. var docA3 = new GcPdfDocument()
  39. {
  40. PageSize = sizeA3,
  41. Landscape = true,
  42. };
  43. // First page is a horizontal image filling the whole page:
  44. var p0 = docA3.Pages.Add();
  45. var imgA3 = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "aurora.jpg"));
  46. p0.Graphics.DrawImage(imgA3, new RectangleF(PointF.Empty, docA3.PageSize), null, GCDRAW.ImageAlign.StretchImage);
  47.  
  48. // Add a few more A3 pages as two columns of random 'lorem ipsum' text:
  49. var p1 = docA3.Pages.Add();
  50. var g = p1.Graphics;
  51. var tl = g.CreateTextLayout();
  52. tl.DefaultFormat.Font = StandardFonts.Times;
  53. tl.DefaultFormat.FontSize = 12;
  54. tl.TextAlignment = GCTEXT.TextAlignment.Justified;
  55. tl.FirstLineIndent = 72 / 2;
  56. tl.ParagraphSpacing = 72 / 8;
  57. // Add random text:
  58. tl.Append(Common.Util.LoremIpsum(37));
  59. // Specify 1/4" margins all around:
  60. const float margin = 72 / 4;
  61. tl.MarginAll = margin;
  62. var colWidth = p1.Size.Width / 2;
  63. tl.MaxWidth = colWidth;
  64. tl.MaxHeight = p1.Size.Height;
  65. tl.PerformLayout(true);
  66. // In a loop, split and render the text in a 2 column layout, adding pages as needed:
  67. int col = 0;
  68. while (true)
  69. {
  70. // 'rest' will accept the text that did not fit:
  71. var splitResult = tl.Split(null, out GCTEXT.TextLayout rest);
  72. g.DrawTextLayout(tl, new PointF(col * colWidth, 0));
  73. if (splitResult != GCTEXT.SplitResult.Split)
  74. break;
  75. tl = rest;
  76. if (++col == 2)
  77. {
  78. docA3.Pages.Add();
  79. g = docA3.Pages.Last.Graphics;
  80. col = 0;
  81. }
  82. }
  83. // At this point, 'docA3' is a PDF with landscape oriented A3 pages,
  84. // the first page contains an image filling the whole page,
  85. // other pages contain random text in two vertical column layout.
  86. //
  87. // The document may be saved for reference if needed:
  88. // docA3.Save("docA3.pdf");
  89.  
  90. // Create the resulting (target) PDF with A4 page size:
  91. var doc = new GcPdfDocument()
  92. {
  93. PageSize = sizeA4,
  94. Landscape = false,
  95. };
  96.  
  97. // FormXObjects are rendered on graphics like images, but without losing fidelity.
  98. // We create two image alignments for the left and the right halves of the source A3 page:
  99. var iaLeft = new GCDRAW.ImageAlign(GCDRAW.ImageAlignHorz.Left, GCDRAW.ImageAlignVert.Top, false, false, true, false, false);
  100. var iaRight = new GCDRAW.ImageAlign(GCDRAW.ImageAlignHorz.Right, GCDRAW.ImageAlignVert.Top, false, false, true, false, false);
  101. foreach (var pA3 in docA3.Pages)
  102. {
  103. // Create FormXObject representing the source page:
  104. var fxoA3 = new FormXObject(doc, pA3);
  105. // Draw its left and right halves on separate pages of the target PDF:
  106. var pLeft = doc.Pages.Add();
  107. pLeft.Graphics.DrawForm(fxoA3, pLeft.Bounds, pLeft.Bounds, iaLeft);
  108. var pRight = doc.Pages.Add();
  109. pRight.Graphics.DrawForm(fxoA3, pLeft.Bounds, pLeft.Bounds, iaRight);
  110. }
  111.  
  112. // Done:
  113. doc.Save(stream);
  114. return doc.Pages.Count;
  115. }
  116. }
  117. }
  118.