Is there any function to process the page of word? I found the old ticket that mentioned the DocumentLayoutHelper, but I can’t find how to use.
for example, get the document build by page number, moveToPageNumer()??
I’d like insert img watermark and text watermark on every page, but i fount when I add the second watemark would be delete first one using word.getWatermark().setImage。
So my solutions the first image watermak, I user the builder insert image on every page, but not using header or footer, the document has own headers and footers
@GusGus You need to use LayoutCollector to get the page number. Following links can be usefull:
Normally, watermark is inserted into the documents header, which is under the main document’s body content. This is the same behavior as in MS Word when you insert a watermark.
Here is a sample code:
Document doc = new Document("input.docx");
for (Section sect : doc.getSections())
{
Shape watermark = createWatermark(doc, sect);
insertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermark, sect, HeaderFooterType.HEADER_EVEN);
}
doc.save("output.docx");
private static Shape createWatermark(Document doc, Section currentSection) throws Exception {
PageSetup pageSetup = currentSection.getPageSetup();
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.setName("WaterMark");
watermark.getTextPath().setText("In Front Watermark");
watermark.getTextPath().setBold(true);
watermark.getTextPath().setSize(100);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500);
watermark.setHeight(100);
watermark.getFill().setForeColor(Color.LIGHT_GRAY);
watermark.getFill().setTransparency(0.7);
watermark.setStrokeColor(Color.LIGHT_GRAY);
watermark.getStroke().setTransparency(0.7);
watermark.setRotation(-45);
watermark.setWrapType(WrapType.NONE);
watermark.setBehindText(true);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
double left = pageSetup.getPageWidth() / 3 - pageSetup.getLeftMargin() - pageSetup.getRightMargin();
watermark.setLeft(left);
watermark.setTop(pageSetup.getPageHeight() / 2);
return watermark;
}
Thanks, could you please show me the funtion of insertWatermarkIntoHeader .
and how to auto set shape width and height
@GusGus Here is the code:
private static void insertWatermarkIntoHeader(Document doc, Shape watemark, Section sect, int headerType) {
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null) {
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
Paragraph headerPara = (Paragraph) header.getChild(NodeType.PARAGRAPH, 0, true);
if (headerPara == null) {
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watemark);
header.appendChild(watermarkPara.deepClone(true));
} else {
headerPara.appendChild(watemark);
}
}
@GusGus There are no automatically set widht and height values for text shapes. You need to set them. However, if you set only height, the width will be automatically scaled.