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

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

Introduction

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

Prototype

public PDDocumentCatalog getDocumentCatalog() 

Source Link

Document

This will get the document CATALOG.

Usage

From source file:PDF.PDFNumbering.java

public PDFNumbering(String pdfFileName) throws IOException, COSVisitorException {
    String cycle = JOptionPane.showInputDialog(null, "Please enter a cycle number");
    PDDocument pdf = PDDocument.load(pdfFileName);
    List pages = pdf.getDocumentCatalog().getAllPages();
    PDFTextStripper pdfStripper = new PDFTextStripper();
    String res = pdfStripper.getText(pdf);
    //System.out.println(res);
    Boolean isPreProcessed = res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL); // check if the file is pre-processed.
    Boolean isNumbered = res.contains("/0");
    Iterator<PDPage> iter = pages.iterator();
    int sequenceNum = 1; // start from 0001
    if (isPreProcessed && isNumbered) {
        GlobalVar.updateSeqNum(pdf, cycle); // update the sequence number
    } else if (isPreProcessed) { // first time
        int pageNumber = 1;

        while (iter.hasNext()) {
            PDPage page = iter.next();/*from  w w  w .  j a  va2s.c o  m*/

            pdfStripper.setStartPage(pageNumber);
            pdfStripper.setEndPage(pageNumber);
            res = pdfStripper.getText(pdf);
            // == numbering
            if (res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL)) {
                PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                stream.setFont(PDType1Font.HELVETICA, GlobalVar.SEQ_NUM_FONT_SIZE);
                stream.moveTextPositionByAmount(GlobalVar.SEQ_NUM_TEXT_X_POSITION,
                        GlobalVar.SEQ_NUM_TEXT_Y_POSITION);
                stream.setTextRotation(3.14 / 2, GlobalVar.SEQ_NUM_TEXT_X_POSITION,
                        GlobalVar.SEQ_NUM_TEXT_Y_POSITION); // rotate text 90 degree at x = 600, y = 400
                stream.drawString(cycle + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum));
                sequenceNum++;
                stream.endText();
                stream.close();
            }
            pageNumber++;
            // end of numbering
        }

    } else { //not pre processed
        while (iter.hasNext()) {
            PDPage page = iter.next();
            PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
            // == numbering
            stream.beginText();
            stream.setFont(PDType1Font.HELVETICA, GlobalVar.SEQ_NUM_FONT_SIZE);
            stream.moveTextPositionByAmount(GlobalVar.SEQ_NUM_TEXT_X_POSITION,
                    GlobalVar.SEQ_NUM_TEXT_Y_POSITION);
            stream.setTextRotation(3.14 / 2, GlobalVar.SEQ_NUM_TEXT_X_POSITION,
                    GlobalVar.SEQ_NUM_TEXT_Y_POSITION); // rotate text 90 degree at x = 600, y = 400
            stream.drawString(cycle + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum));
            sequenceNum++;
            stream.endText();
            stream.close();
        }
    }

    // out put two pdf files: one for audit, the other for reject
    String suffix = "_" + cycle + " Numbered.pdf";
    pdfFileName = pdfFileName.replace(".pdf", suffix);
    pdf.save(pdfFileName);
    pdf.close();
}

From source file:PDF.PDFNumberingPartial.java

