Free Support Forum - aspose.com

Header Color Error

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;
}

}

@anilg530,

Thanks for your inquiry. Please ZIP and upload your input Word document and Aspose.Words generated output Word document (.docx file) showing the undesired behavior here for testing. We will investigate the issue on our end and provide you more information.

Hey Awais, I attached the input word document and aspose.words generated output document. Thank you for your help.
Documents.zip (332.9 KB)

@anilg530,

Thanks for your inquiry. After an initial test with the licensed latest version of Aspose.Words for Java i.e. 18.7, we were unable to reproduce this issue on our end. Please see output document (awjava-18.7.zip (162.5 KB)). We used the code from your first post on our end to produce this output.