In my pdf I need to have more than one header and footer.In header I want title heading on left and some text on the center.
Likewise in footer I need to print my company name on the left side,page number on center and some info regarding the contents in my table on the right side.
I have seen so many posts but I didn't get the correct idea for creating this.Somebody please help me with some example code snippets.Thanks
Headers and footers should be added using 'page events'. If you need some examples, just look for the keyword header / footer on the official web site.
Just create a class that extends PdfPageEventHelper and implement the onEndPage() method. People who read the documentation do not make the common mistake to use the onStartPage() method, but maybe you overlooked this, so I'm adding this as an extra caveat.
Add an instance of your class to the PdfWriter object with the setPageEvent() method.
I don't know if I understand what you mean by "multiple" headers. If you have more than one page event implementation, you can add them all using the setPageEvent() method and they will all be executed. If you want to switch from one page event implementation to another, you need to use setPageEvent(null) first.
Maybe you want the header to be different for different pages, just use a member-variable in your page event implementation and change it along the way. In one of the book examples named MovieHistory2, the text for the header is stored in a String array named header.
The position of the header depends on the page number:
public void onEndPage(PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("art");
switch(writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, header[0],
rect.getRight(), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, header[1],
rect.getLeft(), rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
For even page numbers, the header is added to the right; for odd page numbers to the left. The footer is centered as you can see.
You also mention a header table. If you want to use a table, please take a look at the MovieCountries1 example.
You say: "I have seen so many posts but I didn't get the correct idea for creating this." You will get the correct idea by reading the documentation, more specifically chapter 5 of the book "iText in Action — Second Edition" from which the code snippets I'm referring to are taken.
Related
i am using docx4j library and using templating to genearte report from my application.
i have following requirement,
When a page break comes between a paragraph content, i need to add a custom title before the next page content starts as you see in figure.
I know if we need to repeat same title , we can achieve it by using table and repeating header row. But there title will be same. Here I need custom title.
Paragraph is getting populatated from backend and how do we figure out page breaks happens at code level ?
Thanks in advance
This has to do with the Word Object Model. Word really does not have the concept of pages in the underlying structure of a document. Word Doesn't Know What a Page Is by Daiya Mitchell, MVP
Because of that, this would be better posted in the Word Answers forum hosted by Microsoft.
There are ways to deal with this using headers (not table headers necessarily although they can be used) or using a shape anchored to the table to occlude the word "continued" in the original header.
When you say templating, are you talking about Word templates (term of art) or something else?
I have a pdf document created by Google Chrome. When parsing the text via PDFBox (Java), I found that there was a hidden block of text straddled between the pages. Although the rendering mode was "FILL", I found that the element was off the page. Problem solved.
Now, I find that another similar element also appears off the page, but the coordinates to not tell this. It is within the visible margin of the second page that it straddles. It has y2 = 31.195312 and max height of 29.894833 (Font size = 36). Calculated y1 is about 1, still on the page.
The text positions obj shows some interesting internal properties, but they are not public vars. All I have is this TextPosition object (https://pdfbox.apache.org/docs/1.8.10/javadocs/org/apache/pdfbox/util/TextPosition.html) and the surrounding context.
I can reproduce the issue, but it requires my particular document. Could be attempted with page-break-inside tests, but I haven't found a simple test for it. I'm looking for some kind of margin, but so far all the boxes from within this.getCurrentPage() show just the plain page height, and no start position. Another possibility is that there is another way of looking for the coordinates than firstTextPos.getY() and firstTextPos.getHeight().
PDF in Mac Preview:
The text is selected between pages and is listed on the second page. In the case where it is listed on the first page, I am able to handle the issue as described above.
TextPosition object private vars:
I am processing a word 2010 Document using Docx4J. I want to print the page number and line number of String which i am searching for.
For Example:
My Document is having String called hello at Page 2, Page 6. My o/p should be like this.
Hello found at Page 2 - Line 4,
Hello found at Page 6 - Line 6.
I tried to do but i failed.
I was able to Highlight that Text and able to comment by travelling across the document. But I failed to get its line number and page number.
Note : 1)There are two blank Pages Page 3 and Page 4.
2)There are No paragraphs at the end and starting of pages.
What you're after is a page layout model, because in order to accurately determine what page or line something is on, you need to consider the mapped paper size, headers, footers, gutters, margins, font and sizing, line-height, and so on and so forth.
This is not something that docx4j has, so you'd need to come up with something (a basic word count would be simpler: you can count all the Text objects in a document for example).
One approach may be to consider how the PDF rendering functionality in docx4j works. Take a look at the FOP rendering code in docx4j, which may offer some clues re mapping pages at least:
https://github.com/plutext/docx4j/tree/master/src/main/java/org/docx4j/convert/out/fo
I want to add a piece of text to every page of a PDF file. This answer in SO works fine. But, the text is added to the top of the page. I would like to add my text to the bottom of each page. How do I do this?
Here is the relevant part of the code.
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
document.add(new Paragraph("My Text here")); //As per the SO answer
}
pageOfCurrentReaderPDF = 0;
}
The code is part of a function which accepts a folder, reads the PDF files in it and merges them into one single file. So, I would like to add the text in the above loop itself, instead of iterating the file once again.
If you want to automatically add content to every page, you need a page event.
This is explained in chapter 5 of my book" iText in Action - Second Edition".
If you don't own a copy of the book, you can consult the examples here.
You can also find solutions by looking for the keyword Header / Footer.
The example you're referring to doesn't look correct at first sight. Sure, you can use "two passes", one to create the content, and another to add headers or footers, but the suggested solution is different from the recommended solution: http://itextpdf.com/examples/iia.php?id=118
You are copying the mistake in your question: why on earth would you import the document you've just created into a new document, thus throwing away all possible interactivity you've added to that document? It just doesn't make sense. It's unbelievable that this answer received that many up-votes. I'm the original developer of iText and I'm not at all happy with that answer!
In your case, there may be no need to create the document in memory first and to add the footer afterwards. Just take a look at http://itextpdf.com/examples/iia.php?id=104
You need to create a PdfPageEvent implementation (for instance using PdfPageEventHelper) and you need to implement the onEndPage() method.
Documented caveats:
Do not use onStartPage() to add content,
Do not add anything to the Document object passed to the page event,
Unless you specified a different page size, the lower-left corner has the coordinate x = 0; y = 0. You need to take that into account when adding the footer. The y-value for the footer is lower than the y-value for the header.
For more info: consult my book.
Have a look at chapter 6 of iText in Action, 2nd edition, especially at subsection 6.4.1: Concatenating and splitting PDF documents.
Listing 6.22, ConcatenateStamp.java, shows you how you should create a PDF from copies of pages (in your case: all pages) of multiple other PDFs; the sample additionally adds a new "Page X of Y" footer; this demonstrates how you can add content at given positions on the pages while merging the source files.
Perhaps this may be of assistance here... I suspect you want to do something like the following:
cb.addTemplate(page, 0, 0);
document.add(new Paragraph("My Text here"));
document.setFooter(new HeaderFooter("Footnote goes here"));
}
pageOfCurrentReaderPDF = 0;
I have a requirement to insert Content into the middle of the page in a PDF.
The Content may be a Dynamic Table or an Image.
My Concept was to first split the PDF into 2 parts, then get the new Content that is to be added and append by replacing a place holder field.
the Splitting is called Tiling as per IText and here is an example for the same.
http://itextpdf.com/examples/iia.php?id=116
The Code above has 2 drawbacks:
1. It splits the page into 16 parts. but that is part of the example. Still i cant figure out a way to split the file into 2 parts only.
2. secondly the split page is converted to a complete page thus disturbing its proportions.
The Rearranging code is the another problem.
The remaining Content should be re-ordered in append mode. but till yet i have only found codes to add complete new pages rather than just the content.
I have found a code that appends the PDF content by replacing a placeholder:
float[] fieldPosition= pdfTemplate.getAcroFields().getFieldPositions("tableField");
PdfPTable table = buildTable();
PdfContentByte cb = stamper.getOverContent(1);
table.writeSelectedRows(0, -1, fieldPosition[1],fieldPosition[4],cb);
Please help me to solve this requirement.
PDF is a presentation format, not an edition format. In other words, it is not designed to allow content insertion, with the original content reflowing gracefully. As a consequence, no tool (at least, none that I know of, and surely not iText) will enable you to achieve what you were given as a requirement.
My advice :
refuse the assignment since it's not feasible, or
get your hands on the original document, insert the desired extra content, and then convert to PDF.