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:org.dspace.disseminate.CitationDocumentServiceImpl.java

License:BSD License

@Override
public File makeCitedDocument(Context context, Bitstream bitstream)
        throws IOException, SQLException, AuthorizeException {
    PDDocument document = new PDDocument();
    PDDocument sourceDocument = new PDDocument();
    try {//  w  ww . j a  v a  2s.  c  o  m
        Item item = (Item) bitstreamService.getParentObject(context, bitstream);
        sourceDocument = sourceDocument.load(bitstreamService.retrieve(context, bitstream));
        PDPage coverPage = new PDPage(PDRectangle.LETTER); // TODO: needs to be configurable
        generateCoverPage(context, document, coverPage, item);
        addCoverPageToDocument(document, sourceDocument, coverPage);

        document.save(tempDir.getAbsolutePath() + "/bitstream.cover.pdf");
        return new File(tempDir.getAbsolutePath() + "/bitstream.cover.pdf");
    } finally {
        sourceDocument.close();
        document.close();
    }
}

From source file:org.esteco.jira.pdf.UsingTextMatrix.java

License:Apache License

/**
 * creates a sample document with some text using a text matrix.
 *
 * @param message The message to write in the file.
 * @param outfile The resulting PDF./*from  ww  w . ja  v a 2s .  c  o  m*/
 * @throws IOException If there is an error writing the data.
 */
