14.6.1 Individual Page Printing
As of Version 2.1, the PdfPreview object offers automatic printing functionality
via the method SendToPrinter. This method sends the image of a page contained in this PdfPreview
object to a printer.
The SendToPrinter method accepts two arguments: the local or network name of the printer
and, optionally, a parameter list adjusting the appearance of the printout.
As of Version 2.6.0.3, if the printer name is set to an empty string, the default printer name for the
current machine is used.
The print quality is determined by the resolution of the image being printed. It is therefore recommended
that the ResolutionX and ResolutionY parameters to the ToImage method be set to at least 300
or, better yet, 600.
By default, the SendToPrinter method prints the image as it is, without stretching it. If the parameter Stretch
is set to True, the image is stretched to cover the entire print area. Additionaly,
you can use the parameters ScaleX and ScaleY to scale the image up or down.
For example, the values "ScaleX = 0.5; ScaleY= 0.5" scales the image down by 50%.
The following code sample sends page 2 of c:\path\document.pdf to the printer.
The image is stretched to cover the entire print area.
| VBScript |
Set PDF = Server.CreateObject("Persits.PDF")
Set Doc = PDF.OpenDocument("c:\path\document.pdf")
Set Page = Doc.Pages(2)
Set Preview = Page.ToImage("ResolutionX=600; ResolutionY=600")
Preview.SendToPrinter "\\192.168.1.2\HP LaserJet 6P", "Stretch=true"
|
| C# |
IPdfManager objPDF = new PdfManager();
IPdfDocument objDoc = objPDF.OpenDocument( @"c:\path\document.pdf", Missing.Value );
IPdfPage objPage = objDoc.Pages[2];
IPdfPreview objPreview = objPage.ToImage("ResolutionX=600; ResolutionY=600");
objPreview.SendToPrinter( @"\\192.168.1.2\HP LaserJet 6P", "Stretch=true" );
|
You may encounter the error "Access is denied" when attempting to send the image to a network printer. To avoid
the error, you need to impersonate an interactive user account with the LogonUser method
of the PdfManager object, as follows:
| VBScript |
|
...
PDF.LogonUser "domain", "username", "password"
Preview.SendToPrinter "\\192.168.1.2\HP LaserJet 6P", "Stretch=true"
|
| C# |
|
...
objPDF.LogonUser( "domain", "username", "password", Missing.Value );
objPreview.SendToPrinter( @"\\192.168.1.2\HP LaserJet 6P", "Stretch=true" );
|
The LogonUser method was added in Version 2.1 as well. The first argument is a Windows domain
and can be an empty string. The 2nd and 3rd arguments are the username and password of the account to be impersonated.
The 4th argument is the login type and usually omitted.
14.6.2 Document Printing
As of Version 2.7, the page printing functionality described in the previous sub-section
has been expanded to support the printing of an entire document or any portion thereof,
in both simplex (one-sided) and duplex (double-sided) modes.
In AspPDF 2.7+, the PdfDocument object has been given its own SendToPrinter method
which sends the entire document (or an arbitrary set of pages)
to the printer as opposed to just an individual page.
Internally, the PdfDocument.SendToPrinter method iterates through the pages
of the document, creates PdfPreview objects for each of them and sends the page images to the printer
one by one. If your printer supports duplex printing, this method can optionally print
double-sided documents in both long-edge and short-edge binding modes.
The PdfDocument.SendToPrinter method expects the same arguments as its PdfPreview.SendToPrinter counterpart:
the printer name (local or network) and a list of parameters.
In addition to the parameters described above, this method also accepts
parameters controlling the duplex mode, as well as the page ranges to be printed.
The Duplex parameter controls duplex printing. Duplex=1
enables duplex printing in the regular long-edge-binding
mode, and Duplex=2 in the short-edge binding mode. If this parameter
is set to 0 or omitted, the regular simplex (one-sided) printing is used.
The From1/To1, From2/To2, ..., FromN/ToN
pairs of parameters specify the ranges of pages to be printed.
Page indices are 1-based. If the specified ranges overlap,
the overlapping pages will be printed multiple times.
By default, the entire document is printed.
The following code prints pages 2, 5, and 7-10 of a document
in a duplex long-edge-binding mode:
| VBScript |
|
Set Doc = Pdf.OpenDocument( "c:\path\doc.pdf" )
Doc.SendToPrinter "Brother HL-2270DW series", _
"Stretch=true; Duplex=1; From1=2;To1=2; From2=5;To2=5; From3=7;To3=10"
|
| C# |
|
PdfDocument objDoc = objPdf.OpenDocument( @"c:\path\doc.pdf" );
objDoc.SendToPrinter( "Brother HL-2270DW series",
"Stretch=true; Duplex=1; From1=2;To1=2; From2=5;To2=5; From3=7;To3=10" );
|
As of Version 2.9, the PdfDocument.SendToPrinter method also supports printer tray (or bin) selection via the Tray
parameter. The Microsoft documentation defines the following values for this parameter:
First (1), upper (1), only one (1), lower (2), middle (3), manual (4), envelope (5), envelope manual (6), auto (7),
tractor (8), small format (9), large format (10), large capacity (11), cassette (14), form source (15), last (15).
However, many printers use driver-specific tray values that start at 256 and up. The correct Tray values
for such printers should be determined by trial and error.
As of Version 3.2.0.2, the PdfDocument.SendToPrinter method also supports printing multiple copies
via the Copies parameter (1 by default.)
As of Version 3.4.0.3, the Collate parameter (False by default) is supported, which enable
collation if set to True.
For example, if you print two copies of a three-page document
and you choose not to collate them, the pages print in this order: 1, 1, 2, 2, 3, 3.
If you choose to collate, the pages print in this order: 1, 2, 3, 1, 2, 3.
Also, Version 3.4.0.3 adds two more parameters to support label printers: PaperWidth and PaperHeight.
These parameters specify the paper width and height in tenths of a millimeter.
For example, if your label printer uses 2 5/16" x 4" labels,
the PaperWidth and PaperHeight parameters should be set to 590 and 1020, respectively.
If these parameters are not used, a document may come out shrunk when printed on a label printer.