How do numbering of page in word file by JAVA - java

How do i numbering of pages in word file by Java.
I am using Apache POI driver to interact JAVA and word .
i want border and page number as well in my word file while i am creating file from JAVA.
Please help.

The question marked as a duplicate has a complex answer to a relatively simple question.
The simple answer (for page number) is very similar to this answer: https://stackoverflow.com/a/40264237/2296441. The difference is just which field to insert. The afore mentioned answer shows how to insert a TOC field. In your case you want a PAGE field.
XWPFParagraph p;
...
// get or create your paragraph
....
CTP ctP = p.getCTP();
CTSimpleField page = ctP.addNewFldSimple();
page.setInstr("PAGE");
page.setDirty(STOnOff.TRUE);
Note: setDirty tells Word to update the field which causes a dialog to be opened when the document is opened. This dialog is MS Word making sure you want to update the field. I don't think you can disable the dialog and still have the field calculated on open.
To set page borders you are once again going to have to break into the CT classes. In this case the appropriate location in the document is the section properties. Here is how to set a double line border around the whole page set back 24 points from the page edge.
// Page Borders
CTDocument1 ctDoc = doc.getDocument();
CTBody ctBody = ctDoc.getBody();
CTSectPr ctSectPr = ctBody.isSetSectPr() ? ctBody.getSectPr() : ctBody.addNewSectPr();
CTPageBorders ctPgBorders = ctSectPr.isSetPgBorders() ? ctSectPr.getPgBorders() : ctSectPr.addNewPgBorders();
ctPgBorders.setOffsetFrom(STPageBorderOffset.PAGE);
CTBorder ctBorder = CTBorder.Factory.newInstance();
ctBorder.setVal(STBorder.DOUBLE);
ctBorder.setSpace(new BigInteger("24"));
ctPgBorders.setTop(ctBorder);
ctPgBorders.setBottom(ctBorder);
ctPgBorders.setRight(ctBorder);
ctPgBorders.setLeft(ctBorder);
Disclaimer
The MS-Word functionality in POI is still largely unfinished, and subject to change.

Related

iTextPDF 7 - Table of content with clickable page numbers

I'm trying to create a table of content with iTextPDF 7 in Java from existing PDFs.
I've tried to use Link link = new Link() and PdfAction.createGoTo(PdfExplicitDestination.createFit(pageNum)) but to no avail.
I can't link the page number/row in TOC to the respective page in the end PDF (after merging).
The documentation on their website only gets me so far and I'm out of ideas.
What is the correct way to create a TOC for existing PDFs and how to implement it correctly?

add an invisible paragraph in a word document using Apache POI

I have a word document (.docx) that contains some information, I want to edit this document and add to it some text , I want that the text still invisible when I open the document but I want also to access to it easily from my code. Do you have please any idea how can I proceed ?
I was in fact looking for a solution with ApachePOI to make my text invisible in the generated word document.After some researches, I found this solution:
for (XWPFParagraph paragraph : doc.getParagraphs()){
for(XWPFRun run : paragraph.getRuns()){
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
run.getCTR().getRPr().setVanish(onoffnull);
}
}
this code make all paragraphs of a word document invisible by the user.

Java Jsoup: Retrieve only the article

Trying to retrieve the text of the article. I want to select all of the text within
<p>... </p>
I was able to do that.
But I only want to retrieve the text from the article body, not the entire page
Document article = Jsoup.connect("html doc").get();
Elements paragraphs = article.select("p");
The code above gets the entire text from the page. I just want the text between
<article itemprop= "articleBody">...</article>
I'm sorry if this was hard to understand, I tried to formulate the
questions as best I could.
Elements#text() will return text-only content of all the combined paragraphs (see here for more details https://jsoup.org/apidocs/org/jsoup/select/Elements.html)
Try selecting on the itemprop attribute
for (Element paragraph : doc.select("article[itemprop=articleBody]"))
System.out.println(paragraph.text());
See CSS Selectors for more tips

HTML to PDF using iText, formatting issues

I'm working with iText in java to write PDF files. I'm trying to write a paragraph like heading and then text start very after the heading in the same line like
Heading: this a para now ...
Heading is bold and para is in normal text but I'm unable to do this using iText. I tried to use:
fonts[2] = new Font(Font.HELVETICA, 8, Font.BOLD);
Paragraph paranumber = new Paragraph(
fonts[2].getCalculatedLeading(1),
headingText.trim()
+ " ", fonts[0]);
Paragraph para = new Paragraph(
fonts[0].getCalculatedLeading(1), contentText.trim(), fonts[0]);
para.setAlignment(Element.ALIGN_JUSTIFIED);
para.setSpacingAfter(3f);
//Now adding the para to paraNumber that is having the heading and expecting
//that it will be added very after the heading, but this does not show correct
//result, formatting issue.
paranumber.add(para);
mct.addElement(paranumber);
I also tried to create a new paragraph and added both paras(heading para and normal text para) to that new one, but that is also not showing proper result. please see below chunk for that.
Paragraph newPara = new Paragraph();
newPara.add(paranumber);
newPara.add(para);
but this also not show proper formatting.
Or if anyone can advise me to use some other way to create PDF from HTML that will be good too, so that i may rewrite the module to create required PDF. Please advise.
Paragraphs typically use concepts like indentation and increased leading to set them apart visually. They are block level elements, not inline.
It doesn't make sense to add a paragraph inside another paragraph. The added paragraph would typically start on a new line, essentially making it a separate paragraph anyway.
To get a paragraph with different fonts, like your example, you can use Chunks in iText. A Chunk is basically a piece of text with an associated font.
Font fontbold = new Font(BaseFont.createFont(BaseFont.HELVETICA_BOLD,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);
Font fontregular = new Font(BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);
Chunk header = new Chunk("Heading: ", fontbold);
Chunk content = new Chunk("this is a para now ...", fontregular);
Paragraph paragraph = new Paragraph();
paragraph.add(header);
paragraph.add(content);
document.add(paragraph);
The result looks like this:
It's not clear from your question and code sample how HTML is involved. I assume you are somehow parsing HTML input and converting the parsed content to PDF using iText Elements. This is a valid approach. Alternatively, you can look into iText XML Worker, which does XHTML (+CSS) to PDF conversion.

POI enable different header/footer for the first page in word docx file

I'm generating a docx file using Apache POI 3.13 and I stuck with headers/footers for first page.
I create XMPFParagraph[] without any problem. Next I create headers and footers like this (I've tried in different oreder):
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, defaultHeader);
policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, defaultFooter);
policy.createHeader(XWPFHeaderFooterPolicy.FIRST, firstHeader);
policy.createFooter(XWPFHeaderFooterPolicy.FIRST, firstFooter);
Once I generate my docx file I could see my default header/footer on every page including first one. But if I select to use different header/footer for the first page - my first header and footer apperes correctly.
How could I make this happens automaticaly via code? And is there any appropriate documentation with examples about POI?
If you want to set a first page header in a section, you must enter a title page tag in section properties tag (w:sectPr). The title page tag can be empty, but it is necessary. In your case, you can add only 2 code lines:
CTSectPr sect = document.getDocument().getBody().getSectPr();
sect.addNewTitlePg();
`Best regards!

Categories

Resources