Aperçu d'exemples
Printing
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Begin print job
if (!printer.BeginDocument("My print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Loop over all input documents
foreach (string path in inputPaths)
{
// Open input file
if (!printer.Open(path, ""))
throw new Exception(String.Format("Input file {0} could not be opened. {1} " +
"(ErrorCode: 0x{2:x}).", path, printer.ErrorMessage, printer.ErrorCode));
// Loop over all pages of selected file
for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
{
// Print pages of input file
if (!printer.PrintPage(pageNo))
throw new Exception(String.Format("Page {0} could not be printed successfully on " +
"printer {1}. {2} (ErrorCode: 0x{3:x}).", pageNo, printerName,
printer.ErrorMessage, printer.ErrorCode));
}
// Close input file
printer.Close();
}
// End print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the " +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
printer.ErrorCode));
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Begin print job
if (!printer.beginDocument("My print job."))
throw new Exception(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).",printer.getErrorMessage(), printer.getErrorCode()));
// Loop over all input documents
for (String path : inputPaths)
{
// Open input file
if (!printer.open(path, ""))
throw new IOException(String.format("Input file %s could not be opened. " +
"%s (ErrorCode: 0x%08x).", path, printer.getErrorMessage(),
printer.getErrorCode()));
// Loop over all pages of selected file
for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
{
//Print pages of input file
if (!printer.printPage(pageNo))
throw new Exception(String.format("Page %d could not be printed successfully " +
"on printer %s. %s (ErrorCode: 0x%08x).", pageNo, printerName,
printer.getErrorMessage(), printer.getErrorCode()));
}
// Close input file
printer.close();
}
// End print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the connection " +
"could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opned. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Begin print job
if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Loop over all input documents
for (int i = 0; i < nInputPaths; i++)
{
// Open input file
if (!PDFPrnOpen(pPrinter, szInputPaths[i], _T("")))
{
_tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPaths[i], PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Loop over all pages of selected file
for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
{
// Print pages of input file
if (!PDFPrnPrintPage(pPrinter, iPage))
{
_tprintf(_T("Page %d could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
}
// Close input file
PDFPrnClose(pPrinter);
}
// End print job
if (!PDFPrnEndDocument(pPrinter))
{
_tprintf(_T("The print job could not be completed or the connection could not be closed. %s (ErrorCode: 0x%08x)."), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
C sample:
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Start chain of linked print jobs.
if (!printer.BeginGroup())
throw new Exception(String.Format("Starting chain of linked print jobs failed. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Print first print job
PrintDocument(printer, firstInputPath, printerName);
// Print second print job
PrintDocument(printer, secondInputPath, printerName);
// Define end of the chain of linked print jobs
if (!printer.EndGroup())
throw new Exception(String.Format("End of print jobs chain was not set successfully. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Close printer
printer.ClosePrinter();
}
private static void PrintDocument(Printer printer, string inputPath, string printerName)
{
// Begin print job
if (!printer.BeginDocument("My print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Open input file
if (!printer.Open(inputPath, ""))
throw new Exception(String.Format("Input file {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
// Loop over all pages of the input file
for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
{
// Print pages of the input file
if (!printer.PrintPage(pageNo))
throw new Exception(String.Format("Page {0} of input file {1} could not be printed " +
"successfully on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath,
printerName, printer.ErrorMessage, printer.ErrorCode));
}
// Close input file
printer.Close();
// End print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the " +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
printer.ErrorCode));
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Start chain of linked print jobs
if (!printer.beginGroup())
throw new IOException(String.format("Starting chain of linked print jobs failed. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Print first job
printDocument(printer, firstInputPath, printerName);
// Print second job
printDocument(printer, secondInputPath, printerName);
// Define end of the chain of linked print jobs
if (!printer.endGroup())
throw new IOException(String.format("End of print jobs chain was not set successfully. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Close printer
printer.closePrinter();
private static void printDocument(Printer printer, String inputPath, String printerName)
throws IOException
{
// Begin print job
if (!printer.beginDocument("My print job."))
throw new IOException(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Open input file
if (!printer.open(inputPath, ""))
throw new IOException(String.format("Input file %s could not be opened. " +
"%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(),
printer.getErrorCode()));
// Loop over all pages of the input file
for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
{
// Print pages of the input file
if (!printer.printPage(pageNo))
throw new IOException(String.format("Page %d of %s could not be printed successfully" +
" on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
printer.getErrorMessage(), printer.getErrorCode()));
}
// Close input file
printer.close();
// End print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the " +
"connection could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
}
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Start chain of linked print jobs
if (!PDFPrnBeginGroup(pPrinter))
{
_tprintf(_T("Starting chain of linked print jobs failed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Print first job
printDocument(pPrinter, szFirstInputPath, szPrinterName);
// Print second job
printDocument(pPrinter, szSecondInputPath, szPrinterName);
// Define end of the chain of linked print jobs
if (!PDFPrnEndGroup(pPrinter))
{
_tprintf(_T("End of print job chain was not set successfully. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Close printer
PDFPrnClosePrinter(pPrinter);
void printDocument(TPdfPrinter* pPrinter, TCHAR* szInputPath, TCHAR* szPrinterName)
{
// Begin print job
if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
return;
}
// Open input file
if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
{
_tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Loop over all pages of the input file
for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
{
// Print pages of the input file
if (!PDFPrnPrintPage(pPrinter, iPage))
{
_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));
iReturnValue = 1;
goto cleanup;
}
}
// Close input file
PDFPrnClose(pPrinter);
// End print job
if (!PDFPrnEndDocument(pPrinter))
{
_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));
iReturnValue = 1;
}
return;
cleanup:
PDFPrnEndDocument(pPrinter);
}
C sample:
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Open input file
if (!printer.Open(inputPath, ""))
throw new Exception(String.Format("Input file {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
// Open first printer
if (!printer.OpenPrinter(firstPrinterName))
throw new Exception(String.Format("Printer {0} could not be opened. {1} " +
"(ErrorCode: 0x{2:x}).", firstPrinterName, printer.ErrorMessage, printer.ErrorCode));
// Begin first print job
if (!printer.BeginDocument("My first print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Print desired page of the input file
if (!printer.PrintPage(pageNumber))
throw new Exception(String.Format("Page {0} of {1} could not be printed successfully on " +
"printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNumber, inputPath, firstPrinterName,
printer.ErrorMessage, printer.ErrorCode));
// End first print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the " +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
printer.ErrorCode));
// Close first printer
printer.ClosePrinter();
// Open second printer
if (!printer.OpenPrinter(secondPrinterName))
throw new Exception(String.Format("Printer {0} could not be opened. {1} " +
"(ErrorCode: 0x{2:x}).", secondPrinterName, printer.ErrorMessage, printer.ErrorCode));
// Begin second print job
if (!printer.BeginDocument("My second print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{2:x}).", printer.ErrorMessage, printer.ErrorCode));
// Loop over second (if exist) to last page.
for (int pageNo = 2; pageNo < printer.PageCount; pageNo++)
{
// Print remaining pages of input file
if (!printer.PrintPage(pageNo))
throw new Exception(String.Format("Page {0} could not be printed successfully " +
"on printer {1}. {2} (ErrorCode: 0x{3:x}).", pageNo, secondPrinterName,
printer.ErrorMessage, printer.ErrorCode));
}
// End second print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the " +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
printer.ErrorCode));
// Close second printer
printer.ClosePrinter();
// Close input file
printer.Close();
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Open input file
if (!printer.open(inputPath, ""))
throw new IOException(String.format("Input file %s could not be opened. " +
"%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
// Open first printer
if (!printer.openPrinter(firstPrinterName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
firstPrinterName, printer.getErrorMessage(), printer.getErrorCode()));
// Begin first print job
if (!printer.beginDocument("My first print job."))
throw new IOException(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Print desired page of the input file
if (!printer.printPage(pageNumber))
throw new IOException(String.format("Page %d of %s could not be printed successfully " +
"on printer %s. %s (ErrorCode: 0x%08x).", pageNumber, inputPath, firstPrinterName,
printer.getErrorMessage(), printer.getErrorCode()));
// End first print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the connection " +
"could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
// Close first printer
printer.closePrinter();
// Open second printer
if (!printer.openPrinter(secondPrinterName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
secondPrinterName, printer.getErrorMessage(), printer.getErrorCode()));
// Begin second print job
if (!printer.beginDocument("My second print job."))
throw new IOException(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Loop over second (if exist) to last page
for (int pageNo = 2; pageNo < printer.getPageCount(); pageNo++)
{
// Print remaining pages of input file
if (!printer.printPage(pageNo))
throw new IOException(String.format("Page %d could not be printed successfully on " +
"printer %s. %s (ErrorCode: 0x%08x).", pageNo, secondPrinterName,
printer.getErrorMessage(), printer.getErrorCode()));
}
// End second print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the connection " +
"could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
// Close second printer
printer.closePrinter();
// Close input file
printer.close();
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Open input file
if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
{
_tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
}
// Open first printer
if (!PDFPrnOpenPrinter(pPrinter, szFirstPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szFirstPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Begin first print job
if (!PDFPrnBeginDocument(pPrinter, _T("My first print job.\n")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Print desired page of the input file
if (!PDFPrnPrintPage(pPrinter, iPageNumber))
{
_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));
iReturnValue = 1;
goto cleanup;
}
// End first print job
if (!PDFPrnEndDocument(pPrinter))
{
_tprintf(_T("The print job could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), szFirstPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Close first printer
PDFPrnClosePrinter(pPrinter);
// Open second printer
if (!PDFPrnOpenPrinter(pPrinter, szSecondPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szSecondPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Begin second print job
if (!PDFPrnBeginDocument(pPrinter, _T("My second print job.")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Loop over second (if exist) to last page
for (int iPage = 2; iPage < PDFPrnGetPageCount(pPrinter); iPage++)
{
// Print remaining pages of input file
if (!PDFPrnPrintPage(pPrinter, iPage))
{
_tprintf(_T("Page %d could not be printed successfull on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szSecondPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
}
// End second print job
if (!PDFPrnEndDocument(pPrinter))
{
_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));
iReturnValue = 1;
goto cleanup;
}
// Close second printer
PDFPrnClosePrinter(pPrinter);
// Close input file
PDFPrnClose(pPrinter);
C sample:
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Print input file to one specified printer
if (!printer.PrintFile(inputPath, printerName, "", firstPage, lastPage))
throw new Exception(String.Format("Printing input file {0} failed. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Print input file to one specified printer
if (!printer.printFile(inputPath, printerName, "", firstPage, lastPage))
throw new IOException(String.format("Printing input file %s failed. %s (ErrorCode: 0x%08x).",
inputPath, printer.getErrorMessage(), printer.getErrorCode()));
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Print input file to one specified printer
if (!PDFPrnPrintFile(pPrinter, szInputPath, szPrinterName, _T(""), iFirstPage, iLastPage))
{
_tprintf(_T("Printing input file %s failed. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
}
C sample:
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Set name of file to print to
printer.Output = outputPath;
// Begin print job
if (!printer.BeginDocument("My print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Open input file
if (!printer.Open(inputPath, ""))
throw new Exception(String.Format("Input file {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
// Loop over all pages of selected file
for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
{
// Print pages of input file
if (!printer.PrintPage(pageNo))
throw new Exception(String.Format("Page {0} could not be printed successfully on " +
"printer {1}. {2} (ErrorCode: 0x{3:x}).", pageNo, printerName, printer.ErrorMessage,
printer.ErrorCode));
}
// Close input file
printer.Close();
// End print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the " +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
printer.ErrorCode));
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Set name of file to print to
printer.setOutput(outputPath);
// Begin print job
if (!printer.beginDocument("My print job."))
throw new IOException(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Open input file
if (!printer.open(inputPath, ""))
throw new IOException(String.format("Input file %s could not be opened. " +
"%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
// Loop over all pages of selected file
for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
{
if (!printer.printPage(pageNo))
throw new IOException(String.format("Page %d of %s could not be printed successfully " +
"on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
printer.getErrorMessage(), printer.getErrorCode()));
}
// Close input file
printer.close();
// End print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the connection " +
"could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
Java sample:
C sample:
pPrinter = PDFPrnCreateObject();
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Set name of file to print to
PDFPrnSetOutput(pPrinter, szOutputPath);
// Begin print job
if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Open input file
if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
{
_tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Loop over all pages of selected file
for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
{
// Print pages of input file
if (!PDFPrnPrintPage(pPrinter, iPage))
{
_tprintf(_T("Page %d could not be printed successfully on printer %s. %s (ErrorCode: 0x%08x).\n"), iPage, szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
}
// Close input file
PDFPrnClose(pPrinter);
// End print job
if (!PDFPrnEndDocument(pPrinter))
{
_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));
iReturnValue = 1;
goto cleanup;
}
C sample:
Features and Configuration
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Create output text file containing a list of all available printers in localhost
using (StreamWriter writer = new StreamWriter(txtFilePath))
{
writer.WriteLine("List of available printers in localhost:");
writer.WriteLine("----------------------------------------");
// Loop over all printers in localhost
for (int printerNo = 0, printerCount = printer.GetPrinterCount(""); printerNo < printerCount; printerNo++)
{
string printerName = printer.GetPrinter(printerNo);
writer.WriteLine("Printer {0}: {1}", printerNo + 1, printerName);
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// List duplex modes for selected printer
writer.WriteLine(" Duplex Modes:");
for (int i = 0, n = printer.GetDuplexModeCount(printerName); i < n; i++)
writer.WriteLine(" - {0}", printer.GetDuplexMode(i));
// List bins for selected printer
writer.WriteLine(" Bins:");
for (int i = 0, n = printer.GetBinCount(printerName); i < n; i++)
writer.WriteLine(" - {0}", printer.GetBin(i));
// List paper sizes for selected printer
writer.WriteLine(" Paper:");
for (int i = 0, n = printer.GetPaperCount(printerName); i < n; i++)
writer.WriteLine(" - {0}", printer.GetPaper(i));
// List media types for selected printer
writer.WriteLine(" Media Types:");
for (int i = 0, n = printer.GetMediaTypeCount(printerName); i < n; i++)
writer.WriteLine(" - {0}", printer.GetMediaTypeName(i));
// Close printer
printer.ClosePrinter();
writer.WriteLine("----------------------------------------");
}
}
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Create output text file containing a list of all available printers in localhost
PrintWriter writer = new PrintWriter(new FileOutputStream(txtFilePath));
writer.println("List of available printers in localhost:");
writer.println("----------------------------------------");
// Loop over all printers in localhost
for (int printerNo = 0, printerCount = printer.getPrinterCount(""); printerNo < printerCount; printerNo++)
{
String printerName = printer.getPrinter(printerNo);
writer.println("Printer "+ (printerNo + 1) + ": " + printerName);
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. " +
"%s (ErrorCode: 0x%08x).", printerName, printer.getErrorMessage(),
printer.getErrorCode()));
// List duplex modes for selected printer
writer.println(" Duplex Modes:");
for (int i = 0, n = printer.getDuplexModeCount(printerName); i < n; i++)
writer.println(" - " + printer.getDuplexMode(i));
// List bins for selected printer
writer.println(" Bins:");
for (int i = 0, n = printer.getBinCount(printerName); i < n; i++)
writer.println(" - " + printer.getBin(i));
// List paper sizes for selected printer
writer.println(" Paper:");
for (int i = 0; i < printer.getPaperCount(printerName); i++)
writer.println(" - " + printer.getPaper(i));
// List media types for selected printer
writer.println(" Media Types:");
for (int i = 0, n = printer.getMediaTypeCount(printerName); i < ; i++)
writer.println(" - " + printer.getMediaTypeName(i));
// Close printer
if (!printer.closePrinter())
throw new IOException(String.format("Printer %s could not be closed. " +
"%s (ErrorCode: 0x%08x).", printerName, printer.getErrorMessage(),
printer.getErrorCode()));
writer.println("----------------------------------------");
}
writer.flush();
writer.close();
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Create output text file containing a list of all available printers in localhost
pTxtFile = _tfopen(szTextFilePath, _T("w"));
_ftprintf(pTxtFile, _T("List of available printers in localhost:\n"));
_ftprintf(pTxtFile, _T("----------------------------------------\n"));
// Loop over all printers in localhost
for (int iPrinter = 0, nPrinters = PDFPrnGetPrinterCount(pPrinter, ""); iPrinter < nPrinters; iPrinter++)
{
TCHAR szPrinterName[256];
_tcscpy(szPrinterName, PDFPrnGetPrinter(pPrinter, iPrinter));
_ftprintf(pTxtFile, _T("Printer %d: %s\n"), iPrinter + 1, szPrinterName);
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
continue;
}
// List duplex modes for selected printer
_ftprintf(pTxtFile, _T(" Duplex Modes:\n"));
for (int j = 0, n = PDFPrnGetDuplexModeCount(pPrinter, szPrinterName); j < n; j++)
_ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetDuplexMode(pPrinter, j));
// List bins for selected printer
_ftprintf(pTxtFile, _T(" Bins:\n"));
for (int j = 0, n = PDFPrnGetBinCount(pPrinter, szPrinterName); j < n; j++)
_ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetBin(pPrinter, j));
// List paper sizes for selected printer
_ftprintf(pTxtFile, _T(" Paper:\n"));
for (int j = 0, n = PDFPrnGetPaperCount(pPrinter, szPrinterName); j < n; j++)
_ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetPaper(pPrinter, j));
// List media types for selected printer
_ftprintf(pTxtFile, _T(" Media Types:\n"));
for (int j = 0, n = PDFPrnGetMediaTypeCount(pPrinter, szPrinterName); j < ; j++)
_ftprintf(pTxtFile, _T(" - %s\n"), PDFPrnGetMediaTypeName(pPrinter, j));
// Close printer
PDFPrnClosePrinter(pPrinter);
_ftprintf(pTxtFile, _T("----------------------------------------\n"));
}
C sample:
Customize Printing
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Open input file
if (!printer.Open(inputPath, ""))
throw new Exception(String.Format("Input file {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Set duplex mode when not using default duplex mode
if (setDuplexMode)
printer.Duplex = duplexMode;
// Begin print job
if (!printer.BeginDocument("My print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Print pages of input file
for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
{
if (!printer.PrintPage(pageNo))
throw new Exception(String.Format("Page {0} of {1} could not be printed successfully" +
"on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath, printerName,
printer.ErrorMessage, printer.ErrorCode));
}
// End print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the ´" +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
printer.ErrorCode));
// Close printer
if (!printer.ClosePrinter())
throw new Exception(String.Format("Printer {0} could not be closed. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Close input file
if (!printer.Close())
throw new Exception(String.Format("Input file {0} could not be closed. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Open input file
if (!printer.open(inputPath, ""))
throw new IOException(String.format("Input file %s could not be opened. " +
"%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Set duplex mode when not using default duplex mode
if (duplexMode != -1)
printer.setDuplex(duplexMode);
// Begin print job
if (!printer.beginDocument("My print job."))
throw new IOException(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Print pages of input file
for (int pageNo = 1; pageNo <= printer.getPageCount();pageNo++)
{
if (!printer.printPage(pageNo))
throw new IOException(String.format("Page %d of %s could not be printed successfully " +
"on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
printer.getErrorMessage(), printer.getErrorCode()));
}
// End print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the connection " +
"could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
// Close input file
printer.close();
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Open input file
if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
{
_tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Set duplex mode when not using default duplex mode
if (iDuplexMode != -1)
PDFPrnSetDuplex(pPrinter, iDuplexMode);
// Begin print job
if (!PDFPrnBeginDocument(pPrinter, _T("My print job.")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Print pages of input file
for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
{
if (!PDFPrnPrintPage(pPrinter, iPage))
{
_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));
iReturnValue = 1;
goto cleanup;
}
}
// End print job
if (!PDFPrnEndDocument(pPrinter))
{
_tprintf(_T("The print job could not be completed or connection could not be closed. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Close input file
PDFPrnClose(pPrinter);
C sample:
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Open input file
if (!printer.Open(inputPath, ""))
throw new Exception(String.Format("Input file {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Set device mode from file
printer.DevMode = File.ReadAllBytes(devModeFile);
// Begin print job
if (!printer.BeginDocument("My print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
// Loop over all pages of the input file
for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
{
// Print pages of the input file
if (!printer.PrintPage(pageNo))
throw new Exception(String.Format("Page {0} of input file {1} could not be printed " +
"successfully on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath,
printerName, printer.ErrorMessage, printer.ErrorCode));
}
// Close input file
printer.Close();
// End print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the " +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage,
printer.ErrorCode));
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Open input file
if (!printer.open(inputPath, ""))
throw new Exception(String.format("Input file %s could not be opened. %s (ErrorCode: 0x%08x).",
inputPath, printer.getErrorMessage(), printer.getErrorCode()));
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Read and set device mode file
byte[] bytes = Files.readAllBytes(Paths.get(devModePath));
printer.setDevMode(bytes);
// Begin print job
if (!printer.beginDocument("My print job."))
throw new Exception(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
// Loop over all pages of the input file
for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
{
//Print pages of input file
if (!printer.printPage(pageNo))
throw new Exception(String.format("Page %d of input file %s could not be printed " +
"successfully on printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath,
printerName, printer.getErrorMessage(), printer.getErrorCode()));
}
// Close input file
printer.close();
// End print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the connection " +
"could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Open input file
if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
{
_tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Open device mode file
pStream = _tfopen(szDevModeFile, _T("rb"));
fseek(pStream, 0, SEEK_END);
nLength = ftell(pStream);
void* pDevBuffer = malloc(nLength);
fseek(pStream, 0, SEEK_SET);
fread(pDevBuffer, 1, nLength, pStream);
fclose(pStream);
// Set device mdoe
if (!PDFPrnSetDevMode(pPrinter, pDevBuffer, nLength))
{
_tprintf(_T("Device mode could not be set. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Begin print job
if (!PDFPrnBeginDocument(pPrinter, _T("My print job.\n")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Loop over all pages of the input file
for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
{
// Print pages of the input file
if (!PDFPrnPrintPage(pPrinter, iPage))
{
_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));
iReturnValue = 1;
goto cleanup;
}
}
// Close input file
PDFPrnClose(pPrinter);
// End print job
if (!PDFPrnEndDocument(pPrinter))
{
_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));
iReturnValue = 1;
goto cleanup;
}
C sample:
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Open device mode dialog for the printer at hand
if (!printer.EditDevMode(IntPtr.Zero))
throw new Exception(String.Format("Editing Device Mode of printer {0} failed. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Save device mode in to a file
byte[] devmode = printer.DevMode;
File.WriteAllBytes(DevMode, devmode);
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Open device mode dialog for the printer at hand
if (!printer.editDevMode(0))
throw new IOException(String.format("Editing Device Mode of printer %s failed. " +
"%s (ErrorCode: 0x%08x).", printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Save device mode in to a file
byte[] devmode = printer.getDevMode();
Files.write(Paths.get(devModePath), devmode, StandardOpenOption.CREATE_NEW);
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Open device mode dialog for the printer at hand
if (!PDFPrnEditDevMode(pPrinter, 0))
{
_tprintf(_T("Editing Device Mode of printer %s failed. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Save devide mode in to a file
pDevMode = PDFPrnGetDevMode(pPrinter);
if ((pStream = _tfopen(szDevMode, _T("wb"))) == NULL)
{
_tprintf(_T("Failed to create output device mode file %s.\n"), szDevMode);
iReturnValue = 1;
goto cleanup;
}
// Write bytes to output file
fwrite(pDevMode->m_pData, pDevMode->m_nLength, 1, pStream);
fclose(pStream);
C sample:
C# sample:
// Create the printer object
using (Printer printer = new Printer())
{
string printerName = printer.DefaultPrinter;
if (String.IsNullOrEmpty(printerName))
throw new Exception(String.Format("No default printer available."));
// Open input file
if (!printer.Open(inputPath, ""))
throw new Exception(String.Format("Input file {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", inputPath, printer.ErrorMessage, printer.ErrorCode));
// Open printer
if (!printer.OpenPrinter(printerName))
throw new Exception(String.Format("Printer {0} could not be opened. " +
"{1} (ErrorCode: 0x{2:x}).", printerName, printer.ErrorMessage, printer.ErrorCode));
// Begin print job
if (!printer.BeginDocument("Watermark print job."))
throw new Exception(String.Format("Could not connect to the printer device. " +
"{0} (ErrorCode: 0x{1:x}).", printer.ErrorMessage, printer.ErrorCode));
printer.Center = true;
printer.PaperSize = -2;
// Print pages of input file
for (int pageNo = 1; pageNo <= printer.PageCount; pageNo++)
{
// Add watermark with its properties on every second page, starting with first page
if (pageNo % 2 == 1)
{
printer.PageNo = pageNo;
float width = printer.PageWidth;
float height = printer.PageHeight;
float fontSize = (float)Math.Sqrt(width * width + height * height) /
watermarkText.Length;
printer.WatermarkBold = true;
printer.WatermarkOutline = true;
printer.WatermarkFontName = "Courier";
printer.WatermarkFontSize = fontSize;
printer.WatermarkColor = 0xCCCCCC;
printer.WatermarkXPos = width / 5;
printer.WatermarkYPos = (height - height / 5 + fontSize / 2);
printer.WatermarkAngle = (float)Math.Atan((double)height / width);
printer.AddWatermarkText(watermarkText);
}
else
{
// Remove watermark
printer.DeleteWatermarks();
}
// Print page
if (!printer.PrintPage(pageNo))
throw new Exception(String.Format("Page {0} of {1} could not be printed successfully " +
"on printer {2}. {3} (ErrorCode: 0x{4:x}).", pageNo, inputPath, printerName,
printer.ErrorMessage, printer.ErrorCode));
}
// Close input file
printer.Close();
// End print job
if (!printer.EndDocument())
throw new Exception(String.Format("The print job could not be completed or the " +
"connection could not be closed. {0} (ErrorCode: 0x{1:x}).",
printer.ErrorMessage, printer.ErrorCode));
}
C# sample:
Java sample:
// Create the printer
printer = new Printer();
String printerName = printer.getDefaultPrinter();
if (printerName ==null || printerName.trim().equals(""))
throw new IOException(String.format("No default printer available."));
// Open input file
if (!printer.open(inputPath, ""))
throw new IOException(String.format("Input file %s could not be opened. " +
"%s (ErrorCode: 0x%08x).", inputPath, printer.getErrorMessage(), printer.getErrorCode()));
// Open printer
if (!printer.openPrinter(printerName))
throw new IOException(String.format("Printer %s could not be opened. %s (ErrorCode: 0x%08x).",
printerName, printer.getErrorMessage(), printer.getErrorCode()));
// Begin print job
if (!printer.beginDocument("Watermark print job."))
throw new IOException(String.format("Could not connect to the printer device. " +
"%s (ErrorCode: 0x%08x).", printer.getErrorMessage(), printer.getErrorCode()));
printer.setCenter(true);
printer.setPaperSize(-2);
// Print pages of input file
for (int pageNo = 1; pageNo <= printer.getPageCount(); pageNo++)
{
// Add watermark with its properties on every second page, starting with the first page
if (pageNo % 2 == 1)
{
printer.setPageNo(pageNo);
float width = printer.getPageWidth();
float height = printer.getPageHeight();
float fontSize = (float)Math.sqrt(width * width + height * height) / watermarkText.length();
printer.setWatermarkBold(true);
printer.setWatermarkOutline(true);
printer.setWatermarkFontName("Courier");
printer.setWatermarkFontSize(fontSize);
printer.setWatermarkColor(0xCCCCCC);
printer.setWatermarkXPos(width / 5);
printer.setWatermarkYPos((height - height / 5 + fontSize / 2));
printer.setWatermarkAngle((float)Math.atan((double)height / width));
printer.addWatermarkText(watermarkText);
}
else
{
// Remove watermark
printer.deleteWatermarks();
}
// Print page
if (!printer.printPage(pageNo))
throw new Exception(String.format("Page %d of %s could not be printed successfully on " +
"printer %s. %s (ErrorCode: 0x%08x).", pageNo, inputPath, printerName,
printer.getErrorMessage(), printer.getErrorCode()));
}
// Close input file
printer.close();
// End print job
if (!printer.endDocument())
throw new IOException(String.format("The print job could not be completed or the connection " +
"could not be closed. %s (ErrorCode: 0x%08x).", printer.getErrorMessage(),
printer.getErrorCode()));
Java sample:
C sample:
// Create the printer object
pPrinter = PDFPrnCreateObject();
szPrinterName = PDFPrnGetDefaultPrinter(pPrinter);
if (szPrinterName == NULL || (_tcscmp(szPrinterName, "") == 0))
{
_tprintf(_T("No default printer available."));
iReturnValue = 1;
goto cleanup;
}
// Open input file
if (!PDFPrnOpen(pPrinter, szInputPath, _T("")))
{
_tprintf(_T("Input file %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Open printer
if (!PDFPrnOpenPrinter(pPrinter, szPrinterName))
{
_tprintf(_T("Printer %s could not be opened. %s (ErrorCode: 0x%08x).\n"), szPrinterName, PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
// Begin print job
if (!PDFPrnBeginDocument(pPrinter, _T("Watermark print job.")))
{
_tprintf(_T("Could not connect to the printer device. %s (ErrorCode: 0x%08x).\n"), PDFPrnGetErrorMessage(pPrinter), PDFPrnGetErrorCode(pPrinter));
iReturnValue = 1;
goto cleanup;
}
PDFPrnSetCenter(pPrinter, 1);
PDFPrnSetPaperSize(pPrinter, -2);
// Print pages of input file
for (int iPage = 1; iPage <= PDFPrnGetPageCount(pPrinter); iPage++)
{
// Add watermark with its properties on every second page, starting with first page
if (iPage % 2 == 1)
{
PDFPrnSetPageNo(pPrinter, iPage);
fWidth = PDFPrnGetPageWidth(pPrinter);
fHeight = PDFPrnGetPageHeight(pPrinter);
fFontSize = (float)sqrt(fWidth * fWidth + fHeight * fHeight) / _tcslen(szWatermarkText);
PDFPrnSetWatermarkBold(pPrinter, 1);
PDFPrnSetWatermarkOutline(pPrinter, 1);
PDFPrnSetWatermarkFontName(pPrinter, _T("Courier"));
PDFPrnSetWatermarkFontSize(pPrinter, fFontSize);
PDFPrnSetWatermarkColor(pPrinter, 0xCCCCCC);
PDFPrnSetWatermarkXPos(pPrinter, fWidth / 5);
PDFPrnSetWatermarkYPos(pPrinter, (fHeight - fHeight / 5 + fFontSize / 2));
PDFPrnSetWatermarkAngle(pPrinter, (float)atan((double)fHeight / fWidth));
PDFPrnAddWatermarkText(pPrinter, szWatermarkText);
}
else
{
// Remove watermark
PDFPrnDeleteWatermarks(pPrinter);
}
// Print page
if (!PDFPrnPrintPage(pPrinter, iPage))
{
_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));
iReturnValue = 1;
goto cleanup;
}
}
// Close input file
PDFPrnClose(pPrinter);
// End print job
if (!PDFPrnEndDocument(pPrinter))
{
_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));
iReturnValue = 1;
goto cleanup;
}
C sample: