Hello
Thanks for your request. Please try using the following code:
// Open the document.
Document doc = new Document("C:\\Temp\\Test.doc");
// Work with document.
// Insert Watermark before saving to PDF.
insertWatermarkImage(doc, "C:\\Temp\\Watermark.jpg");
doc.saveToPdf("C:\\Temp\\out.pdf");
private static void insertWatermarkImage(Document doc, String watermarkImagePath) throws Exception
{
// Create a watermark shape. This will be a shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(doc, ShapeType.IMAGE);
// Set up the the watermark.
watermark.getImageData().setImage(watermarkImagePath);
watermark.setWidth(500);
watermark.setHeight(100);
// 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);
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
// Insert the watermark into all headers of each document section.
for (Section sect : doc.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);
}
}
private static 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));
}
If it does not help you, could you please attach you input and output documents here for testing?
Best regards,
Andrey Noskov
Developer/Technical Support
Aspose Auckland Team