Example usage for org.apache.pdfbox.pdmodel PDDocument getPage

List of usage examples for org.apache.pdfbox.pdmodel PDDocument getPage

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument getPage.

Prototype

public PDPage getPage(int pageIndex) 
    

Source Link

Document

Returns the page at the given 0-based index.

Usage

From source file:org.geoserver.wms.map.PDFGetMapTest.java

License:Open Source License

/**
 * Returns the last tiling pattern found during a render of the PDF document. Can be used to extract
 * one tiling pattern that gets actually used to render shapes (meant to be used against a document
 * that only has a single tiling pattern)
 * //from w  w w  . j  a v  a2 s  .c o  m
 * @param pdfDocument
 * @return
 * @throws InvalidPasswordException
 * @throws IOException
 */
PDTilingPattern getTilingPattern(byte[] pdfDocument) throws InvalidPasswordException, IOException {
    // load the document using PDFBOX (iText is no good for parsing tiling patterns, mostly works
    // well for text and image extraction, spent a few hours trying to use it with no results)
    PDDocument doc = PDDocument.load(pdfDocument);
    PDPage page = doc.getPage(0);

    // use a graphics stream engine, it's the only thing I could find that parses the PDF
    // deep enough to allow catching the tiling pattern in parsed form 
    AtomicReference<PDTilingPattern> pattern = new AtomicReference<>();
    PDFStreamEngine engine = new PDFGraphicsStreamEngine(page) {

        @Override
        public void strokePath() throws IOException {
        }

        @Override
        public void shadingFill(COSName shadingName) throws IOException {
        }

        @Override
        public void moveTo(float x, float y) throws IOException {
        }

        @Override
        public void lineTo(float x, float y) throws IOException {
        }

        @Override
        public Point2D getCurrentPoint() throws IOException {
            return null;
        }

        @Override
        public void fillPath(int windingRule) throws IOException {
        }

        @Override
        public void fillAndStrokePath(int windingRule) throws IOException {
        }

        @Override
        public void endPath() throws IOException {
        }

        @Override
        public void drawImage(PDImage pdImage) throws IOException {
        }

        @Override
        public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException {
        }

        @Override
        public void closePath() throws IOException {
        }

        @Override
        public void clip(int windingRule) throws IOException {
        }

        @Override
        public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException {
        }
    };

    // setup the tiling pattern trap
    engine.addOperator(new SetNonStrokingColorN() {

        @Override
        public void process(Operator operator, List<COSBase> arguments) throws IOException {
            super.process(operator, arguments);

            PDColor color = context.getGraphicsState().getNonStrokingColor();
            if (context.getGraphicsState().getNonStrokingColorSpace() instanceof PDPattern) {
                PDPattern colorSpace = (PDPattern) context.getGraphicsState().getNonStrokingColorSpace();
                PDAbstractPattern ap = colorSpace.getPattern(color);
                if (ap instanceof PDTilingPattern) {
                    pattern.set((PDTilingPattern) ap);
                }
            }
        }
    });
    // run it
    engine.processPage(page);

    return pattern.get();
}

From source file:org.pdfsam.pdfbox.component.PdfRotatorTest.java

License:Open Source License

@Test
public void singlePage() {
    PDDocument document = mock(PDDocument.class);
    PDPage page = mock(PDPage.class);
    when(page.getRotation()).thenReturn(180);
    when(document.getPage(2)).thenReturn(page);
    applyRotation(Rotation.DEGREES_270, Collections.singleton(3)).to(document);
    verify(page).setRotation(90);/*from  www . j a  va 2s.c om*/
}

From source file:org.pdfsam.pdfbox.component.PdfRotatorTest.java

License:Open Source License

@Test
public void multiplePages() {
    PDDocument document = mock(PDDocument.class);
    PDPage page1 = mock(PDPage.class);
    when(page1.getRotation()).thenReturn(180);
    when(document.getPage(0)).thenReturn(page1);
    PDPage page2 = mock(PDPage.class);
    when(page2.getRotation()).thenReturn(90);
    when(document.getPage(1)).thenReturn(page2);
    when(document.getNumberOfPages()).thenReturn(2);
    applyRotation(Rotation.DEGREES_270, new HashSet<>(Arrays.asList(1, 2))).to(document);
    verify(page1).setRotation(90);//from   w ww. java  2  s.c o m
    verify(page2).setRotation(0);
}

