Hi, I’m having a bug with Aspose API when converting to a word doc from a template file. So the document keeps displaying red text in the header when it is supposed to be black by default.
Original template:
template.JPG (30.4 KB)
Template with bookmark:
Contract1.JPG (36.6 KB)
The code:
public class Watermark {
private String watermarkPath = GlobalPath.watermarkImage;
public void addWatermark(Document document) throws Exception {
Shape watermark = new Shape(document, ShapeType.IMAGE); //TEXT_PLAIN_TEXT
watermark.getImageData().setImage(watermarkPath);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500);
watermark.setHeight(500);
// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setBehindText(true);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(document);
watermarkPara.appendChild(watermark);
// Insert the watermark into all headers of each document section.
for (Section sect : document.getSections()){
// There could be up to three different headers in each section, since we want
// the watermark to appear on all pages, insert into all headers.
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
}
@SuppressWarnings({ "unchecked" })
public void removeWatermark(Document document) {
NodeCollection<Node> shapesColl = document.getChildNodes(NodeType.SHAPE, true, false);
Node[] shapesArray = shapesColl.toArray();
for (int i = 0; i < shapesArray.length; i++)
{
Shape shape = (Shape) shapesArray[i];
if(shape.isImage()){
// if (shape.getImageData().getTitle().equalsIgnoreCase("watermark")){
if (shape.getHeight() == 500 && shape.getWidth() == 500){
shape.remove();
}
// }
}
}
}
private void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception {
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null){
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
// Insert a clone of the watermark into the header.
header.appendChild(watermarkPara.deepClone(true));
}
public String getWatermarkPath() {
return watermarkPath;
}
public void setWatermarkPath(String watermarkPath) {
this.watermarkPath = watermarkPath;
}
}