StartEndDoc.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 GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GCTEXT = GrapeCity.Documents.Text
  10. Imports GCDRAW = GrapeCity.Documents.Drawing
  11.  
  12. '' Shows how to create a large document using less memory.
  13. ''
  14. '' DsPdf provides two approaches to creating a PDF file:
  15. '' - The usually more convenient approach: you build the document completely first,
  16. '' adding text, graphics and other elements. Then you call Save() on the document
  17. '' passing the name of the file, or the stream to save to. This approach allows
  18. '' to modify the already created content - e.g. you can insert pages anywhere
  19. '' in the document, or modify the already added pages.
  20. '' - The StartDoc/EndDoc method: with this approach, you provide the stream
  21. '' to save to at the very beginning, before adding any content to the document,
  22. '' by calling the StartDoc() method on the document. All content is then written
  23. '' directly to that stream, and you cannot go back and update the already created pages.
  24. '' To complete the document you call the EndDoc() method. If you try to perform an
  25. '' action that is not allowed, an exception will be thrown. While this approach is
  26. '' somewhat limiting (e.g. Linearized cannot be set to true in this mode), it uses
  27. '' less memory and may be preferable especially when creating very large documents.
  28. ''
  29. '' This sample demonstrates the StartDoc/EndDoc approach.
  30. ''
  31. '' Essentially the same code, but without use of StartDoc/EndDoc, is demonstrated by the
  32. '' LargeDocument2 sample. See also LinearizedPdf.
  33. Public Class StartEndDoc
  34. Function CreatePDF(ByVal stream As Stream) As Integer
  35. '' Number of pages to generate:
  36. Dim N = Util.LargeDocumentIterations
  37. Dim doc = New GcPdfDocument()
  38. '' Start creating the document by this call:
  39. doc.StartDoc(stream)
  40. '' Prep a TextLayout to hold/format the text:
  41. Dim tl = New TextLayout(72) With {
  42. .MaxWidth = doc.PageSize.Width,
  43. .MaxHeight = doc.PageSize.Height,
  44. .MarginAll = 72
  45. }
  46. tl.DefaultFormat.Font = StandardFonts.Times
  47. tl.DefaultFormat.FontSize = 12
  48. '' Start with a title page:
  49. tl.FirstLineIndent = 0
  50. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"))
  51. Dim tf0 = New TextFormat() With {.FontSize = 24, .FontBold = True, .Font = fnt}
  52. tl.Append(String.Format("Large Document" + vbLf + "{0} Pages of Lorem Ipsum" + vbLf + vbLf, N), tf0)
  53. Dim tf1 = New TextFormat(tf0) With {.FontSize = 14, .FontItalic = True}
  54. tl.Append(String.Format("Generated on {0}.", Util.TimeNow().ToString("R")), tf1)
  55. tl.TextAlignment = TextAlignment.Center
  56. tl.PerformLayout(True)
  57. doc.Pages.Add().Graphics.DrawTextLayout(tl, PointF.Empty)
  58. tl.Clear()
  59. tl.FirstLineIndent = 36
  60. tl.TextAlignment = TextAlignment.Leading
  61. '' Generate the document:
  62. For pageIdx = 1 To N
  63. tl.Append(Util.LoremIpsum(1))
  64. tl.PerformLayout(True)
  65. doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty)
  66. tl.Clear()
  67. Next
  68. '' NOTE: Certain operations (e.g. the one below) will throw an error when using StartDoc/EndDoc:
  69. '' doc.Pages.Insert(0);
  70. ''
  71. '' Done - call EndDoc instead of Save():
  72. doc.EndDoc()
  73. Return doc.Pages.Count
  74. End Function
  75. End Class
  76.