FlattenForm.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.Pdf.AcroForms;
  10. using GrapeCity.Documents.Pdf.Graphics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This sample shows how to easily 'flatten' an AcroForm PDF with data -
  17. // i.e. convert that form into a non-form PDF that looks like the
  18. // original filled form.
  19. // The original filled form loaded by this sample is generated by FormFields.
  20. public class FlattenForm
  21. {
  22. public int CreatePDF(Stream stream)
  23. {
  24. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "form-fields.pdf"));
  25. // Load the filled PDF form into a temp document:
  26. var srcDoc = new GcPdfDocument();
  27. srcDoc.Load(fs);
  28. // Draw all pages and annotation of the source PDF into a new PDF:
  29. var doc = new GcPdfDocument();
  30. foreach (var srcPage in srcDoc.Pages)
  31. {
  32. var page = doc.Pages.Add();
  33. var fxo = new FormXObject(doc, srcPage);
  34. page.Graphics.DrawForm(fxo, page.Bounds, null, ImageAlign.Default);
  35. // This method draws all annotations on the page including form field widgets:
  36. srcPage.DrawAnnotations(page.Graphics, page.Bounds);
  37. }
  38. // Done:
  39. doc.Save(stream);
  40. return doc.Pages.Count;
  41. }
  42. }
  43. }
  44.