Two method allow to create or get a PDF of a document:
createPdf
: enforces creation of a PDF and returns the Path of the newly generated PDFgetPdf
: returns data of a newly generated or already existing PDF as byte[]Generated PDFs are preserved until either
String w2mlString = ""; Path destinationFile; // ... initialization try (OpenedDocument document = ((Renderer) renderer).openDocument( "DSN", new TmpCopyFromString(w2mlString))) { // createPdf enforces creation (including full rendering) of the Pdf document // The created file is deleted, when the document is changed or closed, so // probably after a createPdf call you want to copy the file: Path pdfPath = document.createPdf(); Files.copy(pdfPath, destinationFile); } catch (PdfLibraryException e) { // PdfLibraryException are exceptions // triggered by the underlying pdflib // library } catch (RendererException e) { // Exceptions thrown during rendering } catch (Exception e) { // Exceptions thrown during auto-close }
String w2mlString = ""; try (OpenedDocument document = ((Renderer) renderer).openDocument( "DSN", new TmpCopyFromString(w2mlString, W2MLDocumentType.class))) { // getPdf returns the data of an already existing or newly generated // Pdf file. As preserved files are deleted automatically, whenever // the document is changed, you should usually call getPdf rather than // createPdf byte[] pdfData = document.getPdf(); } catch (PdfLibraryException e) { // PdfLibraryException are exceptions // triggered by the underlying pdflib // library } catch (RendererException e) { // Exceptions thrown during rendering } catch (Exception e) { // Exceptions thrown during auto-close }
You can provide a pdf profile (aka "joboptions") when creating a Pdf. Please refer to the Plug-Ins documentation for information, how to create and how to manage pdf joboptions.
The Pdf profile can be provided with the Options
parameter to both getPdf
and createPdf
method:
String w2mlString = ""; try (OpenedDocument document = ((Renderer) renderer).openDocument("DSN", new TmpCopyFromString(w2mlString))) { byte[] pdfData = document.getPdf( Options.create().add(Option.PDF_PRESET, "customprofile") ); } catch (Exception e) { // All exceptions }
The profile name can be either
String w2mlString = ""; Path pdfProfilePath = Paths.get("C:\\MyMachine\\customer\\dual\\flyer1979.joboptions"); try (OpenedDocument document = ((Renderer) renderer).openDocument( "DSN", new TmpCopyFromString(w2mlString, W2MLDocumentType.class))) { byte[] pdfData = document.getPdf( Options.create().add(Option.PDF_PRESET, "file://" + pdfProfilePath.toAbsolutePath().toString()) ); } catch (Exception e) { // All exceptions }
getPdf
and createPdf
methods allow to specify a page range to generate. To be precise: the partial PDF is extracted from the complete PDF. Since we cannot predict the page content of particular pages, unless we render the document starting from the first page, we also generate the complete PDF and extract pages afterwards.
String w2mlString = "";
List<Integer> pages = new ArrayList<>();
try (OpenedDocument document = ((Renderer) renderer).openDocument(
"DSN",
new TmpCopyFromString(w2mlString, W2MLDocumentType.class))) {
// get number of spreads in document
int spreadCount = document.getSpreadCount();
// export all 'even' spreads
for (int idx = 0; idx < spreadCount; ++idx) {
if (idx % 2 == 0) pages.add(idx);
}
byte[] pdfData = document.getPdf(pages);
}
catch (PdfLibraryException e) {
// PdfLibraryException are exceptions
// triggered by the underlying pdflib
// library
}
catch (RendererException e) {
// Exceptions thrown during rendering
}
catch (Exception e) {
// Exceptions thrown during auto-close
}