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

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

Introduction

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

Prototype

public void addPage(PDPage page) 

Source Link

Document

This will add a page to the document.

Usage

From source file:Reports.LeaveDataReader.java

public void singlePdfGenerator(String pdfFileNameBase, Set<String> inputSources)
        throws IOException, COSVisitorException {
    GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name                          
    int rowCount = 0;
    int pageCount = 1;
    System.out.println("passed in " + inputSources);
    PDDocument pdf = new PDDocument();
    PDPage page; //default size PAGE_SIZE_A4
    PDPageContentStream stream;//  ww  w  . j  a va2 s. c o m
    //page.setRotation(90); //counterclock wise rotate 90 degree  ////left hand rule

    //        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
    String lastUpdate = null;
    String lastInputSource = null;
    System.out.println("LEAVE_DATA keyset:" + LEAVE_DATA.keySet());
    //        System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size());
    //        System.out.println(cycles);

    String pdfFileName = pdfFileNameBase + "\\LEAVE UPDT";
    for (String updateDate : LEAVE_DATA.keySet()) {
        Map<String, List<String>> thisDTData = LEAVE_DATA.get(updateDate);
        lastUpdate = updateDate;
        System.out.println("thisDT_DATA keyset:" + thisDTData.keySet());
        pdfFileName = pdfFileName + " " + updateDate;
        for (String inputSource : thisDTData.keySet()) {
            if (inputSources.isEmpty() || inputSources.contains(inputSource)) {

                lastInputSource = inputSource;
                List<String> thisCycle = thisDTData.get(inputSource);
                pageCount = 1; // new cycle, restart page num
                page = new PDPage(); //page break
                stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                page.setRotation(PAGE_ROT_DEGREE);
                //stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                addBigFontUpdateNumberAndCycle(updateDate, lastInputSource, stream);
                stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                // stream.setFont(PDType1Font.
                int thisCycleRowCount = 0;
                for (String row : thisCycle) {
                    if (thisCycleRowCount > MAX_NUM_TRANS) {
                        //close the current page
                        setupFootNote(stream, pageCount, lastInputSource, updateDate);
                        pageCount++;
                        stream.endText();
                        stream.close();
                        pdf.addPage(page);

                        // start a new page
                        page = new PDPage();
                        stream = new PDPageContentStream(pdf, page, true, false);
                        page.setRotation(PAGE_ROT_DEGREE);
                        stream.beginText();
                        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                        thisCycleRowCount = 0;
                    }
                    stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y);
                    stream.drawString(row);
                    thisCycleRowCount++;
                    //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row);
                }
                setupFootNote(stream, pageCount, lastInputSource, lastUpdate);
                stream.endText();
                stream.close();
                pdf.addPage(page);
            }
        }
    }
    if (pdf.getNumberOfPages() > 0) {
        pdfFileName = pdfFileName + ".pdf";
        pdf.save(pdfFileName);
    }
    pdf.close();
}

From source file:Reports.MgmtNoticesDataReader.java

public void multiPdfGenerator(String pdfFileNameBase, List<String> cycles)
        throws IOException, COSVisitorException {
    GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name                          
    int rowCount = 0;
    int pageCount = 1;
    PDPage page; //default size PAGE_SIZE_A4
    PDPageContentStream stream;// w ww.ja v  a2 s  .  c  o  m
    //page.setRotation(90); //counterclock wise rotate 90 degree  ////left hand rule

    //        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
    String lastUpdate = null;
    String lastCycle = null;
    //        System.out.println("DROT_DATA keyset:" + MGNT_DATA.keySet());
    //        System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size());
    //        System.out.println(cycles);
    for (String updateDate : MGNT_DATA.keySet()) {
        Map<String, List<String>> thisDTData = MGNT_DATA.get(updateDate);
        lastUpdate = updateDate;
        System.out.println("thisDT_DATA keyset:" + thisDTData.keySet());
        for (String cycle : thisDTData.keySet()) {
            if (cycles.isEmpty() || cycles.contains(cycle)) {
                PDDocument pdf = new PDDocument();
                lastCycle = cycle;
                List<String> thisCycle = thisDTData.get(cycle);
                pageCount = 1; // new cycle, restart page num
                page = new PDPage(); //page break
                stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                page.setRotation(PAGE_ROT_DEGREE);
                //stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                addBigFontUpdateNumberAndCycle(updateDate, cycle, stream);
                stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                // stream.setFont(PDType1Font.
                int thisCycleRowCount = 0;
                for (String row : thisCycle) {
                    if (thisCycleRowCount > MAX_NUM_TRANS) {
                        //close the current page
                        setupFootNote(stream, pageCount, cycle, updateDate);
                        pageCount++;
                        stream.endText();
                        stream.close();
                        pdf.addPage(page);

                        // start a new page
                        page = new PDPage();
                        stream = new PDPageContentStream(pdf, page, true, false);
                        page.setRotation(PAGE_ROT_DEGREE);
                        stream.beginText();
                        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                        thisCycleRowCount = 0;
                    }
                    stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y);
                    stream.drawString(row);
                    thisCycleRowCount++;
                    //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row);
                }
                setupFootNote(stream, pageCount, lastCycle, lastUpdate);
                stream.endText();
                stream.close();
                pdf.addPage(page);
                String pdfFileName = pdfFileNameBase + "\\MGMT NTC UPDT " + updateDate + " " + cycle + ".pdf";

                pdf.save(pdfFileName);
                pdf.close();
            }
        }
    }

    // pdf.save(pdfFileName);        
    // pdf.close();      
}

