pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pablo Aravena <paraven...@gmail.com>
Subject How could I print a PDPage object
Date Sat, 15 Jan 2011 14:58:33 GMT
Hi friends

Someone of you knows how to print an specific page inside a PDF
document. I was trying many ways unsuccessfully

First I tried with this simple code:

            document = PDDocument.load(new FileInputStream("Sample.pdf"));
            System.out.println("Retrieving all pages");
            List pages = document.getDocumentCatalog().getAllPages();
            PDPage page = (PDPage) pages.get(1);

            PrinterJob printerJob = PrinterJob.getPrinterJob();
            printerJob.setPrintable(page);
            document.silentPrint(printerJob);

It does't work, the entire document is printed :-(

Then I tried with something more complex based on the Splitter sample

            document = PDDocument.load(new FileInputStream("Sample.pdf"));
            List pages = document.getDocumentCatalog().getAllPages();
            PDPage page = (PDPage) pages.get(1);

            newDoc = new PDDocument(); // new document is created
            newDoc.setDocumentInformation(document.getDocumentInformation());
            newDoc.getDocumentCatalog().setViewerPreferences(document.getDocumentCatalog().getViewerPreferences());

            PDPage newPage = newDoc.importPage(page);
            newPage.setCropBox(page.findCropBox());
            newPage.setMediaBox(page.findMediaBox());
            newPage.setResources(page.getResources());
            newPage.setRotation(page.findRotation());

            PrinterJob printerJob = PrinterJob.getPrinterJob();
            printerJob.setPrintable(newDoc.getPrintable(0));
            newDoc.silentPrint(printerJob);

I was really close with this last code, the document is created but no
text is included inside. Also throws the following warning:

ADVERTENCIA: java.lang.NullPointerException
java.lang.NullPointerException
	at org.apache.pdfbox.util.PDFStreamEngine.processEncodedText(PDFStreamEngine.java:366)
	at org.apache.pdfbox.util.operator.ShowText.process(ShowText.java:45)
	at org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:551)
	at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:274)
	at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:256)
	at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:225)
	at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:107)
	at org.apache.pdfbox.pdmodel.PDPage.print(PDPage.java:824)
	at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1964)
	at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1431)
	at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1247)
	at org.apache.pdfbox.pdmodel.PDDocument.print(PDDocument.java:1039)
	at org.apache.pdfbox.pdmodel.PDDocument.silentPrint(PDDocument.java:1030)
	at com.fedex.international.lac.utilities.print.PDFSpliterSample.main(PDFSpliterSample.java:38)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)


Thanks in advance

Saludos
Pablo

Mime
View raw message