''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Linq
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
'' This sample shows how to save pages of an existing PDF as images.
'' It loads a PDF generated by the SlidePages sample, then saves
'' the whole PDF as a multi-page TIFF. It also saves each of the pages
'' as a separate JPEG image.
'' All generated images are attached to the resulting PDF.
''
'' Other image formats that are also supported: PNG, BMP, GIF.
Public Class SaveAsImage
Public Function CreatePDF(ByVal stream As Stream) As Integer
Dim doc = New GcPdfDocument()
Dim page = doc.NewPage()
Util.AddNote(
"We load the PDF generated by the 'Slide Pages' sample, " +
"and save the whole PDF as a multi-page TIFF. " +
"We also save each of the pages as a separate JPEG image. " +
"Finally, the last page is saved as SVGZ (compressed SVG). " +
"All created images are then attached to this document.",
page)
'' Keep track of temp files, delete them on exit:
Dim tfiles = New List(Of String)
Using fs = New FileStream(Path.Combine("Resources", "PDFs", "SlidePages.pdf"), FileMode.Open, FileAccess.Read)
Dim origDoc = New GcPdfDocument()
origDoc.Load(fs)
'' Save all pages of the loaded PDF as a multi-page TIFF:
Dim tf = Path.GetTempFileName()
origDoc.SaveAsTiff(tf)
Dim fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, tf))
fspec.File.FileName = "SlidePages.tiff"
doc.EmbeddedFiles.Add(tf, fspec)
tfiles.Add(tf)
'' Save each page of the loaded PDF as a JPEG:
For Each p In origDoc.Pages
tf = Path.GetTempFileName()
p.SaveAsJpeg(tf)
fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, tf))
fspec.File.FileName = $"Page_{p.Index}.jpeg"
doc.EmbeddedFiles.Add(tf, fspec)
tfiles.Add(tf)
Next
End Using
'' Done:
doc.Save(stream)
'' Clean up:
tfiles.ForEach(Sub(tf_) File.Delete(tf_))
Return doc.Pages.Count
End Function
End Class