PageFormXObject.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Numerics
  8. Imports System.Collections.Generic
  9. Imports System.Linq
  10. Imports GrapeCity.Documents.Text
  11. Imports GrapeCity.Documents.Drawing
  12. Imports GrapeCity.Documents.Pdf
  13. Imports GrapeCity.Documents.Pdf.Annotations
  14. Imports GrapeCity.Documents.Pdf.Graphics
  15.  
  16. '' This sample shows how to create FormXObject representing a page
  17. '' from a PDF loaded into a temporary GcPdfDocument, and render that
  18. '' object into the current document.
  19. Public Class PageFormXObject
  20. Function CreatePDF(ByVal stream As Stream) As Integer
  21. Dim doc = New GcPdfDocument()
  22. Dim page = doc.NewPage()
  23. Dim g = page.Graphics
  24.  
  25. Dim rc = Util.AddNote(
  26. "The thumbnails below are pages from an existing document. " +
  27. "We load an arbitrary PDF into a temporary GcPdfDocument, " +
  28. "then create a FormXObject representing each of its pages, " +
  29. "and render those objects as thumbnails into the current document. " +
  30. "To show that the same FormXObject can be used more than once, " +
  31. "we also render each thumbnail a second time applying a mirror transform.",
  32. page)
  33.  
  34. '' Layout params:
  35. Dim margin = rc.Left
  36. Dim pad = 36
  37. Dim side = (page.Size.Width - margin * 2 - pad) / 2
  38. Dim ip = New PointF(margin, rc.Bottom + pad)
  39. '' Mirror transform:
  40. Dim tr = Matrix3x2.CreateScale(-1, 1) * Matrix3x2.CreateTranslation(page.Size.Width, 0)
  41. '' Text format for the overlaid page captions:
  42. Dim clr = Color.DarkRed
  43. Dim tf = New TextFormat() With
  44. {
  45. .Font = StandardFonts.HelveticaBold,
  46. .FontSize = 16,
  47. .ForeColor = Color.FromArgb(128, clr)
  48. }
  49. '' Open an arbitrary PDF, load it into a temp document and loop over its pages,
  50. '' drawing each into the current document:
  51. Using fs = New FileStream(Path.Combine("Resources", "PDFs", "Wetlands.pdf"), FileMode.Open, FileAccess.Read)
  52. Dim doc1 = New GcPdfDocument()
  53. doc1.Load(fs)
  54. '' Create a list of FormXObject for the pages of the loaded PDF:
  55. Dim fxos = New List(Of FormXObject)()
  56. doc1.Pages.ToList().ForEach(Sub(p_) fxos.Add(New FormXObject(doc, p_)))
  57. '' Render the thumbnails into the current document:
  58. For i = 0 To fxos.Count - 1
  59. If (ip.Y + side > page.Size.Height - margin) Then
  60. page = doc.NewPage()
  61. g = page.Graphics
  62. ip = New PointF(margin, margin)
  63. End If
  64. Dim rcfx = New RectangleF(ip.X, ip.Y, side, side)
  65. '' Draw direct:
  66. g.DrawForm(fxos(i), rcfx, Nothing, ImageAlign.ScaleImage)
  67. g.DrawRectangle(rcfx, clr)
  68. g.DrawString($"Page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, False)
  69. '' Draw reversed:
  70. g.Transform = tr
  71. g.DrawForm(fxos(i), rcfx, Nothing, ImageAlign.ScaleImage)
  72. g.DrawRectangle(rcfx, clr)
  73. g.Transform = Matrix3x2.Identity
  74. rcfx.Offset(side + pad, 0)
  75. g.DrawString($"Reversed page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, False)
  76. ''
  77. ip.Y += side + pad
  78. Next
  79. End Using
  80. '' Done:
  81. doc.Save(stream)
  82. Return doc.Pages.Count
  83. End Function
  84. End Class
  85.