From source file:Reports.MgmtNoticesDataReader.java

public void singlePdfGenerator(String pdfFileNameBase, List<String> cycles)
        throws IOException, COSVisitorException {
    GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name                          
    int rowCount = 0;
    int pageCount = 1;
    PDDocument pdf = new PDDocument();
    PDPage page; //default size PAGE_SIZE_A4
    PDPageContentStream stream;/* ww  w  .  jav a2 s. c o  m*/
    //page.setRotation(90); //counterclock wise rotate 90 degree  ////left hand rule

    //        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
    String lastUpdate = null;
    String lastTrans = null;
    System.out.println("MGNT_DATA keyset:" + MGNT_DATA.keySet());
    //        System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size());
    //        System.out.println(cycles);
    String pdfFileName = pdfFileNameBase + "\\MGMT NTC UPDT";
    for (String updateDate : MGNT_DATA.keySet()) {
        Map<String, List<String>> thisDTData = MGNT_DATA.get(updateDate);
        lastUpdate = updateDate;
        System.out.println("thisDT_DATA keyset:" + thisDTData.keySet());
        pdfFileName = pdfFileName + " " + updateDate;
        for (String trans : thisDTData.keySet()) {
            if (cycles.isEmpty() || cycles.contains(trans)) {
                //PDDocument pdf = new PDDocument();    
                lastTrans = trans;
                List<String> thisCycle = thisDTData.get(trans);
                pageCount = 1; // new cycle, restart page num
                page = new PDPage(); //page break
                stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                page.setRotation(PAGE_ROT_DEGREE);
                //stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                addBigFontUpdateNumberAndCycle(updateDate, trans, stream);
                stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                // stream.setFont(PDType1Font.
                int thisCycleRowCount = 0;
                for (String row : thisCycle) {
                    if (thisCycleRowCount > MAX_NUM_TRANS) {
                        //close the current page
                        setupFootNote(stream, pageCount, trans, updateDate);
                        pageCount++;
                        stream.endText();
                        stream.close();
                        pdf.addPage(page);

                        // start a new page
                        page = new PDPage();
                        stream = new PDPageContentStream(pdf, page, true, false);
                        page.setRotation(PAGE_ROT_DEGREE);
                        stream.beginText();
                        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                        thisCycleRowCount = 0;
                    }
                    stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y);
                    stream.drawString(row);
                    thisCycleRowCount++;
                }
                setupFootNote(stream, pageCount, lastTrans, lastUpdate);
                stream.endText();
                stream.close();
                pdf.addPage(page);

            }
        }
    }
    if (pdf.getNumberOfPages() > 0) {
        pdfFileName = pdfFileName + ".pdf";
        pdf.save(pdfFileName);
    }
    pdf.close();
}

From source file:Reports.RecycleDataReader.java

