Apache POI for docx Insert Text On Specific Page - java

I'm trying to make Table of Contents for my Word document docx.
Apache POI is still too buggy. The document.createTOC() does not produce anything unless placed at the end. Sometimes, it doesn't give the correct page numbers.
The document.enforceUpdateFields() doesn't do anything!
So I thought I make my own method that creates the Table of Content. However, I will call it at the end but I need it to be inserted at the beginning!
In other words, suppose my document at some point in my program has some text on the first page and the second page. And I haven't yet saved it; how do I insert at the beginning of first page?

I haven't tried this yet. But, after you write the document. Reload it again
Then try the following:
List<XWPFParagraph> paragraphs = document.getParagraphs();
XWPFRun run = paragraphs.get(0).insertNewRun(0); // first paragraph, 0 is the position
run.setText("your data here");

Related

How can I resolve the printing issue for the document that is modified by apache poi

I want to ask a question which is related to apache poi and generated file printing. What I am doing is I am opening a .docx file, inserting some data and saving and printing it.
I am having a file with only 1 table which is something like:
document table
Now, if there are 'n' items, I am generating n-1 rows which copies the format of row 3.
First item is inserted in row 3, if there's another item I am adding a row in the table and saving it likewise I am creating the rows.
Now I want to make this table length = page length. For this I am counting exactly how many rows this page can incorporate and based on that I am inserting empty lines without inside top and bottom border.
The file does not have any header/ footer or any other details as of now.
While I am creating the file I am shown the file which is coming in print preview of covering the whole page of size A4.
But this document is printed only on 80% of paper. I am having 0.3 cm margins but in print the whole page is not printed.
This does not seem to be the printer issue because if other software like Google Chrome are used then it prints the complete page.
So I want to ask that how can I solve this? I tried to search about this but till now I couldn't solve it. I think of this as an issue that whenever I am using XWPFDocument, its margins and all are changed set to default size. I don't know whether this is the issue or something other is, do I need to write some commands to preserve the margins and all or there's something that I need to do.
How can I solve this thing?
Thank you.

split docx based on footer using apache poi or docx4j

i have a big docx file and i want to split it to a new docx containing only pages with the footer which contains "Appendix B" word in it, can i have some code example or any help.
You can have an algorithm:
inspect the footers to find which ones contain the words of interest. Note the relId in the rels part pointing to such footer.
now go through the main document part, looking at the sectPr elements. Find sectPr elements containing the relId(s). Note that it might be implicit (same as previous).
Provided your footer applies to every page in the relevant section(s), then you can just delete the content before and after, then save the resulting docx.

iText - add Table at the end of last page IF Space available

I am using iText 2.1.7
I write a pdf document for which the page size and contant can differ each time. What i want to achieve is a table at the very last page and at the bottom of that page. I am aware of the method 'writeSelectedRows', but with this it could happen that i overwrite text on the page, because i dont know if there is space for my table.
So in conclusion:
If i have reached the last page i want to add my table at the bottom of the page. But if the table does not fit, i want a new page and on this page i want to add the table at the bottom.
I could not find a solution so far.
Use this http://developers.itextpdf.com/de/node/1910 to calculate the height of a table and then check the available space on a page with PdfWriter.GetVerticalPostition(). You might want to consider your Document.BottomMargin or some other offset in your space calculation. (v4.1.6)
Check out this link:
http://developers.itextpdf.com/content/best-itext-questions-stackoverview/general-questions-about-itext/itext7-how-find-out-current-cursor-position-page
I suppose that once you know the current cursor position on this page, you can write a simple if-statement to add a new page, or not.
Kind regards,
Joris

Insert images in different pdf pages

I am trying to set a list of images in a PDF document using iText with Java, i could just insert some of them in the first page but i don't know how to jump to the next pages in order to put the rest of my pictures
for(int i = 0; i < 25; i++) {
Image myImg = Image.getInstance("/home/code/img"+i+".png");
imgPaper.setAbsolutePosition(50, 728-(y*58));
document.add(myImg);
y++;
}
The OP clarified his question in a comment
i have already another pages, i just want how to jump to themĀ 
You seem to be creating a new document using a PdfWriter. That class is designed to create a pdf one page after the other. As soon as you start a new page, all former ones are written to file.
Thus, in this process you cannot jump to arbitrary pages. You have to add all information for a page while it is the current one.
If, after creating a multi page document, you need to manipulate the content of its pages, first close the document (finishing it), read it into a PdfReader, and apply a PdfStamper which allows you to manipulate arbitrary pages of an existing PDF.
Alternatively, especially if your images constitute something like a water mark or header/footer Logos, consider using page events in your pdf creation process with the PdfWriter.
try to add a new line to your document
document.add( Chunk.NEWLINE );
link for info:
How to insert blank lines in PDF?

How to insert content in the middle of a page in a PDF using IText

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.

Categories