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:pdf.WriteOffMaterialsPDF.java

public void createWriteOffMaterials() 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.writeOffMaterialsInfo();
    contentStream.close();/*  www.j  a v  a 2s. c  o  m*/
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\write_off_materials\\" + "write_off_materials-" + sdf.format(dateOfWriteOff)
            + ".pdf";
    document.save(path);
    createWriteOffMaterialsPNG(path);
    document.close();

}

From source file:pdf.WriteOffProductsPDF.java

public void createWriteOffProducts() 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.writeOffProductsInfo();
    contentStream.close();/*from ww  w  .j  a  v a 2  s . c  om*/
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\write_off_products\\" + "write_off_products-" + sdf.format(dateOfWriteOff)
            + ".pdf";
    document.save(path);
    createWriteOffProductsPNG(path);
    document.close();

}

From source file:pdfconverter.converter3.java

@SuppressWarnings("deprecation")

public static void main(String[] args) throws IOException, WriteException {
    workbook = Workbook.createWorkbook(new File(output));
    System.out.println("File created");
    WritableSheet sheet = workbook.createSheet("Page", 0);
    ExcelStart(sheet);/*from   w  w  w  .ja va  2s.c  o  m*/

    //Scanner user_input = new Scanner( System.in );
    File dir = new File(path);
    //System.out.println(dir.getPath());
    File[] dirList = dir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".pdf");
        }
    });

    int counter = 1;
    PDDocument pd;
    PDFTextStripper stripper = new PDFTextStripper();
    PDFTextStripperByArea areaSearch = new PDFTextStripperByArea();
    PDFTextStripperByArea stripper2 = new PDFTextStripperByArea();
    PDFTextStripperByArea stripper3 = new PDFTextStripperByArea();
    //PDRectangle rect = new PDRectangle(0, 0, 100, 100);
    stripper.setStartPage(1); //Start extracting from page 3
    stripper.setEndPage(1); //Extract till page 5
    File f = new File(dirList[0].getPath());

    pd = PDDocument.load(f);
    //int curHeight = 136;
    //int rowCount = 37;
    int curHeight = 116;
    int rowCount = 39;
    int rowHeight = 9;
    int sheetRowCount = 0;
    int pageStop = 1491;

    for (int curpage = 800; curpage < pageStop; curpage++) {
        if (counter > 800) {
            break;
        }
        PDPage page = pd.getPage(curpage);

        System.out.println("Now parsing page " + curpage);
        for (int curRow = 0; curRow < 80; curRow++) {
            Rectangle2D.Float cell = new Rectangle2D.Float(0, curHeight, 80, rowHeight);
            String name = "cell-1-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            String text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 0, sheetRowCount + 1);

            cell = new Rectangle2D.Float(80, curHeight, 30, rowHeight);
            name = "cell-2-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 1, sheetRowCount + 1);

            cell = new Rectangle2D.Float(110, curHeight, 40, rowHeight);
            name = "cell-3-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 2, sheetRowCount + 1);

            cell = new Rectangle2D.Float(150, curHeight, 120, rowHeight);
            name = "cell-4-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 3, sheetRowCount + 1);

            cell = new Rectangle2D.Float(270, curHeight, 120, rowHeight);
            name = "cell-5-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 4, sheetRowCount + 1);

            cell = new Rectangle2D.Float(390, curHeight, 40, rowHeight);
            name = "cell-6-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 5, sheetRowCount + 1);

            cell = new Rectangle2D.Float(430, curHeight, 46, rowHeight);
            name = "cell-7-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 6, sheetRowCount + 1);

            cell = new Rectangle2D.Float(476, curHeight, 82, rowHeight);
            name = "cell-8-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 7, sheetRowCount + 1);

            cell = new Rectangle2D.Float(558, curHeight, 65, rowHeight);
            name = "cell-9-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 8, sheetRowCount + 1);

            cell = new Rectangle2D.Float(623, curHeight, 66, rowHeight);
            name = "cell-10-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 9, sheetRowCount + 1);

            cell = new Rectangle2D.Float(689, curHeight, 100, rowHeight);
            name = "cell-11-" + curRow;
            areaSearch.addRegion(name, cell);
            areaSearch.extractRegions(page);
            text = areaSearch.getTextForRegion(name);
            areaSearch.removeRegion(name);
            AddCell(sheet, text, 10, sheetRowCount + 1);

            sheetRowCount++;
            curHeight += rowHeight;
        }

        //Rectangle2D.Float issueDate = new Rectangle2D.Float(0, 0, 80, page.getMediaBox().getHeight());
        //stripper2.addRegion("issueDate", issueDate);
        //Rectangle2D.Float amount = new Rectangle2D.Float(80, 0, 30, page.getMediaBox().getHeight());
        //stripper2.addRegion("amount", amount);
        //Rectangle2D.Float citation = new Rectangle2D.Float(110, 0, 40, page.getMediaBox().getHeight());
        //stripper2.addRegion("citation", citation);
        //Rectangle2D.Float violation = new Rectangle2D.Float(150, 0, 120, page.getMediaBox().getHeight());
        //stripper2.addRegion("violation", violation);
        //Rectangle2D.Float comment = new Rectangle2D.Float(270, 0, 120, page.getMediaBox().getHeight());
        //stripper2.addRegion("comment", comment);
        //Rectangle2D.Float warning = new Rectangle2D.Float(390, 0, 40, page.getMediaBox().getHeight());
        //stripper2.addRegion("warning", warning);
        //Rectangle2D.Float license = new Rectangle2D.Float(430, 0, 46, page.getMediaBox().getHeight());
        //stripper2.addRegion("license", license);
        //Rectangle2D.Float lot = new Rectangle2D.Float(476, 0, 82, page.getMediaBox().getHeight());
        //stripper2.addRegion("lot", lot);
        //Rectangle2D.Float make = new Rectangle2D.Float(558, 0, 65, page.getMediaBox().getHeight());
        //stripper2.addRegion("make", make);
        //Rectangle2D.Float officer = new Rectangle2D.Float(623, 0, 66, page.getMediaBox().getHeight());
        //stripper2.addRegion("officer", officer);
        //Rectangle2D.Float state = new Rectangle2D.Float(689, 0, 100, page.getMediaBox().getHeight());
        //stripper2.addRegion("state", state);

        //stripper2.extractRegions(page);
        //String text = stripper2.getTextForRegion("license");

        //Rectangle2D.Float row = new Rectangle2D.Float(0, 156, 80, 10);
        //stripper3.addRegion("row", row);
        //stripper3.extractRegions(page);
        //String text = stripper3.getTextForRegion("row");
        //System.out.println(text);
        counter++;
        curHeight = 116;
        rowCount = 39;
    }
    //AddRow(sheet, text, counter);
    //counter++;
    pd.close();

    System.out.println("Data extracted to Excel, parsing through Excel data...");

    boolean multiline = true;
    while (multiline) {
        multiline = false;
        for (int row = 0; row < sheet.getRows(); row++) {
            Cell cell = sheet.getCell(0, row);
            if (cell.getContents().length() < 5) {
                multiline = true;
                WritableCell cell2 = sheet.getWritableCell(4, row - 1);
                WritableCell cell3 = sheet.getWritableCell(4, row);
                String content = cell2.getContents() + cell3.getContents();
                content = content.replace("\n", "").replace("\r", "");
                Label l = (Label) cell2;
                l.setString(content);
                sheet.removeRow(row);
            }
        }
    }

    System.out.println("Data extraction complete");
    workbook.write();
    workbook.close();
}

