Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot figure out how to use PDFBox

Tags:

java

pdfbox

I am trying to create a PDF file with a lot of text boxes in the document and textfields from another class. I am using PDFBox.

OK, creating a new file is easy and writing one line of text is easy. Now, when I am trying to insert the next text line or textfield, it overwrites the content.

    PDDocument doc = null;
    PDPage page = null;

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

           doc.addPage(page);
           PDFont font = PDType1Font.HELVETICA_BOLD;

           PDPageContentStream title = new PDPageContentStream(doc, page);
           title.beginText();
           title.setFont( font, 14 );
           title.moveTextPositionByAmount( 230, 720 );
           title.drawString("DISPATCH SUMMARY");
           title.endText();
           title.close();

           PDPageContentStream title1 = new PDPageContentStream(doc, page);
           title1.beginText();
           title1.setFont( font, 11 );
           title1.moveTextPositionByAmount( 30, 620 );
           title1.drawString("DEPARTURE");
           title1.endText();
           title1.close();


           doc.save("PDFWithText.pdf");
           doc.close();
    } catch (Exception e){
        System.out.println(e);
    }

It does give me an error: "You are overwriting an existing content, you should use the append mode".

So I am trying title1.appendRawCommands(String), but it is not working.

How would I add new text boxes and textfields (from another class)? I have read tens of tutorials on Internet, but they only show creating one line.

like image 634
Igor Tupitsyn Avatar asked Feb 02 '13 01:02

Igor Tupitsyn


People also ask

How do I use a PDFBox in Python?

import pdfbox p = pdfbox. PDFBox() p. extract_text('/path/to/my_file. pdf') # writes text to /path/to/my_file.

What is the use of PDFBox?

The Apache PDFBox® library is an open source Java tool for working with PDF documents. This project allows creation of new PDF documents, manipulation of existing documents and the ability to extract content from documents. Apache PDFBox also includes several command-line utilities.

How do you write paragraphs in PDFBox?

Adding Text to an Existing PDF Document. You can add contents to a document using the PDFBox library, this provides you a class named PDPageContentStream which contains the required methods to insert text, images, and other types of contents in a page of a PDFDocument.


2 Answers

PDPageContentStream title1 = new PDPageContentStream(doc, page, true, true);

OP posted this as the answer, so this will flag to the system that there was an answer

Furthermore, if the first content stream contains operations substantially changing the graphics state, e.g. by changing the current transformation matrix, and one wants the new content stream to start with these changes reverted, one should use the constructor with three boolean parameters:

PDPageContentStream title1 = new PDPageContentStream(doc, page, true, true, true);
like image 81
3 revs, 2 users 50% Avatar answered Oct 10 '22 01:10

3 revs, 2 users 50%


This implementation is deprecated.

PDPageContentStream title1 = new PDPageContentStream(doc, page, true, true);

The new implementation would be

PDPageContentStream title1 = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, true);
like image 39
Diego de la Cruz Avatar answered Oct 10 '22 02:10

Diego de la Cruz