I have existed pdf template
Now I want to add some text to this file, so I did that:
PdfReader reader = new PdfReader(path + PdfCreator.TEMPORARY);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path + PdfCreator.DEST));
PdfContentByte canvasBookingDate = stamper.getOverContent(1);
//add text "Hellow"
canvasBookingDate.setFontAndSize(base, 9.5f);
canvasBookingDate.moveText(72f, 788f);
canvasBookingDate.showText("Hello");
canvasBookingDate.moveText(72f, 762f);
//add text "How are you"
canvasBookingDate.setFontAndSize(base, 9.5f);
canvasBookingDate.showText("How are you");
canvasBookingDate.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
The problem is that, only "Hello" was inserted to pdf file, "How are you" was not
Maybe I wrong something there?
I also using seperate PdfContentByte object to write each text but no luck
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(path + PdfCreator.DEST));
PdfContentByte canvasBookingDate = stamper.getOverContent(1);
//add text "Hellow"
canvasBookingDate.setFontAndSize(base, 9.5f);
canvasBookingDate.moveText(72f, 788f);
canvasBookingDate.showText("Hello");
canvasBookingDate.moveText(72f, 762f);
//add text "How are you"
PdfContentByte canvasPlanName2 = stamper.getOverContent(1);
canvasPlanName2.setFontAndSize(base, 9.5f);
canvasPlanName2.moveText(72f, 762f);
canvasPlanName2.showText(entity.getPlanName());
canvasPlanName2.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
The problem is that, only "Hello" was inserted to pdf file, "How are you" was not
Your observation is inaccurate: "How are you" was inserted, merely far off-page! (Doing CtrlA CtrlC from adobe reader and pasting into some editor you would have seen that it is there somewhere.)
The cause is that you misunderstand how moveText works. Have a look at its JavaDoc documentation:
/**
* Moves to the start of the next line, offset from the start of the current line.
*
* #param x x-coordinate of the new current point
* #param y y-coordinate of the new current point
*/
public void moveText(final float x, final float y)
Thus, the coordinates are relative, not absolute!
So you should do
canvasBookingDate.beginText();
canvasBookingDate.setFontAndSize(base, 9.5f);
canvasBookingDate.moveText(72f, 788f);
canvasBookingDate.showText("Hello");
canvasBookingDate.moveText(0f, -16f);
//add text "How are you"
canvasBookingDate.showText("How are you");
canvasBookingDate.endText();
I found answer from Vishal Kawade link
Just using beginText() and endText() after each text need insert to pdf
Related
while using itext5 in android to display pdf from XHTML am trying to change the font size but it's not reflecting.
I would like to know the substitutes(or hack) for CSS as itext5 is not supporting CSS.
preparedText = output.toString("UTF-8");
list = XMLWorkerHelper.parseToElementList(preparedText, null);
// URL path =Thread.currentThread().getContextClassLoader().getResource("fontname");
// FontFactory.register(path.toString(), "test_font");
Font titleFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD,7f);
paragraph.setFont(titleFont);
paragraph.addAll(list);
publishProgress(88);
// write to document
document.open();
document.newPage();
Paragraph p= new Paragraph(paragraph);
p.setFont(titleFont);
document.add(p);
document.close();
The font you set in a paragraph applies to all text added to the paragraph afterwards, it does not change the previously added text. To set the font of the text you add to a paragraph in the constructor, there is a constructor that also accepts a font parameter.
Thus, instead of
Paragraph p= new Paragraph(paragraph);
p.setFont(titleFont);
use
Paragraph p = new Paragraph(paragraphText, titleFont);
or
Paragraph p = new Paragraph();
p.setFont(titleFont);
p.add(paragraphText);
I am relativly new to Java and I want to replace an existing iText based Javascript with pdfbox. (Java 2.0)
I have a pdf-Formsheet (but this sheet has no Acroform entries) and I want to fill it with information (Name, Birthdate and so on). The pdf is in a rectangular special size (like a contact card).
My code so far:
File file = new File("ToBeFilled.pdf");
PDDocument document = PDDocument.load(file);
System.out.println("PDF loaded");
//Retrieving the page
PDPage page = (PDPage)document.getPages().get( 0 );
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream content = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);
content.beginText();
//Setting the font to the Content stream
content.setFont(font, 30);
//Setting the position for the line (float x, float y), (0,0) = lower left corner
content.newLineAtOffset(100, 400);
String text = "This is the sample document and we are adding content to it.";
String text1 = "This is an example of adding text to a page in the pdf document. we can add as many lines";
String text2 = "as we want like this using the ShowText() method of the ContentStream class";
//Adding text in the form of string
content.showText(text);
//Adding text in the form of string
content.newLine();
content.showText(text1);
content.newLine();
content.showText(text2);
//Ending the content stream
content.endText();
System.out.println("Text added");
content.close();
//Saving the document
document.save("newPrint.pdf");
//Closing the document
document.close();
The text does not show. What am I missing here? I thought with the correct text-positions I could simply write on the pdf?
The source is working.
Maybe your content.newLineAtOffset(100, 400); is too huge - out of bounds - for your little card.
By the way, you have to setLeading(float) to use newLine() meaningfuly.
I have a PDF File (ex:Hello.pdf) and what i need to do with this PDF is Read the file and get text content and replace the text with HashMap Values (If find the key in Text)
ex:- If PDF Content Text is (My Name is [Name])
and hashMap Key "Name":"Vikrant" Then overwrite this as
"My Name is Vikrant" and write it to the PDF.
I have Tried iTextPDF Jar but still won't got any logical Solution.
/* example inspired from "iText in action" (2006), chapter 2 */
PdfReader reader = new PdfReader("D:/HelloWorld.pdf"); // input PDF
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("D:/Bubi_modified.pdf")); // output PDF
BaseFont bf = BaseFont.createFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); // set font
//loop on pages (1-based)
for (int i=1; i<=reader.getNumberOfPages(); i++){
// get object for writing over the existing content;
// you can also use getUnderContent for writing in the bottom layer
PdfContentByte over = stamper.getOverContent(i);
// write text
over.beginText();
over.setFontAndSize(bf, 10); // set font and size
over.setTextMatrix(107, 740); // set x,y position (0,0 is at the bottom left)
over.showText("I can write at page " + i); // set text
over.endText();
// draw a red circle
over.setRGBColorStroke(0xFF, 0x00, 0x00);
over.setLineWidth(5f);
over.ellipse(250, 450, 350, 550);
over.stroke();
}
stamper.close();
}
I have Tried PDFStamper(iTextPDF.jar) But this class can put the content to the pdf not edit the earlier Text or Image.
This question already has an answer here:
How to add overlay text with link annotations to existing pdf?
(1 answer)
Closed 7 years ago.
Two questions,
How to hide or set color white to Rectangle box border
How to add text to Rectangle
public void addLinkedtoTOC(String src, String dest,String IMG,int linkedPageNumber,int linkDisplayTOCPageNumber) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Rectangle linkLocation = new Rectangle(320, 695, 560, 741);
linkLocation.setBorderColorLeft(BaseColor.WHITE);
linkLocation.setBorderColorRight(BaseColor.RED);
linkLocation.setBorderColorTop(BaseColor.RED);
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(), linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,linkedPageNumber, destination);
stamper.addAnnotation(link, linkDisplayTOCPageNumber);
stamper.close();
reader.close();
}
Your first question is easy. It's a duplicate of iText - How to stamp image on existing PDF and create an anchor
When you create a PdfAnnotation object that represents a link annotation, a border is defined by default. You can remove this border using the setBorder() method at the level of the annotation as is done in the AddImageLink example:
link.setBorder(new PdfBorderArray(0, 0, 0));
Your second question has also been answered before. See for instance:
How to add text at an absolute position on the top of the first page?
How to add text inside a rectangle?
How to truncate text within a bounding box?
How to fit a String inside a rectangle?
I have combined both in the AddLinkAnnotation5 example:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
// Here we define the location:
Rectangle linkLocation = new Rectangle(320, 695, 560, 741);
// here we add the actual content at this location:
ColumnText ct = new ColumnText(stamper.getOverContent(1));
ct.setSimpleColumn(linkLocation);
ct.addElement(new Paragraph("This is a link. Click it and you'll be forwarded to another page in this document."));
ct.go();
// now we create the link that will jump to a specific destination:
PdfDestination destination = new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
3, destination);
// If you don't want a border, here's where you remove it:
link.setBorder(new PdfBorderArray(0, 0, 0));
// We add the link (that is the clickable area, not the text!)
stamper.addAnnotation(link, 1);
stamper.close();
reader.close();
}
However, there's also another way to add the text and the link annotation at the same time. That's explained in my answer to the duplicate question: How to add overlay text with link annotations to existing pdf?
In this answer, I refer to the AddLinkAnnotation2 example, where we add the content using ColumnText as described above, but we introduce a clickable Chunk:
Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
chunk.setAnchor("http://developers.itextpdf.com/frequently-asked-developer-questions");
Wrap the chunk object inside a Paragraph, add the Paragraph using ColumnText and you have your borderless link.
Is it possible to remove all text occurrences contained in a specified area(red color rectangle area) of a pdf document Using iText?
Please take a look at the RemoveContentInRectangle example.
Let's say we have the following page:
Now we want to remove all the text in the rectangle defined by the coordinates: llx = 97, lly = 405, urx = 480, ury = 445] (where ll stands for lower-left and ur stands for upper-right).
We can now use the following code:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(97f, 405f, 480f, 445f), BaseColor.GRAY));
PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
cleaner.cleanUp();
stamper.close();
reader.close();
}
As you see, we define a list of PdfCleanUpLocation objects. To this list, we add a PdfCleanUpLocation passing the page number, a Rectangle defining the area we want to clean up, and a color that will show the area where content has been removed.
We then pass this list of PdfCleanUpLocations to the PdfCleanUpProcessor along with the PdfStamper instance. We invoke the cleanUp() method and when we close the PdfStamper instance, we get the following result:
You can inspect this file: you will no longer be able to select any text in the gray area. All the text inside that rectangle has been removed.
Note that this code sample will only work if you add the itext-xtra.jar to your CLASSPATH (itext-xtra is shipped with iText core). It will only work with versions equal to or higher than iText 5.5.4.