From source file:pdfsplicer.SplicerModel.java

License:Open Source License

/**
 * Create the new PDF, and save it./* w  ww .ja v  a 2 s. co  m*/
 * 
 * @param saveFile the file to save it as
 * @throws IOException if it cannot save the file
 */
public void makeFinalizedPDF(File saveFile) throws IOException {

    PDDocument doc = null;
    PDDocument newdoc = new PDDocument();

    for (int i = 0; i < pageEntryPDFList.size(); ++i) {
        doc = pdfList.get(pageEntryPDFList.get(i));

        if (doc.isEncrypted()) {
            System.out.println("Error: Encrypted PDF");
            System.exit(1);
        }

        List<Integer> pRange = pageRangeList.get(i);
        PDFCloneUtility pdfCloner = new PDFCloneUtility(newdoc);
        for (int pNum : pRange) {
            PDPage page = doc.getPage(pNum - 1);
            COSDictionary clonedDict = (COSDictionary) pdfCloner.cloneForNewDocument(page);
            newdoc.addPage(new PDPage(clonedDict));
        }
    }

    newdoc.save(saveFile);
    if (newdoc != null) {
        newdoc.close();
    }
}

From source file:src.controller.PageController.java

/**
 * Extrait la page spcifie du document et la sauvegade dans le fichier donn
 * @param document/*www .ja  v  a2 s  .c o m*/
 * @param id
 * @param filename 
 */