public void doIt(String message, String outfile) throws IOException {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        // Page 1
        PDFont font = PDType1Font.HELVETICA;
        PDPage page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        float fontSize = 12.0f;

        PDRectangle pageSize = page.getMediaBox();
        float centeredXPosition = (pageSize.getWidth() - fontSize / 1000f) / 2f;
        float stringWidth = font.getStringWidth(message);
        float centeredYPosition = (pageSize.getHeight() - (stringWidth * fontSize) / 1000f) / 3f;

        PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();
        // counterclockwise rotation
        for (int i = 0; i < 8; i++) {
            contentStream.setTextMatrix(Matrix.getRotateInstance(i * Math.PI * 0.25, centeredXPosition,
                    pageSize.getHeight() - centeredYPosition));
            contentStream.showText(message + " " + i);
        }
        // clockwise rotation
        for (int i = 0; i < 8; i++) {
            contentStream.setTextMatrix(
                    Matrix.getRotateInstance(-i * Math.PI * 0.25, centeredXPosition, centeredYPosition));
            contentStream.showText(message + " " + i);
        }

        contentStream.endText();
        contentStream.close();

        // Page 2
        page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        fontSize = 1.0f;

        contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();

        // text scaling and translation
        for (int i = 0; i < 10; i++) {
            contentStream.setTextMatrix(new Matrix(12 + (i * 6), 0, 0, 12 + (i * 6), 100, 100 + i * 50));
            contentStream.showText(message + " " + i);
        }
        contentStream.endText();
        contentStream.close();

        // Page 3
        page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        fontSize = 1.0f;

        contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();

        int i = 0;
        // text scaling combined with rotation
        contentStream.setTextMatrix(new Matrix(12, 0, 0, 12, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(0, 18, -18, 0, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(-24, 0, 0, -24, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(0, -30, 30, 0, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.endText();
        contentStream.close();

        doc.save(outfile);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:org.fit.cssbox.render.PDFRenderer.java

License:Open Source License

/**
 * Creates document witch first page in it using PDFBox
 *///from  w  ww .ja va2s  . c o  m
private int createDocPDFBox() {

    try {
        doc = new PDDocument();
        page = new PDPage(pageFormat);
        doc.addPage(page);
        content = new PDPageContentStream(doc, page);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
    return 0;
}

From source file:org.gfbio.idmg.util.PDFUtil.java

public PDFUtil(DMPTInput userInput, String themePath) throws IOException {
    this.userInput = userInput;
    this.themePath = themePath;

    document = new PDDocument();
    page = createNewPage();//from   w w  w .  j a v  a2s  .c o  m

}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

public Document extract(int begin, int end) throws DocumentException {

    this.assertValidPageRange(begin, end);

    PDFDocument result = new PDFDocument();

    ByteArrayInputStream bais = null;
    ByteArrayOutputStream baos = null;

    if (content != null) {

        PDDocument document = new PDDocument();

        try {//w ww  .  j a  v a2  s  . co  m

            bais = new ByteArrayInputStream(content);
            baos = new ByteArrayOutputStream();
            PDDocument inputPDF = PDDocument.load(bais);
            while (begin <= end) {
                document.addPage((PDPage) inputPDF.getDocumentCatalog().getAllPages().get(begin - 1));
                begin++;
            }
            document.save(baos);
            document.close();
            result.load(new ByteArrayInputStream(baos.toByteArray()));

        } catch (Exception e) {
            throw new DocumentException(e);
        } finally {
            IOUtils.closeQuietly(bais);
            IOUtils.closeQuietly(baos);
        }

    }

    return result;
}

From source file:org.ghost4j.document.PDFDocument.java

License:LGPL

@Override
public void append(Document document) throws DocumentException {

    super.append(document);

    ByteArrayOutputStream baos = null;
    PDDocument mergedDocument = new PDDocument();

    try {// w w  w.  j a  va2  s.  com

        baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais = new ByteArrayInputStream(content);
        PDDocument pDocument = PDDocument.load(bais);
        int pageCount = pDocument.getNumberOfPages();
        for (int i = 0; i < pageCount; i++) {
            mergedDocument.addPage((PDPage) pDocument.getDocumentCatalog().getAllPages().get(i));
        }

        // copy new document
        ByteArrayInputStream baisNewDoc = new ByteArrayInputStream(document.getContent());
        PDDocument pNewDocument = PDDocument.load(baisNewDoc);
        pageCount = pNewDocument.getNumberOfPages();
        for (int i = 0; i < pageCount; i++) {
            mergedDocument.addPage((PDPage) pNewDocument.getDocumentCatalog().getAllPages().get(i));
        }
        mergedDocument.save(baos);
        mergedDocument.close();
        // replace content with new content
        content = baos.toByteArray();

    } catch (Exception e) {
        throw new DocumentException(e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

}

From source file:org.kay.ini.ExportPDF.java

public void createPDF(ArrayList<Connection> exportList) {

    PDFont font = PDType1Font.HELVETICA_BOLD;

    try {/*  w  ww .j  av a 2  s.  c o m*/

        PDDocument document = new PDDocument();

        PDPage page = new PDPage();

        document.addPage(page);

        PDPageContentStream content = new PDPageContentStream(document, page, true, true);
        content.beginText();
        content.setFont(font, 12);

        content.moveTextPositionByAmount(100, 700);
        for (Connection con : exportList) {
            content.drawString(con.getKey() + " = " + con.getValue());
            content.moveTextPositionByAmount(0, -15);
        }

        content.endText();
        content.close();

        document.save(saveTo);
        JOptionPane.showMessageDialog(null, "success");

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());

    }

}

From source file:org.kuali.coeus.common.impl.person.signature.PersonSignatureServiceImpl.java

License:Open Source License

/**
 * This method is to scan for signature tag in each page and apply the signature
 * at desired location./*from  ww  w .ja  v a  2s.  com*/
 * @param imageData
 * @param originalByteArrayOutputStream
 */
@SuppressWarnings("unchecked")
protected ByteArrayOutputStream scanAndApplyAutographInEachPage(byte[] imageData,
        ByteArrayOutputStream originalByteArrayOutputStream) throws Exception {
    ByteArrayOutputStream outputStream = originalByteArrayOutputStream;
    byte[] pdfFileData = originalByteArrayOutputStream.toByteArray();
    PDDocument originalDocument = getPdfDocument(pdfFileData); //PDDocument.load(is);
    PDDocument signatureDocument = new PDDocument();
    List<PDPage> originalDocumentPages = originalDocument.getDocumentCatalog().getAllPages();
    for (PDPage page : originalDocumentPages) {
        List<String> signatureTags = new ArrayList<String>(getSignatureTagParameter());
        PersonSignatureLocationHelper printer = new PersonSignatureLocationHelper(signatureTags);
        PDStream contents = page.getContents();
        if (contents != null) {
            printer.processStream(page, page.findResources(), page.getContents().getStream());
        }
        PDPage signaturePage = new PDPage();
        if (printer.isSignatureTagExists()) {
            PDJpeg signatureImage = new PDJpeg(signatureDocument, getBufferedImage(imageData));
            PDPageContentStream stream = new PDPageContentStream(signatureDocument, signaturePage, true, true);
            for (PersonSignaturePrintHelper signatureHelper : printer.getPersonSignatureLocations()) {
                float coordinateX = signatureHelper.getCoordinateX();
                float coordinateY = signatureHelper.getCoordinateY() - signatureImage.getHeight()
                        - ADDITIONAL_SPACE_BETWEEN_TAG_AND_IMAGE;
                stream.drawImage(signatureImage, coordinateX, coordinateY);
                stream.close();
            }
        } else {
            signaturePage = page;
        }
        signatureDocument.addPage(signaturePage);
    }

    Overlay overlay = new Overlay();
    overlay.overlay(signatureDocument, originalDocument);

    originalDocument.save(outputStream);
    originalDocument.close();
    signatureDocument.close();
    return outputStream;
}

From source file:org.nmrfx.processor.gui.graphicsio.PDFWriter.java

License:Open Source License

public void create(boolean landScape, double width, double height, String fileName) throws GraphicsIOException {
    // the document
    this.landScape = landScape;
    this.fileName = fileName;
    doc = new PDDocument();
    try {//from   ww w.j  a  v  a 2 s . c o m
        PDPage page = new PDPage(PDRectangle.LETTER);
        doc.addPage(page);
        PDRectangle pageSize = page.getMediaBox();
        pageWidth = pageSize.getWidth();
        pageHeight = pageSize.getHeight();
        contentStream = new PDPageContentStream(doc, page, false, false);
        // add the rotation using the current transformation matrix
        // including a translation of pageWidth to use the lower left corner as 0,0 reference
        if (landScape) {
            page.setRotation(90);
            contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));
        }
    } catch (IOException ioE) {
        throw new GraphicsIOException(ioE.getMessage());
    }
}

From source file:org.nuxeo.typeDocPkg.TestPdfBoxN.java

License:Apache License

public void create(String message, String outfile) throws IOException, COSVisitorException {
    PDDocument doc = null;//from   ww w  .j  av a 2  s .  c om
    try {
        doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        PDFont font = PDType1Font.HELVETICA;
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(message);
        contentStream.endText();
        contentStream.close();
        doc.save(outfile);
        doc.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}