Code samples for PDF Printer
With PDF Printer, you can send multiple documents in a single job, send multiple jobs, and even customize the print settings used. Here you'll find some examples of how to integrate the code in your development.
Printing
Print multiple documents in one print job
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Open printer
5if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
6{
7 _tprintf(_T("Printer %s could not be opned. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
8 iReturnValue = 1;
9 goto cleanup;
10}
11
12// Begin print job
13if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
14{
15 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Loop over all input documents
21for (int i = 0; i < nInputPaths; i++)
22{
23 // Open input file
24 if (!PDFPrnOpen(pPrinter, szInputPaths[i], _T("")))
25 {
26 _tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPaths[i], PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
27 iReturnValue = 1;
28 goto cleanup;
29 }
30
31 // Loop over all pages of selected file
32 for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
33 {
34 // Print pages of input file
35 if (!PDFPrnPrintPage(pPrinter, iPage))
36 {
37 _tprintf(_T("Page %d could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
38 iReturnValue = 1;
39 goto cleanup;
40 }
41 }
42
43 // Close input file
44 PDFPrnClose(pPrinter);
45}
46
47// End print job
48if (!PDFPrnEndDocument(pPrinter))
49{
50 _tprintf(_T("The print job could not be completed or the connection could not be closed. %s (ErrorCode: 0x%08x)."), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
51 iReturnValue = 1;
52 goto cleanup;
53}
54
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Open printer
5 if (!printer.OpenPrinter(printerName))
6 throw new Exception(String.Format("Printer {0} could not be opened. " +
7 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
8
9 // Begin print job
10 if (!printer.BeginDocument("My print job."))
11 throw new Exception(String.Format("Could not connect to the printer device. " +
12 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
13
14 // Loop over all input documents
15 foreach (string path in inputPaths)
16 {
17 // Open input file
18 if (!printer.Open(path, ""))
19 throw new Exception(String.Format("Input file {0} could not be opened. {1} " +
20 "(ErrorCode: 0x{2:x}).", path, printer.ErrorMessage, printer.ErrorCode));
21
22 // Loop over all pages of selected file
23 for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
24 {
25 // Print pages of input file
26 if (!printer.PrintPage(pageNo))
27 throw new Exception(String.Format("Page {0} could not be printed successfully on " +
28 "printer {1}. {2} (ErrorCode: 0x{3:x}).", pageNo, printerName,
29 printer.ErrorMessage, printer.ErrorCode));
30 }
31
32 // Close input file
33 printer.Close();
34 }
35
36 // End print job
37 if (!printer.EndDocument())
38 throw new Exception(String.Format("The print job could not be completed or the " +
39 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
40 printer.ErrorCode));
41}
42
1// Create the printer
2printer = new Printer();
3
4// Open printer
5if (!printer.openPrinter(printerName))
6 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
7 printerName, printer.getErrorMessage(), printer.getErrorCode()));
8
9// Begin print job
10if (!printer.beginDocument("My print job."))
11 throw new Exception(String.format("Could not connect to the printer device. " +
12 "%s (ErrorCode: 0x%08x).",printer.getErrorMessage(), printer.getErrorCode()));
13
14// Loop over all input documents
15for (String path : inputPaths)
16{
17 // Open input file
18 if (!printer.open(path, ""))
19 throw new IOException(String.format("Input file %s could not be opened. " +
20 "%s (ErrorCode: 0x%08x).", path, printer.getErrorMessage(),
21 printer.getErrorCode()));
22
23 // Loop over all pages of selected file
24 for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
25 {
26 //Print pages of input file
27 if (!printer.printPage(pageNo))
28 throw new Exception(String.format("Page %d could not be printed successfully " +
29 "on printer %s. %s (ErrorCode: 0x%08x).", pageNo, printerName,
30 printer.getErrorMessage(), printer.getErrorCode()));
31 }
32
33 // Close input file
34 printer.close();
35}
36
37// End print job
38if (!printer.endDocument())
39 throw new IOException(String.format("The print job could not be completed or the connection " +
40 "could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
41 printer.getErrorCode()));
42
Print multiple jobs on same printer
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Open printer
5if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
6{
7 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
8 iReturnValue = 1;
9 goto cleanup;
10}
11
12// Start chain of linked print jobs
13if (!PDFPrnBeginGroup(pPrinter))
14{
15 _tprintf(_T("Starting chain of linked print jobs failed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Print first job
21printDocument(pPrinter, szFirstInputPath, szPrinterName);
22
23// Print second job
24printDocument(pPrinter, szSecondInputPath, szPrinterName);
25
26// Define end of the chain of linked print jobs
27if (!PDFPrnEndGroup(pPrinter))
28{
29 _tprintf(_T("End of print job chain was not set successfully. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
30 iReturnValue = 1;
31 goto cleanup;
32}
33
34// Close printer
35PDFPrnClosePrinter(pPrinter);
36
1void printDocument(TPdfPrinter* pPrinter, TCHAR* szInputPath, TCHAR* szPrinterName)
2{
3 // Begin print job
4 if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
5 {
6 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
7 iReturnValue = 1;
8 return;
9 }
10
11 // Open input file
12 if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
13 {
14 _tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
15 iReturnValue = 1;
16 goto cleanup;
17 }
18
19 // Loop over all pages of the input file
20 for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
21 {
22 // Print pages of the input file
23 if (!PDFPrnPrintPage(pPrinter, iPage))
24 {
25 _tprintf(_T("Page %d of input file %s could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
26 iReturnValue = 1;
27 goto cleanup;
28 }
29 }
30
31 // Close input file
32 PDFPrnClose(pPrinter);
33
34 // End print job
35 if (!PDFPrnEndDocument(pPrinter))
36 {
37 _tprintf(_T("The print job could not be completed or the connection could not be closed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
38 iReturnValue = 1;
39 }
40
41 return;
42
43cleanup:
44 PDFPrnEndDocument(pPrinter);
45}
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Open printer
5 if (!printer.OpenPrinter(printerName))
6 throw new Exception(String.Format("Printer {0} could not be opened. " +
7 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
8
9 // Start chain of linked print jobs.
10 if (!printer.BeginGroup())
11 throw new Exception(String.Format("Starting chain of linked print jobs failed. " +
12 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
13
14 // Print first print job
15 PrintDocument(printer, firstInputPath, printerName);
16
17 // Print second print job
18 PrintDocument(printer, secondInputPath, printerName);
19
20 // Define end of the chain of linked print jobs
21 if (!printer.EndGroup())
22 throw new Exception(String.Format("End of print jobs chain was not set successfully. " +
23 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
24
25 // Close printer
26 printer.ClosePrinter();
27}
1private static void PrintDocument(Printer printer, string inputPath, string printerName)
2{
3 // Begin print job
4 if (!printer.BeginDocument("My print job."))
5 throw new Exception(String.Format("Could not connect to the printer device. " +
6 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
7
8 // Open input file
9 if (!printer.Open(inputPath, ""))
10 throw new Exception(String.Format("Input file {0} could not be opened. " +
11 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
12
13 // Loop over all pages of the input file
14 for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
15 {
16 // Print pages of the input file
17 if (!printer.PrintPage(pageNo))
18 throw new Exception(String.Format("Page {0} of input file {1} could not be printed " +
19 "successfully on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath,
20 printerName, printer.ErrorMessage, printer.ErrorCode));
21 }
22
23 // Close input file
24 printer.Close();
25
26 // End print job
27 if (!printer.EndDocument())
28 throw new Exception(String.Format("The print job could not be completed or the " +
29 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
30 printer.ErrorCode));
31}
1// Create the printer
2printer = new Printer();
3
4// Open printer
5if (!printer.openPrinter(printerName))
6 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
7 printerName, printer.getErrorMessage(), printer.getErrorCode()));
8
9// Start chain of linked print jobs
10if (!printer.beginGroup())
11 throw new IOException(String.format("Starting chain of linked print jobs failed. " +
12 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
13
14// Print first job
15printDocument(printer, firstInputPath, printerName);
16
17// Print second job
18printDocument(printer, secondInputPath, printerName);
19
20// Define end of the chain of linked print jobs
21if (!printer.endGroup())
22 throw new IOException(String.format("End of print jobs chain was not set successfully. " +
23 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
24
25// Close printer
26printer.closePrinter();
1private static void printDocument(Printer printer, String inputPath, String printerName)
2throws IOException
3{
4 // Begin print job
5 if (!printer.beginDocument("My print job."))
6 throw new IOException(String.format("Could not connect to the printer device. " +
7 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
8
9 // Open input file
10 if (!printer.open(inputPath, ""))
11 throw new IOException(String.format("Input file %s could not be opened. " +
12 "%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(),
13 printer.getErrorCode()));
14
15 // Loop over all pages of the input file
16 for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
17 {
18 // Print pages of the input file
19 if (!printer.printPage(pageNo))
20 throw new IOException(String.format("Page %d of %s could not be printed successfully" +
21 " on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
22 printer.getErrorMessage(), printer.getErrorCode()));
23 }
24
25 // Close input file
26 printer.close();
27
28 // End print job
29 if (!printer.endDocument())
30 throw new IOException(String.format("The print job could not be completed or the " +
31 "connection could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
32 printer.getErrorCode()));
33}
Print same document on multiple printers
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Open input file
5if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
6{
7 _tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
8 iReturnValue = 1;
9}
10
11// Open first printer
12if (!PDFPrnOpenPrinter(pPrinter, szFirstPrinterName))
13{
14 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szFirstPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
15 iReturnValue = 1;
16 goto cleanup;
17}
18
19// Begin first print job
20if (!PDFPrnBeginDocument(pPrinter, _T("My first print job.\n")))
21{
22 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
23 iReturnValue = 1;
24 goto cleanup;
25}
26
27// Print desired page of the input file
28if (!PDFPrnPrintPage(pPrinter, iPageNumber))
29{
30 _tprintf(_T("Page %d of %s could not be printed succesfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPageNumber, szInputPath, szFirstPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
31 iReturnValue = 1;
32 goto cleanup;
33}
34
35// End first print job
36if (!PDFPrnEndDocument(pPrinter))
37{
38 _tprintf(_T("The print job could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), szFirstPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
39 iReturnValue = 1;
40 goto cleanup;
41}
42
43// Close first printer
44PDFPrnClosePrinter(pPrinter);
45
46
47// Open second printer
48if (!PDFPrnOpenPrinter(pPrinter, szSecondPrinterName))
49{
50 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szSecondPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
51 iReturnValue = 1;
52 goto cleanup;
53}
54
55// Begin second print job
56if (!PDFPrnBeginDocument(pPrinter, _T("My second print job.")))
57{
58 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
59 iReturnValue = 1;
60 goto cleanup;
61}
62
63// Loop over second (if exist) to last page
64for (int iPage = 2; iPage < PDFPrnGetPageCount(pPrinter); iPage++)
65{
66 // Print remaining pages of input file
67 if (!PDFPrnPrintPage(pPrinter, iPage))
68 {
69 _tprintf(_T("Page %d could not be printed successfull on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szSecondPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
70 iReturnValue = 1;
71 goto cleanup;
72 }
73}
74
75// End second print job
76if (!PDFPrnEndDocument(pPrinter))
77{
78 _tprintf(_T("The print job could not be completed or the connection could not be closed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
79 iReturnValue = 1;
80 goto cleanup;
81}
82
83// Close second printer
84PDFPrnClosePrinter(pPrinter);
85
86// Close input file
87PDFPrnClose(pPrinter);
88
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Open input file
5 if (!printer.Open(inputPath, ""))
6 throw new Exception(String.Format("Input file {0} could not be opened. " +
7 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
8
9 // Open first printer
10 if (!printer.OpenPrinter(firstPrinterName))
11 throw new Exception(String.Format("Printer {0} could not be opened. {1} " +
12 "(ErrorCode: 0x{2:x}).", firstPrinterName, printer.ErrorMessage, printer.ErrorCode));
13
14 // Begin first print job
15 if (!printer.BeginDocument("My first print job."))
16 throw new Exception(String.Format("Could not connect to the printer device. " +
17 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
18
19 // Print desired page of the input file
20 if (!printer.PrintPage(pageNumber))
21 throw new Exception(String.Format("Page {0} of {1} could not be printed successfully on " +
22 "printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNumber, inputPath, firstPrinterName,
23 printer.ErrorMessage, printer.ErrorCode));
24
25 // End first print job
26 if (!printer.EndDocument())
27 throw new Exception(String.Format("The print job could not be completed or the " +
28 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
29 printer.ErrorCode));
30
31 // Close first printer
32 printer.ClosePrinter();
33
34 // Open second printer
35 if (!printer.OpenPrinter(secondPrinterName))
36 throw new Exception(String.Format("Printer {0} could not be opened. {1} " +
37 "(ErrorCode: 0x{2:x}).", secondPrinterName, printer.ErrorMessage, printer.ErrorCode));
38
39 // Begin second print job
40 if (!printer.BeginDocument("My second print job."))
41 throw new Exception(String.Format("Could not connect to the printer device. " +
42 "{0} (ErrorCode: 0x{2:x}).", printer.ErrorMessage, printer.ErrorCode));
43
44 // Loop over second (if exist) to last page.
45 for (int pageNo = 2; pageNo < printer.PageCount; pageNo++)
46 {
47 // Print remaining pages of input file
48 if (!printer.PrintPage(pageNo))
49 throw new Exception(String.Format("Page {0} could not be printed successfully " +
50 "on printer {1}. {2} (ErrorCode: 0x{3:x}).", pageNo, secondPrinterName,
51 printer.ErrorMessage, printer.ErrorCode));
52 }
53
54 // End second print job
55 if (!printer.EndDocument())
56 throw new Exception(String.Format("The print job could not be completed or the " +
57 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
58 printer.ErrorCode));
59
60 // Close second printer
61 printer.ClosePrinter();
62
63 // Close input file
64 printer.Close();
65}
66
1// Create the printer
2printer = new Printer();
3
4// Open input file
5if (!printer.open(inputPath, ""))
6 throw new IOException(String.format("Input file %s could not be opened. " +
7 "%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
8
9// Open first printer
10if (!printer.openPrinter(firstPrinterName))
11 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
12 firstPrinterName, printer.getErrorMessage(), printer.getErrorCode()));
13
14// Begin first print job
15if (!printer.beginDocument("My first print job."))
16 throw new IOException(String.format("Could not connect to the printer device. " +
17 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
18
19// Print desired page of the input file
20if (!printer.printPage(pageNumber))
21 throw new IOException(String.format("Page %d of %s could not be printed successfully " +
22 "on printer %s. %s (ErrorCode: 0x%08x).", pageNumber, inputPath, firstPrinterName,
23 printer.getErrorMessage(), printer.getErrorCode()));
24
25// End first print job
26if (!printer.endDocument())
27 throw new IOException(String.format("The print job could not be completed or the connection " +
28 "could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
29 printer.getErrorCode()));
30
31// Close first printer
32printer.closePrinter();
33
34// Open second printer
35if (!printer.openPrinter(secondPrinterName))
36 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
37 secondPrinterName, printer.getErrorMessage(), printer.getErrorCode()));
38
39// Begin second print job
40if (!printer.beginDocument("My second print job."))
41 throw new IOException(String.format("Could not connect to the printer device. " +
42 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
43
44// Loop over second (if exist) to last page
45for (int pageNo = 2; pageNo < printer.getPageCount(); pageNo++)
46{
47 // Print remaining pages of input file
48 if (!printer.printPage(pageNo))
49 throw new IOException(String.format("Page %d could not be printed successfully on " +
50 "printer %s. %s (ErrorCode: 0x%08x).", pageNo, secondPrinterName,
51 printer.getErrorMessage(), printer.getErrorCode()));
52}
53
54// End second print job
55if (!printer.endDocument())
56 throw new IOException(String.format("The print job could not be completed or the connection " +
57 "could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
58 printer.getErrorCode()));
59
60// Close second printer
61printer.closePrinter();
62
63// Close input file
64printer.close();
65
Print a PDF document
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Print input file to one specified printer
5if (!PDFPrnPrintFile(pPrinter, szInputPath, szPrinterName, _T(""), iFirstPage, iLastPage))
6{
7 _tprintf(_T("Printing input file %s failed. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
8 iReturnValue = 1;
9}
10
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Print input file to one specified printer
5 if (!printer.PrintFile(inputPath, printerName, "", firstPage, lastPage))
6 throw new Exception(String.Format("Printing input file {0} failed. " +
7 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
8}
9
1// Create the printer
2printer = new Printer();
3
4// Print input file to one specified printer
5if (!printer.printFile(inputPath, printerName, "", firstPage, lastPage))
6 throw new IOException(String.format("Printing input file %s failed. %s (ErrorCode: 0x%08x).",
7 inputPath, printer.getErrorMessage(), printer.getErrorCode()));
8
Print to file
1pPrinter = PDFPrnCreateObject();
2
3// Open printer
4if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
5{
6 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
7 iReturnValue = 1;
8 goto cleanup;
9}
10
11// Set name of file to print to
12PDFPrnSetOutput(pPrinter, szOutputPath);
13
14// Begin print job
15if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
16{
17 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
18 iReturnValue = 1;
19 goto cleanup;
20}
21
22// Open input file
23if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
24{
25 _tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
26 iReturnValue = 1;
27 goto cleanup;
28}
29
30// Loop over all pages of selected file
31for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
32{
33 // Print pages of input file
34 if (!PDFPrnPrintPage(pPrinter, iPage))
35 {
36 _tprintf(_T("Page %d could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
37 iReturnValue = 1;
38 goto cleanup;
39 }
40}
41
42// Close input file
43PDFPrnClose(pPrinter);
44
45// End print job
46if (!PDFPrnEndDocument(pPrinter))
47{
48 _tprintf(_T("The print job could not be completed or the connection could not be closed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
49 iReturnValue = 1;
50 goto cleanup;
51}
52
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Open printer
5 if (!printer.OpenPrinter(printerName))
6 throw new Exception(String.Format("Printer {0} could not be opened. " +
7 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
8
9 // Set name of file to print to
10 printer.Output = outputPath;
11
12 // Begin print job
13 if (!printer.BeginDocument("My print job."))
14 throw new Exception(String.Format("Could not connect to the printer device. " +
15 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
16
17 // Open input file
18 if (!printer.Open(inputPath, ""))
19 throw new Exception(String.Format("Input file {0} could not be opened. " +
20 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
21
22 // Loop over all pages of selected file
23 for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
24 {
25 // Print pages of input file
26 if (!printer.PrintPage(pageNo))
27 throw new Exception(String.Format("Page {0} could not be printed successfully on " +
28 "printer {1}. {2} (ErrorCode: 0x{3:x}).", pageNo, printerName, printer.ErrorMessage,
29 printer.ErrorCode));
30 }
31
32 // Close input file
33 printer.Close();
34
35 // End print job
36 if (!printer.EndDocument())
37 throw new Exception(String.Format("The print job could not be completed or the " +
38 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
39 printer.ErrorCode));
40}
41
1// Create the printer
2printer = new Printer();
3
4// Open printer
5if (!printer.openPrinter(printerName))
6 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
7 printerName, printer.getErrorMessage(), printer.getErrorCode()));
8
9// Set name of file to print to
10printer.setOutput(outputPath);
11
12// Begin print job
13if (!printer.beginDocument("My print job."))
14 throw new IOException(String.format("Could not connect to the printer device. " +
15 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
16
17// Open input file
18if (!printer.open(inputPath, ""))
19 throw new IOException(String.format("Input file %s could not be opened. " +
20 "%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
21
22// Loop over all pages of selected file
23for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
24{
25 if (!printer.printPage(pageNo))
26 throw new IOException(String.format("Page %d of %s could not be printed successfully " +
27 "on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
28 printer.getErrorMessage(), printer.getErrorCode()));
29}
30
31// Close input file
32printer.close();
33
34// End print job
35if (!printer.endDocument())
36 throw new IOException(String.format("The print job could not be completed or the connection " +
37 "could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
38 printer.getErrorCode()));
39
Features and Configuration
List printers and their properties
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Create output text file containing a list of all available printers in localhost
5pTxtFile = _tfopen(szTextFilePath, _T("w"));
6_ftprintf(pTxtFile, _T("List of available printers in localhost:\n"));
7_ftprintf(pTxtFile, _T("----------------------------------------\n"));
8
9// Loop over all printers in localhost
10for (int iPrinter = 0, nPrinters = PDFPrnGetPrinterCount(pPrinter, ""); iPrinter < nPrinters; iPrinter++)
11{
12 TCHAR szPrinterName[256];
13 _tcscpy(szPrinterName, PDFPrnGetPrinter(pPrinter, iPrinter));
14 _ftprintf(pTxtFile, _T("Printer %d: %s\n"), iPrinter + 1, szPrinterName);
15
16 // Open printer
17 if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
18 {
19 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
20 continue;
21 }
22
23 // List duplex modes for selected printer
24 _ftprintf(pTxtFile, _T(" Duplex Modes:\n"));
25 for (int j = 0, n = PDFPrnGetDuplexModeCount(pPrinter, szPrinterName); j < n; j++)
26 _ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetDuplexMode(pPrinter, j));
27
28 // List bins for selected printer
29 _ftprintf(pTxtFile, _T(" Bins:\n"));
30 for (int j = 0, n = PDFPrnGetBinCount(pPrinter, szPrinterName); j < n; j++)
31 _ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetBin(pPrinter, j));
32
33 // List paper sizes for selected printer
34 _ftprintf(pTxtFile, _T(" Paper:\n"));
35 for (int j = 0, n = PDFPrnGetPaperCount(pPrinter, szPrinterName); j < n; j++)
36 _ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetPaper(pPrinter, j));
37
38 // List media types for selected printer
39 _ftprintf(pTxtFile, _T(" Media Types:\n"));
40 for (int j = 0, n = PDFPrnGetMediaTypeCount(pPrinter, szPrinterName); j < ; j++)
41 _ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetMediaTypeName(pPrinter, j));
42
43 // Close printer
44 PDFPrnClosePrinter(pPrinter);
45
46 _ftprintf(pTxtFile, _T("----------------------------------------\n"));
47}
48
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Create output text file containing a list of all available printers in localhost
5 using (StreamWriter writer = new StreamWriter(txtFilePath))
6 {
7 writer.WriteLine("List of available printers in localhost:");
8 writer.WriteLine("----------------------------------------");
9
10 // Loop over all printers in localhost
11 for (int printerNo = 0, printerCount = printer.GetPrinterCount(""); printerNo < printerCount; printerNo++)
12 {
13 string printerName = printer.GetPrinter(printerNo);
14 writer.WriteLine("Printer {0}: {1}", printerNo + 1, printerName);
15
16 // Open printer
17 if (!printer.OpenPrinter(printerName))
18 throw new Exception(String.Format("Printer {0} could not be opened. " +
19 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
20
21 // List duplex modes for selected printer
22 writer.WriteLine(" Duplex Modes:");
23 for (int i = 0, n = printer.GetDuplexModeCount(printerName); i < n; i++)
24 writer.WriteLine(" - {0}", printer.GetDuplexMode(i));
25
26 // List bins for selected printer
27 writer.WriteLine(" Bins:");
28 for (int i = 0, n = printer.GetBinCount(printerName); i < n; i++)
29 writer.WriteLine(" - {0}", printer.GetBin(i));
30
31 // List paper sizes for selected printer
32 writer.WriteLine(" Paper:");
33 for (int i = 0, n = printer.GetPaperCount(printerName); i < n; i++)
34 writer.WriteLine(" - {0}", printer.GetPaper(i));
35
36 // List media types for selected printer
37 writer.WriteLine(" Media Types:");
38 for (int i = 0, n = printer.GetMediaTypeCount(printerName); i < n; i++)
39 writer.WriteLine(" - {0}", printer.GetMediaTypeName(i));
40
41 // Close printer
42 printer.ClosePrinter();
43
44 writer.WriteLine("----------------------------------------");
45 }
46 }
47}
48
1// Create the printer
2printer = new Printer();
3
4// Create output text file containing a list of all available printers in localhost
5PrintWriter writer = new PrintWriter(new FileOutputStream(txtFilePath));
6
7writer.println("List of available printers in localhost:");
8writer.println("----------------------------------------");
9
10// Loop over all printers in localhost
11for (int printerNo = 0, printerCount = printer.getPrinterCount(""); printerNo < printerCount; printerNo++)
12{
13 String printerName = printer.getPrinter(printerNo);
14 writer.println("Printer "+ (printerNo + 1) + ": " + printerName);
15
16 // Open printer
17 if (!printer.openPrinter(printerName))
18 throw new IOException(String.format("Printer %s could not be opened. " +
19 "%s (ErrorCode: 0x%08x).", printerName, printer.getErrorMessage(),
20 printer.getErrorCode()));
21
22 // List duplex modes for selected printer
23 writer.println(" Duplex Modes:");
24 for (int i = 0, n = printer.getDuplexModeCount(printerName); i < n; i++)
25 writer.println(" - " + printer.getDuplexMode(i));
26
27 // List bins for selected printer
28 writer.println(" Bins:");
29 for (int i = 0, n = printer.getBinCount(printerName); i < n; i++)
30 writer.println(" - " + printer.getBin(i));
31
32 // List paper sizes for selected printer
33 writer.println(" Paper:");
34 for (int i = 0; i < printer.getPaperCount(printerName); i++)
35 writer.println(" - " + printer.getPaper(i));
36
37 // List media types for selected printer
38 writer.println(" Media Types:");
39 for (int i = 0, n = printer.getMediaTypeCount(printerName); i < ; i++)
40 writer.println(" - " + printer.getMediaTypeName(i));
41
42 // Close printer
43 if (!printer.closePrinter())
44 throw new IOException(String.format("Printer %s could not be closed. " +
45 "%s (ErrorCode: 0x%08x).", printerName, printer.getErrorMessage(),
46 printer.getErrorCode()));
47
48 writer.println("----------------------------------------");
49}
50writer.flush();
51writer.close();
52
Customize Printing
Print document using the specified duplex mode
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Open input file
5if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
6{
7 _tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
8 iReturnValue = 1;
9 goto cleanup;
10}
11
12// Open printer
13if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
14{
15 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Set duplex mode when not using default duplex mode
21if (iDuplexMode != -1)
22 PDFPrnSetDuplex(pPrinter, iDuplexMode);
23
24// Begin print job
25if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
26{
27 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
28 iReturnValue = 1;
29 goto cleanup;
30}
31
32// Print pages of input file
33for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
34{
35 if (!PDFPrnPrintPage(pPrinter, iPage))
36 {
37 _tprintf(_T("Page %d of %s could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
38 iReturnValue = 1;
39 goto cleanup;
40 }
41}
42
43// End print job
44if (!PDFPrnEndDocument(pPrinter))
45{
46 _tprintf(_T("The print job could not be completed or connection could not be closed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
47 iReturnValue = 1;
48 goto cleanup;
49}
50
51// Close input file
52PDFPrnClose(pPrinter);
53
54
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Open input file
5 if (!printer.Open(inputPath, ""))
6 throw new Exception(String.Format("Input file {0} could not be opened. " +
7 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
8
9 // Open printer
10 if (!printer.OpenPrinter(printerName))
11 throw new Exception(String.Format("Printer {0} could not be opened. " +
12 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
13
14 // Set duplex mode when not using default duplex mode
15 if (setDuplexMode)
16 printer.Duplex = duplexMode;
17
18 // Begin print job
19 if (!printer.BeginDocument("My print job."))
20 throw new Exception(String.Format("Could not connect to the printer device. " +
21 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
22
23 // Print pages of input file
24 for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
25 {
26 if (!printer.PrintPage(pageNo))
27 throw new Exception(String.Format("Page {0} of {1} could not be printed successfully" +
28 "on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath, printerName,
29 printer.ErrorMessage, printer.ErrorCode));
30 }
31
32 // End print job
33 if (!printer.EndDocument())
34 throw new Exception(String.Format("The print job could not be completed or the ´" +
35 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
36 printer.ErrorCode));
37
38 // Close printer
39 if (!printer.ClosePrinter())
40 throw new Exception(String.Format("Printer {0} could not be closed. " +
41 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
42
43 // Close input file
44 if (!printer.Close())
45 throw new Exception(String.Format("Input file {0} could not be closed. " +
46 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
47}
48
1// Create the printer
2printer = new Printer();
3
4// Open input file
5if (!printer.open(inputPath, ""))
6 throw new IOException(String.format("Input file %s could not be opened. " +
7 "%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
8
9// Open printer
10if (!printer.openPrinter(printerName))
11 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
12 printerName, printer.getErrorMessage(), printer.getErrorCode()));
13
14// Set duplex mode when not using default duplex mode
15if (duplexMode != -1)
16 printer.setDuplex(duplexMode);
17
18// Begin print job
19if (!printer.beginDocument("My print job."))
20 throw new IOException(String.format("Could not connect to the printer device. " +
21 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
22
23// Print pages of input file
24for (int pageNo = 1; pageNo <= printer.getPageCount();pageNo++)
25{
26 if (!printer.printPage(pageNo))
27 throw new IOException(String.format("Page %d of %s could not be printed successfully " +
28 "on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
29 printer.getErrorMessage(), printer.getErrorCode()));
30}
31
32// End print job
33if (!printer.endDocument())
34 throw new IOException(String.format("The print job could not be completed or the connection " +
35 "could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
36 printer.getErrorCode()));
37
38// Close input file
39printer.close();
40
Print a PDF document using a device mode file
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Open input file
5if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
6{
7 _tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
8 iReturnValue = 1;
9 goto cleanup;
10}
11
12// Open printer
13if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
14{
15 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Open device mode file
21pStream = _tfopen(szDevModeFile, _T("rb"));
22fseek(pStream, 0, SEEK_END);
23nLength = ftell(pStream);
24void* pDevBuffer = malloc(nLength);
25fseek(pStream, 0, SEEK_SET);
26fread(pDevBuffer, 1, nLength, pStream);
27fclose(pStream);
28
29// Set device mdoe
30if (!PDFPrnSetDevMode(pPrinter, pDevBuffer, nLength))
31{
32 _tprintf(_T("Device mode could not be set. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
33 iReturnValue = 1;
34 goto cleanup;
35}
36
37// Begin print job
38if (!PDFPrnBeginDocument(pPrinter, _T("My print job.\n")))
39{
40 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
41 iReturnValue = 1;
42 goto cleanup;
43}
44
45// Loop over all pages of the input file
46for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
47{
48 // Print pages of the input file
49 if (!PDFPrnPrintPage(pPrinter, iPage))
50 {
51 _tprintf(_T("Page %d of input file %s could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
52 iReturnValue = 1;
53 goto cleanup;
54 }
55}
56
57// Close input file
58PDFPrnClose(pPrinter);
59
60// End print job
61if (!PDFPrnEndDocument(pPrinter))
62{
63 _tprintf(_T("The print job could not be completed or the connection could not be closed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
64 iReturnValue = 1;
65 goto cleanup;
66}
67
1// Create the printer object
2using (Printer printer = new Printer())
3{
4
5 // Open input file
6 if (!printer.Open(inputPath, ""))
7 throw new Exception(String.Format("Input file {0} could not be opened. " +
8 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
9
10 // Open printer
11 if (!printer.OpenPrinter(printerName))
12 throw new Exception(String.Format("Printer {0} could not be opened. " +
13 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
14
15 // Set device mode from file
16 printer.DevMode = File.ReadAllBytes(devModeFile);
17
18 // Begin print job
19 if (!printer.BeginDocument("My print job."))
20 throw new Exception(String.Format("Could not connect to the printer device. " +
21 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
22
23 // Loop over all pages of the input file
24 for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
25 {
26 // Print pages of the input file
27 if (!printer.PrintPage(pageNo))
28 throw new Exception(String.Format("Page {0} of input file {1} could not be printed " +
29 "successfully on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath,
30 printerName, printer.ErrorMessage, printer.ErrorCode));
31 }
32
33 // Close input file
34 printer.Close();
35
36 // End print job
37 if (!printer.EndDocument())
38 throw new Exception(String.Format("The print job could not be completed or the " +
39 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
40 printer.ErrorCode));
41}
42
1// Create the printer
2printer = new Printer();
3
4// Open input file
5if (!printer.open(inputPath, ""))
6 throw new Exception(String.format("Input file %s could not be opened. %s (ErrorCode: 0x%08x).",
7 inputPath, printer.getErrorMessage(), printer.getErrorCode()));
8
9// Open printer
10if (!printer.openPrinter(printerName))
11 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
12 printerName, printer.getErrorMessage(), printer.getErrorCode()));
13
14// Read and set device mode file
15byte[] bytes = Files.readAllBytes(Paths.get(devModePath));
16printer.setDevMode(bytes);
17
18// Begin print job
19if (!printer.beginDocument("My print job."))
20 throw new Exception(String.format("Could not connect to the printer device. " +
21 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
22
23// Loop over all pages of the input file
24for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
25{
26 //Print pages of input file
27 if (!printer.printPage(pageNo))
28 throw new Exception(String.format("Page %d of input file %s could not be printed " +
29 "successfully on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath,
30 printerName, printer.getErrorMessage(), printer.getErrorCode()));
31}
32
33// Close input file
34printer.close();
35
36// End print job
37if (!printer.endDocument())
38 throw new IOException(String.format("The print job could not be completed or the connection " +
39 "could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
40 printer.getErrorCode()));
41
Customize the device mode
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4// Open printer
5if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
6{
7 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
8 iReturnValue = 1;
9 goto cleanup;
10}
11
12// Open device mode dialog for the printer at hand
13if (!PDFPrnEditDevMode(pPrinter, 0))
14{
15 _tprintf(_T("Editing Device Mode of printer %s failed. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Save devide mode in to a file
21pDevMode = PDFPrnGetDevMode(pPrinter);
22
23if ((pStream = _tfopen(szDevMode, _T("wb"))) == NULL)
24{
25 _tprintf(_T("Failed to create output device mode file %s.\n"), szDevMode);
26 iReturnValue = 1;
27 goto cleanup;
28}
29
30// Write bytes to output file
31fwrite(pDevMode->m_pData, pDevMode->m_nLength, 1, pStream);
32fclose(pStream);
33
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 // Open printer
5 if (!printer.OpenPrinter(printerName))
6 throw new Exception(String.Format("Printer {0} could not be opened. " +
7 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
8
9 // Open device mode dialog for the printer at hand
10 if (!printer.EditDevMode(IntPtr.Zero))
11 throw new Exception(String.Format("Editing Device Mode of printer {0} failed. " +
12 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
13
14 // Save device mode in to a file
15 byte[] devmode = printer.DevMode;
16 File.WriteAllBytes(DevMode, devmode);
17}
18
1// Create the printer
2printer = new Printer();
3
4// Open printer
5if (!printer.openPrinter(printerName))
6 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
7 printerName, printer.getErrorMessage(), printer.getErrorCode()));
8
9// Open device mode dialog for the printer at hand
10if (!printer.editDevMode(0))
11 throw new IOException(String.format("Editing Device Mode of printer %s failed. " +
12 "%s (ErrorCode: 0x%08x).", printerName, printer.getErrorMessage(), printer.getErrorCode()));
13
14// Save device mode in to a file
15byte[] devmode = printer.getDevMode();
16Files.write(Paths.get(devModePath), devmode, StandardOpenOption.CREATE_NEW);
17
Print PDF with watermarks
1// Create the printer object
2pPrinter = PDFPrnCreateObject();
3
4szPrinterName = PDFPrnGetDefaultPrinter(pPrinter);
5if (szPrinterName == NULL || (_tcscmp(szPrinterName, "") == 0))
6{
7 _tprintf(_T("No default printer available."));
8 iReturnValue = 1;
9 goto cleanup;
10}
11
12// Open input file
13if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
14{
15 _tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
16 iReturnValue = 1;
17 goto cleanup;
18}
19
20// Open printer
21if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
22{
23 _tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
24 iReturnValue = 1;
25 goto cleanup;
26}
27
28// Begin print job
29if (!PDFPrnBeginDocument(pPrinter, _T("Watermark print job.")))
30{
31 _tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
32 iReturnValue = 1;
33 goto cleanup;
34}
35
36PDFPrnSetCenter(pPrinter, 1);
37PDFPrnSetPaperSize(pPrinter, -2);
38
39// Print pages of input file
40for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
41{
42 // Add watermark with its properties on every second page, starting with first page
43 if (iPage % 2 == 1)
44 {
45 PDFPrnSetPageNo(pPrinter, iPage);
46 fWidth = PDFPrnGetPageWidth(pPrinter);
47 fHeight = PDFPrnGetPageHeight(pPrinter);
48 fFontSize = (float)sqrt(fWidth * fWidth + fHeight * fHeight) / _tcslen(szWatermarkText);
49
50 PDFPrnSetWatermarkBold(pPrinter, 1);
51 PDFPrnSetWatermarkOutline(pPrinter, 1);
52 PDFPrnSetWatermarkFontName(pPrinter, _T("Courier"));
53 PDFPrnSetWatermarkFontSize(pPrinter, fFontSize);
54 PDFPrnSetWatermarkColor(pPrinter, 0xCCCCCC);
55 PDFPrnSetWatermarkXPos(pPrinter, fWidth / 5);
56 PDFPrnSetWatermarkYPos(pPrinter, (fHeight - fHeight / 5 + fFontSize / 2));
57 PDFPrnSetWatermarkAngle(pPrinter, (float)atan((double)fHeight / fWidth));
58 PDFPrnAddWatermarkText(pPrinter, szWatermarkText);
59 }
60 else
61 {
62 // Remove watermark
63 PDFPrnDeleteWatermarks(pPrinter);
64 }
65
66 // Print page
67 if (!PDFPrnPrintPage(pPrinter, iPage))
68 {
69 _tprintf(_T("Page %d of %s could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szInputPath, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
70 iReturnValue = 1;
71 goto cleanup;
72 }
73}
74
75// Close input file
76PDFPrnClose(pPrinter);
77
78// End print job
79if (!PDFPrnEndDocument(pPrinter))
80{
81 _tprintf(_T("The print job could not be completed or the connection could not be closed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
82 iReturnValue = 1;
83 goto cleanup;
84}
85
1// Create the printer object
2using (Printer printer = new Printer())
3{
4 string printerName = printer.DefaultPrinter;
5 if (String.IsNullOrEmpty(printerName))
6 throw new Exception(String.Format("No default printer available."));
7
8 // Open input file
9 if (!printer.Open(inputPath, ""))
10 throw new Exception(String.Format("Input file {0} could not be opened. " +
11 "{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
12
13 // Open printer
14 if (!printer.OpenPrinter(printerName))
15 throw new Exception(String.Format("Printer {0} could not be opened. " +
16 "{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
17
18 // Begin print job
19 if (!printer.BeginDocument("Watermark print job."))
20 throw new Exception(String.Format("Could not connect to the printer device. " +
21 "{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
22
23 printer.Center = true;
24 printer.PaperSize = -2;
25
26 // Print pages of input file
27 for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
28 {
29 // Add watermark with its properties on every second page, starting with first page
30 if (pageNo % 2 == 1)
31 {
32 printer.PageNo = pageNo;
33 float width = printer.PageWidth;
34 float height = printer.PageHeight;
35 float fontSize = (float)Math.Sqrt(width * width + height * height) /
36 watermarkText.Length;
37 printer.WatermarkBold = true;
38 printer.WatermarkOutline = true;
39 printer.WatermarkFontName = "Courier";
40 printer.WatermarkFontSize = fontSize;
41 printer.WatermarkColor = 0xCCCCCC;
42 printer.WatermarkXPos = width / 5;
43 printer.WatermarkYPos = (height - height / 5 + fontSize / 2);
44 printer.WatermarkAngle = (float)Math.Atan((double)height / width);
45 printer.AddWatermarkText(watermarkText);
46 }
47 else
48 {
49 // Remove watermark
50 printer.DeleteWatermarks();
51 }
52
53 // Print page
54 if (!printer.PrintPage(pageNo))
55 throw new Exception(String.Format("Page {0} of {1} could not be printed successfully " +
56 "on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath, printerName,
57 printer.ErrorMessage, printer.ErrorCode));
58 }
59
60 // Close input file
61 printer.Close();
62
63 // End print job
64 if (!printer.EndDocument())
65 throw new Exception(String.Format("The print job could not be completed or the " +
66 "connection could not be closed. {0} (ErrorCode: 0x{1:x}).",
67 printer.ErrorMessage, printer.ErrorCode));
68}
69
1// Create the printer
2printer = new Printer();
3
4String printerName = printer.getDefaultPrinter();
5if (printerName ==null || printerName.trim().equals(""))
6 throw new IOException(String.format("No default printer available."));
7
8// Open input file
9if (!printer.open(inputPath, ""))
10 throw new IOException(String.format("Input file %s could not be opened. " +
11 "%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
12
13// Open printer
14if (!printer.openPrinter(printerName))
15 throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
16 printerName, printer.getErrorMessage(), printer.getErrorCode()));
17
18// Begin print job
19if (!printer.beginDocument("Watermark print job."))
20 throw new IOException(String.format("Could not connect to the printer device. " +
21 "%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
22
23printer.setCenter(true);
24printer.setPaperSize(-2);
25
26// Print pages of input file
27for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
28{
29 // Add watermark with its properties on every second page, starting with the first page
30 if (pageNo % 2 == 1)
31 {
32 printer.setPageNo(pageNo);
33 float width = printer.getPageWidth();
34 float height = printer.getPageHeight();
35 float fontSize = (float)Math.sqrt(width * width + height * height) / watermarkText.length();
36 printer.setWatermarkBold(true);
37 printer.setWatermarkOutline(true);
38 printer.setWatermarkFontName("Courier");
39 printer.setWatermarkFontSize(fontSize);
40 printer.setWatermarkColor(0xCCCCCC);
41 printer.setWatermarkXPos(width / 5);
42 printer.setWatermarkYPos((height - height / 5 + fontSize / 2));
43 printer.setWatermarkAngle((float)Math.atan((double)height / width));
44 printer.addWatermarkText(watermarkText);
45 }
46 else
47 {
48 // Remove watermark
49 printer.deleteWatermarks();
50 }
51
52 // Print page
53 if (!printer.printPage(pageNo))
54 throw new Exception(String.format("Page %d of %s could not be printed successfully on " +
55 "printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
56 printer.getErrorMessage(), printer.getErrorCode()));
57}
58
59// Close input file
60printer.close();
61
62// End print job
63if (!printer.endDocument())
64 throw new IOException(String.format("The print job could not be completed or the connection " +
65 "could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
66 printer.getErrorCode()));
67