PdfToTiffGcImaging.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GrapeCity.Documents.Drawing

Public Class PdfToTiffGcImaging
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        ' Arbitrary target DPI for the generated TIFF, adjust as needed:
        Const targetDPI As Integer = 200
        Dim ptToDpi As Func(Of Single, Integer) = Function(pts As Single) CInt(Math.Round(CDbl(pts) * targetDPI / 72.0))

        Dim inputDoc = New GcPdfDocument()
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf"))
            inputDoc.Load(fs)

        ' NOTE: to produce a TIFF disk file, you would create a GcTiffWriter
        ' with the file path as the parameter. But due to the demo browser requirements,
        ' we create a GcTiffWriter on a memory stream instead:
        ' Using tiffWriter As New GcTiffWriter("result.tiff")
        Using msTiff As New MemoryStream()
            Using tiffWriter As New GcTiffWriter(msTiff)
                For Each pIn In inputDoc.Pages
                    ' Create GcBitmap with same size as the PDF page, adjusted for the target DPI:
                    Using bmp As New GcBitmap(ptToDpi(pIn.Size.Width), ptToDpi(pIn.Size.Height), True, ptToDpi(72), ptToDpi(72))
                        ' Draw the PDF page on the bitmap and apply in-place grayscale effect:
                        Using g = bmp.CreateGraphics(Color.White)
                            pIn.Draw(g, New RectangleF(0, 0, bmp.Width, bmp.Height))
                        End Using
                        bmp.ApplyEffect(GrayscaleEffect.Get())
                        ' Convert full color bitmap to grayscale bitmap and append it as a frame to the TIFF:
                        Using gbmp = bmp.ToGrayscaleBitmap()
                            tiffWriter.AppendFrame(gbmp)
                        End Using
                    End Using
                Next
            End Using
            ' At this point tiffWriter has the newly created TIFF. If it was created on
            ' a disk file, that file would contain the TIFF when tiffWriter is disposed.
            '
            ' To use this sample in the demo browser, we have to convert the TIFF
            ' back to a PDF that is returned to the controller.
            msTiff.Position = 0
            Dim doc = New GcPdfDocument()
            Using tiffReader As New GcTiffReader(msTiff)
                Dim disposables As New List(Of IDisposable)()
                For Each tp In tiffReader.Frames
                    Dim img = tp.ToImage(ImageBinding.InMemoryData)
                    Dim pOut = doc.Pages.Add(New SizeF(img.Width / img.HorizontalResolution * 72, img.Height / img.VerticalResolution * 72))
                    pOut.Graphics.DrawImage(img, pOut.Bounds, Nothing, ImageAlign.Default)
                    disposables.Add(img)
                Next
                ' Save the PDF:
                doc.Save(stream)
                ' We can only dispose images after saving the PDF:
                disposables.ForEach(Sub(d_) d_.Dispose())
                Return doc.Pages.Count
            End Using
        End Using
        End Using
    End Function
End Class