From source file:org.pennyledger.docstore.parser.impl.PDFImageExtractor.java

License:Apache License

/**
 * Entry point for the application.//from   w  w  w. j a  v a  2 s  .c  o m
 *
 * @param args
 *          The command-line arguments.
 * @throws IOException
 *           if there is an error reading the file or extracting the images.
 */
//  public static void main(String[] args) throws IOException {
//    // suppress the Dock icon on OS X
//    System.setProperty("apple.awt.UIElement", "true");
//
//    IImageParser imageParser = new TesseractImageOCR();
//    PDFImageExtractor extractor = new PDFImageExtractor(imageParser, 150);
//    extractor.run(args);
//  }

//  private void run(String[] args) throws IOException {
//    //String pdfFile = args[0];
//    String pdfFile = "c:/PennyLedger/1d51-9fe1b211e8039458b2ac4dbbfbf1.pdf";
//    if (pdfFile.length() <= 4) {
//      throw new IllegalArgumentException("Invalid file name: not PDF");
//    }
//    String password = "";
//    Path pdfPath = Paths.get(pdfFile);
//    PDDocument document = PDDocument.load(pdfPath.toFile(), password);
//    IDocumentContents docContents = new DocumentContents();
//    extract(document, id, pdfPath, docContents);
//  }

IDocumentContents extract(PDDocument document, String id, IDocumentContents docContents) throws IOException {
    AccessPermission ap = document.getCurrentAccessPermission();
    if (!ap.canExtractContent()) {
        throw new IOException("You do not have permission to extract images");
    }

    for (int i = 0; i < document.getNumberOfPages(); i++) {
        PDPage page = document.getPage(i);
        ImageGraphicsEngine extractor = new ImageGraphicsEngine(page, i, id);
        extractor.run();
        IDocumentContents pageContents = extractor.getPageContents();
        docContents = docContents.merge(pageContents);
    }
    return docContents;
}

From source file:org.springframework.restdocs.asciidoctor.OperationBlockMacroTests.java

License:Apache License

private List<String> extractStrings(File pdfFile) throws IOException {
    PDDocument pdf = PDDocument.load(pdfFile);
    assertThat(pdf.getNumberOfPages()).isEqualTo(1);
    StringExtractor stringExtractor = new StringExtractor();
    stringExtractor.processPage(pdf.getPage(0));
    return stringExtractor.getStrings();
}

From source file:pdf.DailyReportPDF.java

public String createDailyReport() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertProducts();
    this.dailyReportInfo();
    contentStream.close();//from   w  ww .  jav  a2 s .c o  m
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\daily_reports\\" + "daily_report_" + dateOfDailyReport.getTime() + ".pdf";
    document.save(path);
    //createDailyReportPNG(path);
    document.close();

    return path;
}

From source file:pdf.NormativPDF.java

public String createNormativ() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertSupplies();
    this.insertProduct();
    this.normativInfo();
    contentStream.close();//w ww .j  a v a 2 s  . c  o m
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\norms\\" + "norm" + "_" + dateNormativ.getTime() + ".pdf";
    document.save(path);
    //createNormativPNG(path);
    document.close();
    return path;

}

From source file:pdf.StockItemsPDF.java

public String createStockItems() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertProducts();
    this.stockItemsInfo();
    contentStream.close();//from  ww  w. j a v a2  s .co  m
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\stock_items\\" + "stock_item_" + dateOfWriteOff.getTime() + ".pdf";
    document.save(path);
    //createStockItemsPNG(path);
    document.close();

    return path;
}

From source file:pdf.SuppliesListPDF.java

public String createSuppliesList() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertSupplies();
    this.suppliesListInfo();
    contentStream.close();/*w  ww  .j av  a2 s  . c  o m*/
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\lager_lists\\" + "supplies_list_" + dateSuppliesList.getTime() + ".pdf";
    document.save(path);
    //createDailyReportPNG(path);
    document.close();
    return path;

}

From source file:pdf.WorkOrderPDF.java

public void createWorkOrder() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertProducts();
    this.insertSupplies();
    this.workOrderInfo();

    document.save("pdf_docs\\work_orders\\" + "work_order_" + numOfWorkOrder.split("\\/")[0] + "_"
            + numOfWorkOrder.split("/")[1] + ".pdf");
    document.close();/*  w  w w .java2 s.c  o  m*/

}