LoadPDF.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.  
  11. namespace DsPdfWeb.Demos
  12. {
  13. // This sample loads the PDF file created by the HelloWorld sample,
  14. // adds a short text note to the first page, and saves the result.
  15. public class LoadPDF
  16. {
  17. public int CreatePDF(Stream stream)
  18. {
  19. // IMPORTANT: when working with an existing PDF file using GcPdfDocument.Load() method,
  20. // the stream passed to that method MUST REMAIN OPEN while working with the document.
  21. // This is because Load() does not load the whole PDF document into memory right away,
  22. // instead it loads the various parts of the PDF as needed.
  23. // The stream is only used for reading, and the original file itself is not modified.
  24. // To save the changes you need to call one of the GcPdfDocument.Save() overloads
  25. // as usual.
  26. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "HelloWorld.pdf"));
  27. var doc = new GcPdfDocument();
  28. doc.Load(fs);
  29. // Add note to the (only) page in the doc:
  30. var page = doc.Pages.Last;
  31. Common.Util.AddNote(
  32. "This text was added to the original \"Hello World\" PDF.",
  33. page,
  34. new RectangleF(72, 72 * 3, page.Size.Width - 72 * 2, 72));
  35. // Done:
  36. doc.Save(stream);
  37. return doc.Pages.Count;
  38. }
  39. }
  40. }
  41.