public void extractPage(PDDocument document, int id, String filename) {
    try {
        PDDocument doc = new PDDocument();
        if (document.getPage(id) != null) {
            doc.addPage(document.getPage(id));
        }
        doc.save(filename + ".pdf");
        doc.close();
        System.out.println("La page " + id + " a bien t extraite.");
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

From source file:src.controller.PageController.java

/**
 * Tourne la page spcifie//  www  . j a  v  a 2s  .c o  m
 * @param document
 * @param id
 * @param degree 
 */
public void rotatePage(PDDocument document, int id, int degree) {
    PDPage page = document.getPage(id);
    page.setRotation(page.getRotation() + degree);
}

From source file:src.view.controller.MainController.java

public void testAddImage() {
    try {/*from   w  w  w .  j  a  v a 2  s  . c  o  m*/
        ImageController imageController = new ImageController();

        // Chargement du document
        File file = new File(TEST_DOC_TITLE + ".pdf");
        PDDocument document = PDDocument.load(file);

        document.addPage(new PDPage());

        PDPageContentStream contentStream = new PDPageContentStream(document,
                document.getPage(document.getNumberOfPages() - 1), PDPageContentStream.AppendMode.APPEND, true);

        // Ajout d'une image sur la deuxime page
        imageController.addImage(document, contentStream, TEST_IMG_NAME, 100, 400, 0.2f);

        contentStream.close();

        document.save(TEST_DOC_TITLE + ".pdf");
        document.close();

        System.out.println(TRANSLATOR.getString("TEST_IMAGE_ADDED"));
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:src.view.controller.MainController.java

public void testAddTable() {
    try {//from  w  ww. j a  v  a 2 s. c  om
        TableController tableController = new TableController();

        // Chargement du document
        File file = new File(TEST_DOC_TITLE + ".pdf");
        PDDocument document = PDDocument.load(file);

        document.addPage(new PDPage());

        PDPageContentStream contentStream = new PDPageContentStream(document,
                document.getPage(document.getNumberOfPages() - 1), PDPageContentStream.AppendMode.APPEND, true);

        // Instanciation d'un tableau
        Table table = new Table(100, 600, 200, 200);
        // Gnration du tableau
        table.generateTable(2, 7);
        table.addColumns(2);

        // Rcupration d'une cellule
        Cell cell = table.getCell(0);
        // Initialisation du contenu
        cell.setContent("TEST");
        // Affichage du contenu dans la cellule
        tableController.printCellContent(contentStream, cell, "center", "middle");

        // Ajout d'un tableau
        tableController.printTable(contentStream, table, false);

        contentStream.close();
        document.save(TEST_DOC_TITLE + ".pdf");

        System.out.println(TRANSLATOR.getString("TEST_TABLE_ADDED"));
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}