Hi
Thanks for your inquiry. Please try using the following
code.
@Test
public void Test001() throws Exception {
// Open the document.
Document doc = new Document("C:\\Temp\\Test.doc");
// Insert Watermark before saving to PDF.
insertWatermarkText(doc,
"Watermark");
// Save to PDF
doc.saveToPdf("C:\\Temp\\out.pdf");
}
private static void insertWatermarkText(Document doc, String
watermarkText) 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);
GraphicsEnvironment
environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
// Create buffered image that supports transparency
BufferedImage
bufimage = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_BGR);
// Create Graphics2D.
Graphics2D g2d =
environment.createGraphics(bufimage);
g2d.setColor(Color.white);
g2d.fillRect(0, 0,
500, 500);
g2d.setColor(Color.gray);
g2d.setFont(new java.awt.Font("TimesRoman",
java.awt.Font.BOLD | java.awt.Font.ITALIC, 100));
g2d.rotate(Math.PI
/ 4, 40, 50);
g2d.drawString(watermarkText, 40, 50);
// Set up the the watermark.
watermark.getImageData().setImage(bufimage);
watermark.setWidth(500);
watermark.setHeight(500);
// 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));
}
This code does not produce an excellent result, but the
result is acceptable and I think you can use this approach while you are
waiting for a fix.
Best regards,
Alexey Noskov
Developer/Technical Support
Aspose Auckland Team