I have used TextWatermarkOptions to write watermark in word document. Currently I am using aspose words 23.5 version.
I have added GroupShape which contains multiple shapes. If watermark is added, shape placed at bottom of the page shifts down and data gets cropped.
This doesn’t happen if no watermark is applied.
Why does the shape x,y position shift after adding TextWatermarkOptions watermark?
@aishwarya12joshi Could you please attach your input and output documents along with code that will allow us to reproduce the problem? We will check the issue and provide you more information.
Please run below code once with applying watermark and another by no watermark by commenting the watermark code.
Issue reproduces when I read the license file. Please read license file while running the code.
We could see GroupShape shifts from the top eventually making all the shapes to move down.
public static void main(String[] args) {
try {
License license = new License();
String fileText = readLicenseFile();
if(fileText != null){
InputStream in = IOUtils.toInputStream(fileText, "UTF-8");
license.setLicense(in);
}
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
PageSetup pageSetup = document.getFirstSection().getPageSetup();
pageSetup.setTopMargin(0);
pageSetup.setLeftMargin(0);
pageSetup.setBottomMargin(0);
pageSetup.setRightMargin(0);
GroupShape g1 = new GroupShape(document);
g1.setBounds(new Rectangle2D.Float(0f, 0f, (float) builder.getPageSetup().getPageWidth(),
(float) builder.getPageSetup().getPageHeight()));
g1.setCoordSize(new Dimension((int) builder.getPageSetup().getPageWidth(),
(int) builder.getPageSetup().getPageHeight()));
g1.setCoordOrigin(new Point(0, 0));
document.getLastSection().getBody().getLastParagraph().appendChild(g1);
Shape shape4 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph4 = new Paragraph(document);
Run run4 = new Run(document, "Start");
paragraph4.appendChild(run4);
shape4.appendChild(paragraph4);
shape4.setFillColor(Color.LIGHT_GRAY);
shape4.setLeft(200);
shape4.setTop(10);
shape4.setHeight(40);
shape4.setWidth(100);
g1.appendChild(shape4);
Shape shape = new Shape(document, ShapeType.RECTANGLE);
Table table4 = new Table(document);
for(int i=0; i<20; i++) {
Row row = new Row(document);
table4.appendChild(row);
for(int j=0; j< 3; j++) {
Cell cell = new Cell(document);
row.appendChild(cell);
Paragraph paragraph = new Paragraph(document);
Run run = new Run(document, "Row "+i+" Cell "+j);
Font font = run.getFont();
font.setName("Courier");
font.setSize(12);
paragraph.appendChild(run);
cell.appendChild(paragraph);
}
}
shape.appendChild(table4);
shape.setHeight(300);
shape.setWidth(400);
shape.setTop(60);
shape.setLeft(50);
g1.appendChild(shape);
Shape shape1 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph1 = new Paragraph(document);
Run run1 = new Run(document, "Hello");
paragraph1.appendChild(run1);
shape1.appendChild(paragraph1);
shape1.setFillColor(Color.YELLOW);
shape1.setLeft(400);
shape1.setTop(400);
shape1.setHeight(40);
shape1.setWidth(100);
g1.appendChild(shape1);
Shape shape2 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph2 = new Paragraph(document);
Run run2 = new Run(document, "Everyone");
paragraph2.appendChild(run2);
shape2.appendChild(paragraph2);
shape2.setFillColor(Color.GRAY);
shape2.setLeft(400);
shape2.setTop(560);
shape2.setHeight(40);
shape2.setWidth(100);
g1.appendChild(shape2);
Shape shape3 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph3 = new Paragraph(document);
Run run3 = new Run(document, "End");
paragraph3.appendChild(run3);
shape3.appendChild(paragraph3);
shape3.setFillColor(Color.PINK);
shape3.setLeft(200);
shape3.setTop(660);
shape3.setHeight(40);
shape3.setWidth(100);
g1.appendChild(shape3);
/*TextWatermarkOptions options = new TextWatermarkOptions();
options.setFontFamily("Arial");
options.setFontSize(50);
options.setColor(Color.RED);
options.setLayout(WatermarkLayout.DIAGONAL);
options.isSemitrasparent(true);
document.getWatermark().setText("Watermark Text", options);*/
document.save("D:\\WatermarkIssue.docx");
System.out.println("Done...");
} catch (Exception e) {
e.printStackTrace();
}
}
@aishwarya12joshi This is an expected behavior, you will observe exactly the same if add watermark into your document using MS Word. Watermarks in MS Word documents are added as floating shapes in documents’ headers. Since your document has zero margins and no header/footer initially, position of the shape is on top of the page. but once you add watermark, the document has header, which ocuppy some space on the top. You can use floating shape, in this case it’s position will not be changed:
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
PageSetup pageSetup = document.getFirstSection().getPageSetup();
pageSetup.setTopMargin(0);
pageSetup.setLeftMargin(0);
pageSetup.setBottomMargin(0);
pageSetup.setRightMargin(0);
GroupShape g1 = new GroupShape(document);
g1.setBounds(new Rectangle2D.Float(0f, 0f, (float)builder.getPageSetup().getPageWidth(),
(float)builder.getPageSetup().getPageHeight()));
g1.setCoordSize(new Dimension((int)builder.getPageSetup().getPageWidth(),
(int)builder.getPageSetup().getPageHeight()));
g1.setCoordOrigin(new Point(0, 0));
g1.setWrapType(WrapType.NONE);
g1.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
g1.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
g1.setLeft(0);
g1.setTop(0);
document.getLastSection().getBody().getLastParagraph().appendChild(g1);
Shape shape4 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph4 = new Paragraph(document);
Run run4 = new Run(document, "Start");
paragraph4.appendChild(run4);
shape4.appendChild(paragraph4);
shape4.setFillColor(Color.LIGHT_GRAY);
shape4.setLeft(200);
shape4.setTop(10);
shape4.setHeight(40);
shape4.setWidth(100);
g1.appendChild(shape4);
Shape shape = new Shape(document, ShapeType.RECTANGLE);
Table table4 = new Table(document);
for (int i = 0; i < 20; i++)
{
Row row = new Row(document);
table4.appendChild(row);
for (int j = 0; j < 3; j++)
{
Cell cell = new Cell(document);
row.appendChild(cell);
Paragraph paragraph = new Paragraph(document);
Run run = new Run(document, "Row " + i + " Cell " + j);
Font font = run.getFont();
font.setName("Courier");
font.setSize(12);
paragraph.appendChild(run);
cell.appendChild(paragraph);
}
}
shape.appendChild(table4);
shape.setHeight(300);
shape.setWidth(400);
shape.setTop(60);
shape.setLeft(50);
g1.appendChild(shape);
Shape shape1 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph1 = new Paragraph(document);
Run run1 = new Run(document, "Hello");
paragraph1.appendChild(run1);
shape1.appendChild(paragraph1);
shape1.setFillColor(Color.YELLOW);
shape1.setLeft(400);
shape1.setTop(400);
shape1.setHeight(40);
shape1.setWidth(100);
g1.appendChild(shape1);
Shape shape2 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph2 = new Paragraph(document);
Run run2 = new Run(document, "Everyone");
paragraph2.appendChild(run2);
shape2.appendChild(paragraph2);
shape2.setFillColor(Color.GRAY);
shape2.setLeft(400);
shape2.setTop(560);
shape2.setHeight(40);
shape2.setWidth(100);
g1.appendChild(shape2);
Shape shape3 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph paragraph3 = new Paragraph(document);
Run run3 = new Run(document, "End");
paragraph3.appendChild(run3);
shape3.appendChild(paragraph3);
shape3.setFillColor(Color.PINK);
shape3.setLeft(200);
shape3.setTop(660);
shape3.setHeight(40);
shape3.setWidth(100);
g1.appendChild(shape3);
TextWatermarkOptions options = new TextWatermarkOptions();
options.setFontFamily("Arial");
options.setFontSize(50);
options.setColor(Color.RED);
options.setLayout(WatermarkLayout.DIAGONAL);
options.isSemitrasparent(true);
document.getWatermark().setText("Watermark Text", options);
document.save("C:\\Temp\\out.docx");
This worked when margins are set to zero.
But what if we need page margins as well? In this case page margins are not getting honoured. GroupShape will be created inside page margin.
If I want to set 10pt as page margin then I create GroupShape as
float groupShapeWidth = (float) (pageWidth - 20);
float groupShapeHeight = (float) (pageHeight - 20);
GroupShape g1 = new GroupShape(document);
g1.setBounds(new Rectangle2D.Float(0f, 0f, groupShapeWidth, groupShapeHeight));
g1.setCoordSize(new Dimension((int)groupShapeWidth, (int)groupShapeHeight));
g1.setCoordOrigin(new Point(10, 10));
g1.setWrapType(WrapType.NONE);
g1.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
g1.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
g1.setLeft(10);
g1.setTop(10);
@aishwarya12joshi Yes, in this case relative position of the shape is set to page, so it’s position is shifted from the page edges. You can try using margin relative position, in this case position will be shifted from page margins.
I tried various margin combinations but it didn’t work. The watermark either shifts the shapes or page margins are not honoured.
Tried setting both relative positions as MARGIN
setting both relative positions as INSIDE_MARGIN
setting both relative positions as OUTSIDE_MARGIN
RelativeVerticalPosition.TOP_MARGIN and RelativeHorizontalPosition.LEFT_MARGIN
@aishwarya12joshi As I can see with the following code the group shape is properly shifted:
PageSetup pageSetup = document.getFirstSection().getPageSetup();
pageSetup.setTopMargin(10);
pageSetup.setLeftMargin(10);
pageSetup.setBottomMargin(10);
pageSetup.setRightMargin(10);
float groupShapeWidth = (float)(builder.getPageSetup().getPageWidth() - 20);
float groupShapeHeight = (float)(builder.getPageSetup().getPageHeight() - 20);
GroupShape g1 = new GroupShape(document);
g1.setBounds(new Rectangle2D.Float(0f, 0f, groupShapeWidth, groupShapeHeight));
g1.setCoordSize(new Dimension((int)groupShapeWidth, (int)groupShapeHeight));
g1.setCoordOrigin(new Point(0, 0));
g1.setWrapType(WrapType.NONE);
g1.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
g1.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
g1.setLeft(10);
g1.setTop(10);
document.getLastSection().getBody().getLastParagraph().appendChild(g1);