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

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

Introduction

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

Prototype

public PDDocument() 

Source Link

Document

Creates an empty PDF document.

Usage

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;//w  w  w.ja v a 2s . co  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  w w w  . java 2s  . 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("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;/*from  w ww. j  a v a  2s  .c  om*/
    //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:ResumeBuilder.PDFTest.java

public void createPdf(String name) throws IOException {
    try (PDDocument document = new PDDocument()) {
        PDPage blankPage = new PDPage();
        document.addPage(blankPage);/* ww w . j av  a  2s .c  o m*/
        document.save(name);
    }
    Document doc = new Document();
}

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);/*from ww w .ja v a 2  s  .  co m*/
            //@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;
            }
            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:se.mithlond.services.content.impl.ejb.report.PdfReportServiceBean.java

License:Apache License

/**
 * {@inheritDoc}/*from   w w w.j ava2s  .  com*/
 */
@Override
public PDDocument createDocument(@NotNull final Membership activeMembership, @NotNull final String title) {

    // Check sanity
    Validate.notNull(activeMembership, "activeMembership");
    Validate.notEmpty(title, "title");

    // Create the document and add some metadata to it.
    final PDDocument toReturn = new PDDocument();
    final PDDocumentInformation pdd = toReturn.getDocumentInformation();

    pdd.setAuthor("" + activeMembership.getAlias());
    pdd.setProducer("Nazgl Services Excel Report Generator");
    pdd.setCreationDate(Calendar.getInstance());
    pdd.setTitle(title);

    // All Done.
    return toReturn;
}

From source file:se.streamsource.streamflow.web.application.pdf.PdfDocument.java

License:Apache License

public void init() {
    try {/*  w ww .ja v a2s  .  c o  m*/
        pdf = new PDDocument();

        PDPage page = new PDPage();

        pdf.addPage(page);
        page.setMediaBox(pageSize);
        maxStringLength = page.getMediaBox().getWidth() - (leftMargin + rightMargin);

        contentStream = new PDPageContentStream(pdf, page);
        contentStream.beginText();

        y = page.getMediaBox().getHeight() - headerMargin;
        contentStream.moveTextPositionByAmount(leftMargin, y);

    } catch (IOException e) {
        try {
            if (contentStream != null) {
                contentStream.close();
            }
        } catch (IOException ioe) {
            contentStream = null;
        }
        throw new IllegalStateException(e.getMessage());
    }
}

From source file:so.rezervacija.StampajRezervaciju.java

@Override
protected void izvrsiKonkretnuOperaciju() throws Exception {
    PDDocument doc = null;//ww w. j  a va2  s. c  om
    PDPage page = null;

    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 w  w . ja  v a 2 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.view.controller.MainController.java

public void testCreateFile() {
    try {/*from   w ww.j  a  v a  2  s.  c  o m*/
        PDDocument document = new PDDocument();
        document.save(TEST_DOC_TITLE + ".pdf");
        document.close();
        System.out.println(TRANSLATOR.getString("TEST_FILE_GENERATED"));
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}