SaveAsImage.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.Linq
  7. Imports System.Collections.Generic
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Drawing
  11.  
  12. '' This sample shows how to save pages of an existing PDF as images.
  13. '' It loads a PDF generated by the SlidePages sample, then saves
  14. '' the whole PDF as a multi-page TIFF. It also saves each of the pages
  15. '' as a separate JPEG image.
  16. '' All generated images are attached to the resulting PDF.
  17. ''
  18. '' Other image formats that are also supported: PNG, BMP, GIF.
  19. Public Class SaveAsImage
  20. Public Function CreatePDF(ByVal stream As Stream) As Integer
  21. Dim doc = New GcPdfDocument()
  22. Dim page = doc.NewPage()
  23.  
  24. Util.AddNote(
  25. "We load the PDF generated by the 'Slide Pages' sample, " +
  26. "and save the whole PDF as a multi-page TIFF. " +
  27. "We also save each of the pages as a separate JPEG image. " +
  28. "Finally, the last page is saved as SVGZ (compressed SVG). " +
  29. "All created images are then attached to this document.",
  30. page)
  31.  
  32. '' Keep track of temp files, delete them on exit:
  33. Dim tfiles = New List(Of String)
  34.  
  35. Using fs = New FileStream(Path.Combine("Resources", "PDFs", "SlidePages.pdf"), FileMode.Open, FileAccess.Read)
  36. Dim origDoc = New GcPdfDocument()
  37. origDoc.Load(fs)
  38. '' Save all pages of the loaded PDF as a multi-page TIFF:
  39. Dim tf = Path.GetTempFileName()
  40. origDoc.SaveAsTiff(tf)
  41. Dim fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, tf))
  42. fspec.File.FileName = "SlidePages.tiff"
  43. doc.EmbeddedFiles.Add(tf, fspec)
  44. tfiles.Add(tf)
  45.  
  46. '' Save each page of the loaded PDF as a JPEG:
  47. For Each p In origDoc.Pages
  48. tf = Path.GetTempFileName()
  49. p.SaveAsJpeg(tf)
  50. fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, tf))
  51. fspec.File.FileName = $"Page_{p.Index}.jpeg"
  52. doc.EmbeddedFiles.Add(tf, fspec)
  53. tfiles.Add(tf)
  54. Next
  55.  
  56. End Using
  57. '' Done:
  58. doc.Save(stream)
  59. '' Clean up:
  60. tfiles.ForEach(Sub(tf_) File.Delete(tf_))
  61. Return doc.Pages.Count
  62. End Function
  63. End Class
  64.