SaveAsImage.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.Linq;
  8. using System.Drawing;
  9. using System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Svg;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos.Basics
  16. {
  17. // This sample shows how to save pages of an existing PDF as images.
  18. // It loads a PDF generated by the SlidePages sample, then saves
  19. // the whole PDF as a multi-page TIFF. It also saves each of the pages
  20. // as a separate JPEG image. Finally, the last page of the loaded PDF
  21. // is saved as SVGZ (compressed SVG).
  22. // All generated images are attached to the resulting PDF.
  23. //
  24. // Other image formats that are also supported: PNG, BMP, GIF.
  25. public class SaveAsImage
  26. {
  27. public int CreatePDF(Stream stream)
  28. {
  29. var doc = new GcPdfDocument();
  30. var page = doc.NewPage();
  31.  
  32. Common.Util.AddNote(
  33. "We load the PDF generated by the 'Slide Pages' sample, " +
  34. "and save the whole PDF as a multi-page TIFF. " +
  35. "We also save each of the pages as a separate JPEG image. " +
  36. "Finally, the last page is saved as SVGZ (compressed SVG). " +
  37. "All created images are then attached to this document.",
  38. page);
  39.  
  40. // Keep track of temp files, delete them on exit:
  41. var tfiles = new List<string>();
  42.  
  43. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf"));
  44. var docSrc = new GcPdfDocument();
  45. docSrc.Load(fs);
  46. // Save all pages of the loaded PDF as a multi-page TIFF:
  47. var tf = Path.GetTempFileName();
  48. docSrc.SaveAsTiff(tf);
  49. var fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, tf));
  50. fspec.File.FileName = "SlidePages.tiff";
  51. doc.EmbeddedFiles.Add(fspec.File.FileName, fspec);
  52. tfiles.Add(tf);
  53.  
  54. // Save each page of the loaded PDF as a JPEG:
  55. foreach (var p in docSrc.Pages)
  56. {
  57. tf = Path.GetTempFileName();
  58. p.SaveAsJpeg(tf);
  59. fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, tf));
  60. fspec.File.FileName = $"Page_{p.Index}.jpeg";
  61. doc.EmbeddedFiles.Add(fspec.File.FileName, fspec);
  62. tfiles.Add(tf);
  63. }
  64.  
  65. // Finally, save the last page of the PDF as SVGZ (compressed SVG):
  66. var bytes = docSrc.Pages.Last.ToSvgz(new SaveAsImageOptions() { BackColor = Color.Transparent });
  67. fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromBytes(doc, bytes));
  68. fspec.File.FileName = $"Page_{docSrc.Pages.Last.Index}.svgz";
  69. doc.EmbeddedFiles.Add(fspec.File.FileName, fspec);
  70.  
  71. doc.Save(stream);
  72. // Clean up:
  73. tfiles.ForEach(tf_ => File.Delete(tf_));
  74. // Done:
  75. return doc.Pages.Count;
  76. }
  77. }
  78. }
  79.