Create a FormXObject from a page and draw it in another document

PDF TIFF SVG JPG C# VB
PageFormXObject.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.Text;
  12. using GrapeCity.Documents.Drawing;
  13. using GrapeCity.Documents.Pdf;
  14. using GrapeCity.Documents.Pdf.Annotations;
  15. using GrapeCity.Documents.Pdf.Graphics;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This sample shows how to create FormXObject representing a page
  20. // from a PDF loaded into a temporary GcPdfDocument, and render that
  21. // object into the current document.
  22. public class PageFormXObject
  23. {
  24. public int CreatePDF(Stream stream)
  25. {
  26. var doc = new GcPdfDocument();
  27. var page = doc.NewPage();
  28. var g = page.Graphics;
  29.  
  30. var rc = Common.Util.AddNote(
  31. "The thumbnails below are pages from an existing document. " +
  32. "We load an arbitrary PDF into a temporary GcPdfDocument, " +
  33. "then create a FormXObject representing each of its pages, " +
  34. "and render those objects as thumbnails into the current document. " +
  35. "To show that the same FormXObject can be used more than once, " +
  36. "we also render each thumbnail a second time applying a mirror transform.",
  37. page);
  38.  
  39. // Layout params:
  40. var margin = rc.Left;
  41. var pad = 36;
  42. var side = (page.Size.Width - margin * 2 - pad) / 2;
  43. var ip = new PointF(margin, rc.Bottom + pad);
  44. // Mirror transform:
  45. var tr = Matrix3x2.CreateScale(-1, 1) * Matrix3x2.CreateTranslation(page.Size.Width, 0);
  46. // Text format for the overlaid page captions:
  47. var color = Color.DarkRed;
  48. var tf = new TextFormat()
  49. {
  50. Font = StandardFonts.HelveticaBold,
  51. FontSize = 16,
  52. ForeColor = Color.FromArgb(128, color),
  53. };
  54. // Open an arbitrary PDF, load it into a temp document and loop over its pages,
  55. // drawing each into the current document:
  56. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"));
  57. var doc1 = new GcPdfDocument();
  58. doc1.Load(fs);
  59. // Create a list of FormXObject for the pages of the loaded PDF:
  60. var fxos = new List<FormXObject>();
  61. doc1.Pages.ToList().ForEach(p_ => fxos.Add(new FormXObject(doc, p_)));
  62. // Render the thumbnails into the current document:
  63. for (int i = 0; i < fxos.Count; ++i)
  64. {
  65. if (ip.Y + side > page.Size.Height - margin)
  66. {
  67. page = doc.NewPage();
  68. g = page.Graphics;
  69. ip = new PointF(margin, margin);
  70. }
  71. var rcfx = new RectangleF(ip.X, ip.Y, side, side);
  72. // Draw direct:
  73. g.DrawForm(fxos[i], rcfx, null, ImageAlign.ScaleImage);
  74. g.DrawRectangle(rcfx, color);
  75. g.DrawString($"Page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, false);
  76. // Draw reversed:
  77. g.Transform = tr;
  78. g.DrawForm(fxos[i], rcfx, null, ImageAlign.ScaleImage);
  79. g.DrawRectangle(rcfx, color);
  80. g.Transform = Matrix3x2.Identity;
  81. rcfx.Offset(side + pad, 0);
  82. g.DrawString($"Reversed page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, false);
  83. //
  84. ip.Y += side + pad;
  85. }
  86. // Done:
  87. doc.Save(stream);
  88. return doc.Pages.Count;
  89. }
  90. }
  91. }
  92.