public void PdfGenerator(String pdfFileNameBase, List<String> cycles) throws IOException, COSVisitorException {
    GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name                          
    int rowCount = 0;
    int pageCount = 1;
    PDPage page; //default size PAGE_SIZE_A4
    PDPageContentStream stream;//from  ww w  .  j ava  2s  .c  o  m
    //page.setRotation(90); //counterclock wise rotate 90 degree  ////left hand rule

    //        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
    String lastUpdate = null;
    String lastCycle = null;
    System.out.println("RECYCLE_DATA keyset:" + RECYCLE_DATA.keySet());
    System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size());
    System.out.println(cycles);
    for (String updateDate : RECYCLE_DATA.keySet()) {
        Map<String, List<String>> thisDTData = RECYCLE_DATA.get(updateDate);
        lastUpdate = updateDate;
        System.out.println("thisDT_DATA keyset:" + thisDTData.keySet());
        for (String cycle : thisDTData.keySet()) {
            if (cycles.isEmpty() || cycles.contains(cycle)) {
                PDDocument pdf = new PDDocument();
                lastCycle = cycle;
                List<String> thisCycle = thisDTData.get(cycle);
                pageCount = 1; // new cycle, restart page num
                page = new PDPage(); //page break
                stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                page.setRotation(PAGE_ROT_DEGREE);
                //stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                addBigFontUpdateNumberAndCycle(updateDate, cycle, stream);
                stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                // stream.setFont(PDType1Font.
                int thisCycleRowCount = 0;
                for (String row : thisCycle) {
                    if (thisCycleRowCount > MAX_NUM_TRANS) {
                        //close the current page
                        setupFootNote(stream, pageCount, cycle, updateDate);
                        pageCount++;
                        stream.endText();
                        stream.close();
                        pdf.addPage(page);

                        // start a new page
                        page = new PDPage();
                        stream = new PDPageContentStream(pdf, page, true, false);
                        page.setRotation(PAGE_ROT_DEGREE);
                        stream.beginText();
                        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                        thisCycleRowCount = 0;
                    }
                    stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y);
                    stream.drawString(row);
                    thisCycleRowCount++;
                    //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row);
                }
                setupFootNote(stream, pageCount, lastCycle, lastUpdate);
                stream.endText();
                stream.close();
                pdf.addPage(page);
                String pdfFileName = pdfFileNameBase + "\\RECYCLE UPDT " + updateDate + " " + cycle + ".pdf";

                pdf.save(pdfFileName);
                pdf.close();
            }
        }
    }

    // pdf.save(pdfFileName);        
    // pdf.close();      
}

From source file:Reports.RejectDataReader.java

public void PdfGenerator(String pdfFileNameBase, List<String> cycles) throws IOException, COSVisitorException {
    GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name                          
    int rowCount = 0;
    int pageCount = 1;
    PDPage page; //default size PAGE_SIZE_A4
    PDPageContentStream stream;/* w w w  . j a v a  2 s  .  co m*/
    //page.setRotation(90); //counterclock wise rotate 90 degree  ////left hand rule

    //        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
    String lastUpdate = null;
    String lastCycle = null;
    System.out.println("REJECT_DATA keyset:" + REJECT_DATA.keySet());
    System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size());
    System.out.println(cycles);
    for (String updateDate : REJECT_DATA.keySet()) {
        Map<String, List<String>> thisDTData = REJECT_DATA.get(updateDate);
        lastUpdate = updateDate;
        System.out.println("thisDT_DATA keyset:" + thisDTData.keySet());
        for (String cycle : thisDTData.keySet()) {
            if (cycles.isEmpty() || cycles.contains(cycle)) {
                PDDocument pdf = new PDDocument();
                lastCycle = cycle;
                List<String> thisCycle = thisDTData.get(cycle);
                pageCount = 1; // new cycle, restart page num
                page = new PDPage(); //page break
                stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                page.setRotation(PAGE_ROT_DEGREE);
                //stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                addBigFontUpdateNumberAndCycle(updateDate, cycle, stream);
                stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                // stream.setFont(PDType1Font.
                int thisCycleRowCount = 0;
                for (String row : thisCycle) {
                    if (thisCycleRowCount > MAX_NUM_TRANS) {
                        //close the current page
                        setupFootNote(stream, pageCount, cycle, updateDate);
                        pageCount++;
                        stream.endText();
                        stream.close();
                        pdf.addPage(page);

                        // start a new page
                        page = new PDPage();
                        stream = new PDPageContentStream(pdf, page, true, false);
                        page.setRotation(PAGE_ROT_DEGREE);
                        stream.beginText();
                        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                        thisCycleRowCount = 0;
                    }
                    stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y);
                    stream.drawString(row);
                    thisCycleRowCount++;
                    //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row);
                }
                setupFootNote(stream, pageCount, lastCycle, lastUpdate);
                stream.endText();
                stream.close();
                pdf.addPage(page);
                String pdfFileName = pdfFileNameBase + "\\REJECT UPDT " + updateDate + " " + cycle + ".pdf";

                pdf.save(pdfFileName);
                pdf.close();
            }
        }
    }

    // pdf.save(pdfFileName);        
    // pdf.close();      
}

From source file:richtercloud.document.scanner.gui.DefaultMainPanel.java

License:Open Source License