private void generatePDFFile(String pdfFileName, Boolean[][] statusArray, String cycle)
        throws IOException, COSVisitorException {
    PDDocument pdf = PDDocument.load(pdfFileName);
    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator<PDPage> iter = pages.iterator();

    PDDocument pdfBlank = new PDDocument();

    int pageNum = 0; // 0 based
    int sequenceNum = 1; // start from 0001
    while (iter.hasNext()) {
        PDPage page = iter.next();//  www.j  av  a  2  s. c  o  m
        PDPage pageBlank = new PDPage();

        PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false);
        PDPageContentStream streamBlank = new PDPageContentStream(pdfBlank, pageBlank, true, false);

        if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) {
            pageWrite(stream, sequenceNum, cycle);
            pageWrite(streamBlank, sequenceNum, cycle);
            sequenceNum++;
        }
        pdfBlank.addPage(pageBlank);

        stream.close();
        streamBlank.close();
        pageNum++;
    }

    // out put two pdf files: one is template for printer print hardcopies, the other is digital copy
    String suffix = "_" + cycle + "_P numbered.pdf";
    pdfFileName = pdfFileName.replace(".pdf", suffix);
    String blankPdfFileName = pdfFileName.replace(".pdf", "BLANK.pdf");

    pdf.save(pdfFileName);
    pdfBlank.save(blankPdfFileName);

    pdf.close();
    pdfBlank.close();
}

From source file:PDF.PDFRemover.java

private void extractGoodPdf(PDDocument pdf, String auditPdfFileName, Boolean[][] statusArray)
        throws COSVisitorException, IOException {

    PDDocument auditPdf = new PDDocument();
    int pageNum = pdf.getNumberOfPages();

    // add reject page into rejectPdf
    for (int i = 0; i < pageNum; i++) {
        PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i);
        if (!statusArray[GlobalVar.VOID_BUTTON_INDEX][i]) {

            auditPdf.addPage(page);/*from www .ja v a 2 s .  co m*/
        }
    }

    auditPdf.save(auditPdfFileName);
    auditPdf.close();
}

From source file:pdf.PDFUtils.java

License:Open Source License

public static JPanel createPanelWithAllPages(PDDocument pdfDoc) throws IOException {
    JPanel docPanel = new JPanel();
    docPanel.setLayout(new BoxLayout(docPanel, BoxLayout.Y_AXIS));
    List<PDPage> docPages = pdfDoc.getDocumentCatalog().getAllPages();

    for (PDPage page : docPages) {
        PDFPagePanel pagePanel = new PDFPagePanel();
        pagePanel.setPage(page);/*  w  w  w  .ja v a  2 s .  c o m*/

        docPanel.add(pagePanel);
    }

    return docPanel;
}

From source file:PDF.RotatePDF.java

public void rotate(double degree) throws IOException, COSVisitorException {
    if (FILENAME != null) {
        String destFileName = FILENAME.replace(".PDF", ".pdf");
        destFileName = FILENAME.replace(".pdf", "_rotated.pdf");
        final PDDocument document = PDDocument.load(FILENAME);
        final AffineTransform transform = AffineTransform.getRotateInstance(degree);//
        List<PDPage> pages = document.getDocumentCatalog().getAllPages();//
        for (PDPage page : pages) {
            transformPage(document, page, transform);
        }//from w  w w.  ja v  a2  s  .  com
        document.save(destFileName);
        JOptionPane.showMessageDialog(null, "The pdf file is successfully rotated.");
    }
}

From source file:pdf.to.info.PDF.java

/**
 * Reading fields of a PDF file//  ww w.  ja v  a  2 s . c  om
 *
 * @param filePath
 * @throws java.io.IOException
 */
public void PdfFields(String filePath) throws IOException {
    PDDocument pdDoc = ReadPDDoc(filePath);
    PDAcroForm form = pdDoc.getDocumentCatalog().getAcroForm();
    if (form != null) {
        List FieldTy = form.getFields();
        PDField pdfFields;
        for (int i = 0; i < FieldTy.size(); i++) {
            pdfFields = (PDField) FieldTy.get(i);
            String fieldNameTyope = pdfFields.getFieldType();
            System.out.println(fieldNameTyope);
        }
    } else {
        System.out.print("There is no standard field in your PDF file.\n");
    }
}

From source file:pdfbox.GetImagesFromPDF.java

