Q:
Thanks for the reply. After some tinkering I have been able to scale
the image to match what is printed out by ADOBE. The only issue I
have now is with the size the print job. A two page print job is over
10megs. Is there some way to optimize this process? Attached is a
sample of the code please let me know if you need anything else.
My code is as follows:
Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports pdftron
Imports pdftron.PDF
Imports pdftron.Common
Imports pdftron.Filters
Imports System.Drawing.Drawing2D
Namespace PDFPrintTestCS
_
’
’ The following sample illustrates how to print PDF document using
currently selected
’ default printer. PDFDraw is used to send pre-rasterized data.
’
’ If you would like to rasterize page at high resolutions (e.g. more
than 600 DPI), you
’ should use PDFRasterizer or PDFNet vector output instead of
PDFDraw.
’
Class PDFPrint
Private Shared pdfdoc As PDFDoc = Nothing
Private Shared pdfdraw As PDFDraw = Nothing
Private Shared pageitr As PageIterator = Nothing
Private Shared paperSize As PaperSize
Private Shared printableArea As RectangleF
Private Shared printerSetting As PrinterSettings
Private Shared intPages As Integer = 0
'Entry point which delegates to C-style main Private Function
Overloads Shared Sub Main(ByVal args() As String)
PDFNet.Initialize()
’ Search for PDFNet resource file (i.e. ‘pdfnet.res’).
Dim resource_path As String = “C:\PDFNet\resources”
Dim found As Boolean = False
PDFNet.SetResourcesPath(resource_path)
Dim input_path As String = “c:\Temp” ’ Relative path to the
folder containing test files.
Dim strFiles() As String = System.IO.Directory.GetFiles(“c:
\Temp”, “fw*.pdf”)
Dim strFile As String
For Each strFile In strFiles
Try
’ Open the PDF document.
Console.WriteLine(“Opening the input file…”)
Console.WriteLine(strFile)
pdfdoc = New PDFDoc(strFile)
pdfdoc.InitSecurityHandler()
’ Start printing from the first page
pageitr = pdfdoc.PageBegin()
pdfdraw = New PDFDraw()
'pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus)
pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn)
’ PDFNet includes two different rasterizer
implementations.
’
’ The two implementations offer a tradeoff between
print
’ speed and accuracy/quality, as well as a tradeoff
between
’ vector and raster output.
’
’ e_GDIPlus rasterizer can be used to render the page
’ using Windows GDI+, whereas e_BuiltIn rasterizer can
’ be used to render bitmaps using platform-
independent
’ graphics engine (in this case images are always
converted
’ to bitmap prior to printing).
’
pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn);
’ Create a printer
Dim printer As New PrintDocument()
printer.DefaultPageSettings.Margins.Left = 0
printer.DefaultPageSettings.Margins.Right = 0
printer.DefaultPageSettings.Margins.Bottom = 0
printer.DefaultPageSettings.Margins.Top = 0
printer.OriginAtMargins = True
paperSize = printer.DefaultPageSettings.PaperSize
printerSetting = printer.PrinterSettings
updatePrintableArea()
’ Set the PrintPage delegate which will be invoked to
print each page
AddHandler printer.PrintPage, AddressOf PrintPage
’ Start printing
printer.Print()
’ Close the file
pdfdoc.Close()
Catch e As PDFNetException
Console.WriteLine(e.Message)
End Try
Next
PDFNet.Terminate()
Console.WriteLine(“Complete”)
Console.ReadLine()
End Sub 'Main
’ Print event hander
Shared Sub PrintPage(ByVal sender As Object, ByVal ev As
PrintPageEventArgs)
Dim dpi As Single = gr.DpiX
If dpi > 200 Then
dpi = 200
End If
Dim example As Integer = 1
’ Example 1) Print the Bitmap.
If example = 1 Then
ev.Graphics.PageUnit = GraphicsUnit.Point
'Dim page As Page = pageitr.Current
ev.Graphics.MultiplyTransform(PrintTransformation)
pdfdraw.SetDPI(dpi)
Dim bmp As System.Drawing.Bitmap =
pdfdraw.GetBitmap(pageitr.Current())
g.DrawImage(bmp, printableArea)
End If
pageitr.Next() ’ Move to the next page, if any
If pageitr = pdfdoc.PageEnd() Then
ev.HasMorePages = False
Else
ev.HasMorePages = True
End If
End Sub 'PrintPage
Shared ReadOnly Property PrintTransformation() As Matrix
Get
Dim page As Page = pageitr.Current()
Dim transformation As Matrix = New Matrix
Dim pdfWidth As Single = CSng(page.GetPageWidth)
Dim pdfHeight As Single = CSng(page.GetPageHeight)
Dim pdfRatio As Single = pdfWidth / pdfHeight
Dim paperWidth As Single = (paperSize.Width * 72.0F) /
100.0F
Dim paperHeight As Single = (paperSize.Height * 72.0F) /
100.0F
paperWidth = printableArea.Width
paperHeight = printableArea.Height
transformation.Translate(printableArea.Left,
printableArea.Top)
Dim paperRatio As Single = paperWidth / paperHeight
Dim scaledPdfWidth As Single = pdfWidth
Dim scaledPdfHeight As Single = pdfHeight
Dim rotate As Boolean = False
If ((pdfRatio > 1) And (paperRatio < 1)) Or ((pdfRatio <
- And (paperRatio > 1)) Then
'// need to rotate
rotate = True
Dim temp As Single = scaledPdfWidth
scaledPdfWidth = scaledPdfHeight
scaledPdfHeight = temp
pdfRatio = 1 / pdfRatio
End If
Dim scale As Single = 1
If pdfRatio > paperRatio Then
scale = paperWidth / scaledPdfWidth
Else
scale = paperHeight / scaledPdfHeight
End If
scaledPdfWidth *= scale
scaledPdfHeight *= scale
Dim dx As Single
dx = (paperWidth - scaledPdfWidth) / 2
Dim dy As Single
dy = (paperHeight - scaledPdfHeight) / 2
’ align
transformation.Translate(dx, dy)
’ auto-rotate
If (rotate) Then
transformation.Rotate(-90)
transformation.Translate(-scaledPdfHeight, 0)
End If
’ scale
transformation.Scale(scale, scale)
Return transformation
End Get
End Property
Shared Sub updatePrintableArea()
Dim graphics As Graphics =
printerSetting.CreateMeasurementGraphics()
Dim paperWidth As Single = (paperSize.Width * 72.0F) / 100.0F
Dim paperHeight As Single = (paperSize.Height * 72.0F) /
100.0F
Dim printableWidth As Single =
(graphics.VisibleClipBounds.Width * 72.0F) / 100.0F
Dim printableHeight As Single =
(graphics.VisibleClipBounds.Height * 72.0F) / 100.0F
printableArea = New RectangleF(0, 0, printableWidth,
printableHeight)
Dim dx As Single = (paperWidth - printableWidth) / 2
Dim dy As Single = (paperHeight - printableHeight) / 2
printableArea.Offset(dx, dy)
End Sub
End Class 'PDFPrint
End Namespace 'PDFPrintTestCS
A:
You could try using GDI+ based rasterizer instead of ‘Built-in’
rasterizer.
Simply uncomment the line:
'pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_GDIPlus)
And comment out the following line:
pdfdraw.SetRasterizerType(PDFRasterizer.Type.e_BuiltIn)
In case you prefer to use ‘Built-in’ rasterizer, you can also lower
output DPI resolution (pdfdraw.SetDPI(dpi)) which will effectively
decrease the size of the spool file.
Also if your printer directly supports printing PDF documents you can
directly send the document to the printer.
With PDFNet SDK v.4 there will be additional options to significantly
speed-up printing.