@Override
public void exportActiveDocumentItem(OutputStream out, int exportFormat)
        throws IOException, ImageWrapperException {
    if (exportFormat == EXPORT_FORMAT_PDF) {
        //There seems to be no PNG support in Apache PDFBox, but
        //transforming into JPEG isn't too much of an effort and allows to
        //limit dependencies to Apache PDFBox
        PDDocument document = new PDDocument();
        PDRectangle documentRectangle = PDRectangle.A4;
        for (OCRSelectPanel oCRSelectPanel : oCRSelectComponent.getoCRSelectPanelPanel().getoCRSelectPanels()) {
            ImageWrapper imageWrapper = oCRSelectPanel.getImage();
            PDPage page = new PDPage(documentRectangle);
            document.addPage(page);
            //@TODO: figure out how to create PDImageXObject from stream
            //since this was possible in 1.8 and it's unlikely that there's
            //such a severe regression
            InputStream inputStream = imageWrapper.getOriginalImageStream();
            if (inputStream == null) {
                //cache has been shut down
                return;
            }//from  www . j  a  v  a 2s .  com
            BufferedImage awtImage = ImageIO.read(inputStream);
            PDImageXObject pdImageXObject = LosslessFactory.createFromImage(document, awtImage);
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.drawImage(pdImageXObject, 0, 0, documentRectangle.getWidth(),
                    documentRectangle.getHeight());
            //in case width and height exceed the size of
            //documentRectangle, the page is empty (or the content might
            //be placed outside the page which has the same effect)
            contentStream.setFont(PDType1Font.COURIER, 10);
            contentStream.close();
        }
        document.save(out);
        document.close();
        out.flush();
        out.close();
    } else {
        throw new IllegalArgumentException("export format %s isn't supported");
    }
}

From source file:serock.pdfpagerestorer.PdfPageRestorer.java

License:Open Source License

private static void addPages(final PDDocument pdDoc, final COSDocument cosDoc) throws IOException {
    final List<COSObject> pageObjects = cosDoc.getObjectsByType(COSName.PAGE);
    for (COSObject pageObject : pageObjects) {
        final COSBase baseObject = pageObject.getObject();
        final COSDictionary pageDictionary = (COSDictionary) baseObject;
        final PDPage page = new PDPage(pageDictionary);
        pdDoc.addPage(page);
    }/*from   ww w.  j a  v a 2 s . c o m*/
}

From source file:so.rezervacija.StampajRezervaciju.java

@Override
protected void izvrsiKonkretnuOperaciju() throws Exception {
    PDDocument doc = null;
    PDPage page = null;//from  ww w . j a va  2s  . c  o  m

    try {
        doc = new PDDocument();
        page = new PDPage();

        doc.addPage(page);
        PDFont pdfFont = PDType1Font.HELVETICA_BOLD;
        float fontSize = 25;
        float leading = 1.5f * fontSize;

        PDPageContentStream contentStream = new PDPageContentStream(doc, page);

        PDRectangle mediabox = page.findMediaBox();
        float margin = 72;
        float width = mediabox.getWidth() - 2 * margin;
        float startX = mediabox.getLowerLeftX() + margin;
        float startY = mediabox.getUpperRightY() - margin;

        String text = "Izvrsili ste rezervaciju za tretman " + r.getTretman() + ", vreme rezervacije:"
                + new SimpleDateFormat("YYYY-MM-dd HH:mm").format(r.getVreme())
                + " zaposleni koji ce vrsiti tretman:" + r.getZaposleni().getImePrezime();
        List<String> lines = new ArrayList<String>();
        int lastSpace = -1;
        while (text.length() > 0) {
            int spaceIndex = text.indexOf(' ', lastSpace + 1);
            if (spaceIndex < 0) {
                lines.add(text);
                text = "";
            } else {
                String subString = text.substring(0, spaceIndex);
                float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
                if (size > width) {
                    if (lastSpace < 0) // So we have a word longer than the line... draw it anyways
                    {
                        lastSpace = spaceIndex;
                    }
                    subString = text.substring(0, lastSpace);
                    lines.add(subString);
                    text = text.substring(lastSpace).trim();
                    lastSpace = -1;
                } else {
                    lastSpace = spaceIndex;
                }
            }
        }

        contentStream.beginText();
        contentStream.setFont(pdfFont, fontSize);
        contentStream.moveTextPositionByAmount(startX, startY);
        for (String line : lines) {
            contentStream.drawString(line);
            contentStream.moveTextPositionByAmount(0, -leading);
        }
        contentStream.endText();
        contentStream.close();

        doc.save("PotvrdaRezervacije.pdf");

        if (Desktop.isDesktopSupported()) {
            try {
                File myFile = new File("../ServerProjekat/PotvrdaRezervacije.pdf");
                Desktop.getDesktop().open(myFile);
            } catch (IOException ex) {
                // no application registered for PDFs
            }
        }

        doc.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:src.controller.PageController.java

/**
 * Extrait la page spcifie du document et la sauvegade dans le fichier donn
 * @param document//from   w ww  .j a va2s. co  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.view.controller.MainController.java

public void testAddImage() {
    try {// w  w w  . ja  va 2s.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());
    }
}