Printing to a PDF File
The snippet below illustrates how to use the PDF3Generator or PDF4Generator object to print to a PDF file.
...
try {
ReportSheet report = createReport();
FileOutputStream output =
new FileOutputStream("output.pdf");
PDF3Generator pdf =
PDF3Generator.getPDFGenerator(output);
pdf.generate(report);
PreviewView previewer = Previewer.createPreviewer();
previewer.setExitOnClose(true);
previewer.pack();
previewer.setVisible(true);
previewer.print(report);
String prop = ReportEnv.getProperty("print");
if(prop != null && prop.equals("true")) {
previewer.printAction();
}
} catch(Exception e) {
e.printStackTrace();
}
public static ReportSheet createReport() {
try {
FileInputStream input =
new FileInputStream("report.srt");
Builder builder =
Builder.getBuilder(Builder.TEMPLATE, input);
return builder.read(".");
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
The PDFPrinter is a simpler version of the PDF3Generator. The PDFPrinter lacks support for generating tables and TOC (Table of Contents). To create a PDFPrinter, pass a File object or a FileOutputStream to the constructor:
PDFPrinter pdf = new PDFPrinter(new File("report.pdf"));
Once you have created the PDFPrinter object, you can retrieve a PrintJob from it by calling getPrintJob():
report.print(pdf.getPrintJob());
After printing is finished, close the PDF stream:
pdf.close(); // or pdf.getPrintJob().end();
It is important to close the PDF stream after printing, otherwise the PDF file may not be complete. You can close the PDF output either by calling the end() method on the PrintJob object returned by the PDFPrinter, or by calling the close() method on the PDFPrinter directly.
| << 3.8.1 Exporting to PDF Programmatically | © 1996-2013 InetSoft Technology Corporation (v11.4) | Compression Options >> |