Hi,
pinnamanenim:1) Do we always need to use the ooxmlcompliance options to save to 2013 or 2016 versions?like: OoxmlSaveOptions options = new OoxmlSaveOptions();options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
pinnamanenim:2) I understand we need to use optimizefor option for watermarks. Do we need to use this for other operations like updating doc properties, track changes etc..?Document doc = new Document(ipfile);doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2007);3) Is it always MsWordVersion.WORD_2007? or can we set WORD_2013 for other operations like updating doc properties, track changes etc..
private static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a watermark shape.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily("Times New Roman");
watermark.setWidth(500);
watermark.setHeight(100);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more
// Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more
// Word-style watermark
// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
ShapeRenderer shapeRenderer = watermark.getShapeRenderer();
Shape shape;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.PNG);
shapeRenderer.save(outputStream, saveOptions);
shape = builder.insertImage(outputStream.toByteArray());
shape.setWrapType(WrapType.NONE);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(shape);
// Insert the watermark into all headers of each document section.
for (Section sect : doc.getSections()) {
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
}
Thank you very much Tahir.