public static void main(String[] args) {
    try {/*from   w  w w .  j  ava2  s  .  c  o m*/
        String sourceDir = "D:/PdfBox/04-Request-Headers.pdf";// Paste pdf files in PDFCopy folder to read
        String destinationDir = "D:/PdfBox/";
        File oldFile = new File(sourceDir);
        if (oldFile.exists()) {
            PDDocument document = PDDocument.load(sourceDir);

            List<PDPage> list = document.getDocumentCatalog().getAllPages();

            String fileName = oldFile.getName().replace(".pdf", "_cover");
            int totalImages = 1;
            for (PDPage page : list) {
                PDResources pdResources = page.getResources();

                Map pageImages = pdResources.getImages();
                if (pageImages != null) {

                    Iterator imageIter = pageImages.keySet().iterator();
                    while (imageIter.hasNext()) {
                        String key = (String) imageIter.next();
                        PDXObjectImage pdxObjectImage = (PDXObjectImage) pageImages.get(key);
                        pdxObjectImage.write2file(destinationDir + fileName + "_" + totalImages);
                        totalImages++;
                    }
                }
            }
        } else {
            System.err.println("File not exists");
        }
    } catch (Exception e) {
    }
}

From source file:pdfbox.PDFA3File.java

License:Apache License

/**
 * Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the
 * metadata level, this will not e.g. convert graphics to JPG-2000)
 *///from   www . ja v a 2  s.  c o m
private PDDocumentCatalog makeA3compliant(PDDocument doc) throws IOException, TransformerException {
    PDDocumentCatalog cat = doc.getDocumentCatalog();
    PDMetadata metadata = new PDMetadata(doc);
    cat.setMetadata(metadata);
    // jempbox version
    XMPMetadata xmp = new XMPMetadata();
    XMPSchemaPDFAId pdfaid = new XMPSchemaPDFAId(xmp);
    xmp.addSchema(pdfaid);

    XMPSchemaDublinCore dc = xmp.addDublinCoreSchema();
    String creator = System.getProperty("user.name");
    String producer = "PDFBOX";
    dc.addCreator(creator);
    dc.setAbout("");

    XMPSchemaBasic xsb = xmp.addBasicSchema();
    xsb.setAbout("");

    xsb.setCreatorTool(creator);
    xsb.setCreateDate(GregorianCalendar.getInstance());
    // PDDocumentInformation pdi=doc.getDocumentInformation();
    PDDocumentInformation pdi = new PDDocumentInformation();
    pdi.setProducer(producer);
    pdi.setAuthor(creator);
    doc.setDocumentInformation(pdi);

    XMPSchemaPDF pdf = xmp.addPDFSchema();
    pdf.setProducer(producer);
    pdf.setAbout("");

    // Mandatory: PDF-A3 is tagged PDF which has to be expressed using a
    // MarkInfo dictionary (PDF A/3 Standard sec. 6.7.2.2)
    PDMarkInfo markinfo = new PDMarkInfo();
    markinfo.setMarked(true);
    doc.getDocumentCatalog().setMarkInfo(markinfo);

    pdfaid.setPart(3);
    pdfaid.setConformance("A");/*
                                * All files are PDF/A-3, setConformance refers
                                * to the level conformance, e.g. PDF/A-3-B where
                                * B means only visually preservable, U means
                                * visually and unicode preservable and A -like
                                * in this case- means full compliance, i.e.
                                * visually, unicode and structurally preservable
                                */
    pdfaid.setAbout("");
    metadata.importXMPMetadata(xmp);
    return cat;
}

From source file:pdfbox.PDFA3FileAttachment.java

License:Apache License

