Hi, I tried converting .doc to .docx file format using Aspose library in java.
Java Version :8 (JDK : 1.8.0_351)
Aspose jar version: aspose-words-23.1.jar
While setting watermark image(background image),not able to set for middle pages but able to set first and last pages in .docx files.
// Insert the watermark shape
Shape MMGGwatermark = createWatermarkShape(doc, "watermark" + (pageIndex + 1) + ".png",
private static Shape createWatermarkShape(Document doc, String imagePath, double bckImageTop, double bckImageLeft) throws Exception {
// Create a new shape with the watermark image
Shape watermark = new Shape(doc, ShapeType.IMAGE);
watermark.getImageData().setImage("D:\\JavaApplication1\\src\\" + imagePath);
// Set the width and height of the watermark image independently
watermark.setWidth(558.0);
watermark.setTop(bckImageTop);
watermark.setLeft(bckImageLeft);
//watermark.setHeight(100);
// Position the watermark image
watermark.setWrapType(WrapType.NONE);
watermark.setBehindText(true);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
//watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
//watermark.setVerticalAlignment(VerticalAlignment.CENTER);
return watermark;
}
Also tried using bookmarks to set background image.
But its always points to first page.
// using bookmark
InsertWatermarkImageAtEachPage(doc, "watermark1.png");
public static void InsertWatermarkImageAtEachPage (Document doc, String watermarkImagePath) throws Exception
{
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 1;
for (Section section : doc.getSections())
{
NodeCollection paragraphs = section.getBody().getChildNodes(NodeType.PARAGRAPH, true);
for (Iterator it = paragraphs.iterator(); it.hasNext();) {
Paragraph para = (Paragraph)it.next();
if (collector.getStartPageIndex(para) == pageIndex)
{
builder.moveToParagraph(collector.getStartPageIndex(para), 0);
builder.startBookmark("BM_Page" + pageIndex);
builder.endBookmark("BM_Page" + pageIndex);
pageIndex++;
}
}
}
collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
int PageRelativeY = 50;
int PageRelativeX = 50;
int pageind=0;
Paragraph para;
for (Bookmark bookmark : doc.getRange().getBookmarks())
{
if (bookmark.getName().startsWith("BM_"))
{
para = (Paragraph)bookmark.getBookmarkStart().getParentNode();
DocumentBuilder builder1 = new DocumentBuilder(doc);
Shape watermark = new Shape(doc, ShapeType.IMAGE);
builder1.moveToBookmark(bookmark.getName(),true,true);
if(bookmark.getName().equals("BM_Page2")){
builder1.moveToBookmark("BM_Page2");
watermark.setTop(70.0);
watermark.setLeft(24.0);
watermark.getImageData().setImage("D:\\JavaApplication1\\src\\watermark2.png");
}
else {
builder1.moveToBookmark("BM_Page1");
watermark.setTop(0.0);
watermark.setLeft(0.0);
watermark.getImageData().setImage("D:\\JavaApplication1\\src\\watermark1.png");
}
//Set the width height according to your requirements
watermark.setWidth(558.0);
// watermark.setHeight(625);
watermark.setBehindText(true);
builder1.insertNode(watermark);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
Boolean isInCell = bookmark.getBookmarkStart().getAncestor(NodeType.CELL) != null;
}
pageind++;
}
}