private void attachSampleFile(PDDocument doc) throws IOException {
    PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();

    // first create the file specification, which holds the embedded file

    PDComplexFileSpecification fs = new PDComplexFileSpecification();
    fs.setFile("Test.txt");
    COSDictionary dict = fs.getCOSDictionary();
    // Relation "Source" for linking with eg. catalog
    dict.setName("AFRelationship", "Alternative");
    // dict.setName("AFRelationship", "Source");

    dict.setString("UF", "Test.txt");
    // fs.put(new PdfName("AFRelationship"), new PdfName("Source"));

    String payload = "This is a test";

    InputStream is = new ByteArrayInputStream(payload.getBytes());

    PDEmbeddedFile ef = new PDEmbeddedFile(doc, is);
    // set some of the attributes of the embedded file

    ef.setSubtype("text/plain");
    // ef.setFile(fs);
    // ef.getStream().setItem(COSName.UF, fs);

    ef.setModDate(GregorianCalendar.getInstance());

    // PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer,
    // src.getAbsolutePath(), src.getName(), null, false, "image/jpeg",
    // fileParameter);

    // fs.put(new PdfName("AFRelationship"), new PdfName("Source"));

    ef.setSize(payload.length());/*from w  w  w . j a va2 s  .c o m*/
    ef.setCreationDate(new GregorianCalendar());
    fs.setEmbeddedFile(ef);

    // now add the entry to the embedded file tree and set in the document.
    efTree.setNames(Collections.singletonMap("My first attachment", fs));

    /**
     * Validating file "RE-20131206_22.pdf" for conformance level pdfa-3a The
     * key UF is required but missing. The key AFRelationship is required but
     * missing. File specification 'Test.txt' not associated with an object.
     */
    // attachments are stored as part of the "names" dictionary in the document
    // catalog
    PDDocumentCatalog catalog = doc.getDocumentCatalog();

    PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
    names.setEmbeddedFiles(efTree);
    catalog.setNames(names);

    // // AF entry (Array) in catalog with the FileSpec
    // PDAcroForm pdAcroForm = new PDAcroForm(doc);
    // COSArray cosArray = new COSArray();
    // cosArray.add(fs);
    // catalog.setItem("AF", cosArray);

}

From source file:pdfbox.PDFAFile.java

License:Apache License

/**
 * Create a simple PDF/A document./*from www.ja  va2 s . c  o m*/
 * This example is based on HelloWorld example.
 * As it is a simple case, to conform the PDF/A norm, are added : - the font
 * used in the document - a light xmp block with only PDF identification
 * schema (the only mandatory) - an output intent
 * 
 * @param file
 *          The file to write the PDF to.
 * @param message
 *          The message to write in the file.
 * @throws Exception
 *           If something bad occurs
 */
public void doIt(String file, String message) throws Exception {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        PDPage page = new PDPage();
        doc.addPage(page);

        InputStream fontStream = PDFA3File.class.getResourceAsStream("/Ubuntu-R.ttf");
        PDFont font = PDTrueTypeFont.loadTTF(doc, fontStream);

        // create a page with the message where needed
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(message);
        contentStream.endText();
        contentStream.saveGraphicsState();
        contentStream.close();

        PDDocumentCatalog cat = doc.getDocumentCatalog();
        PDMetadata metadata = new PDMetadata(doc);
        cat.setMetadata(metadata);

        // jempbox version
        XMPMetadata xmp = new XMPMetadata();
        XMPSchemaPDFAId pdfaid = new XMPSchemaPDFAId(xmp);
        xmp.addSchema(pdfaid);
        pdfaid.setConformance("B");
        pdfaid.setPart(1);
        pdfaid.setAbout("");
        metadata.importXMPMetadata(xmp.asByteArray());

        InputStream colorProfile = PDFA3File.class.getResourceAsStream("/sRGB Color Space Profile.icm");
        // create output intent
        PDOutputIntent oi = new PDOutputIntent(doc, colorProfile);
        oi.setInfo("sRGB IEC61966-2.1");
        oi.setOutputCondition("sRGB IEC61966-2.1");
        oi.setOutputConditionIdentifier("sRGB IEC61966-2.1");
        oi.setRegistryName("http://www.color.org");
        cat.addOutputIntent(oi);

        doc.save(file);

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