How to add watermark with rotate 45 to pdf (aspose java)

hi friends,


i have tired to add watermark to pdf but there’re 3 options, on90, on180 and on270. i want to add on45 from left bottom corner to right top corner. is it possible if it how can i.

thanks for all suggest,

Best regards.

Hi,

Thanks for contacting support.

Currently the Rotation enumeration in com.aspose.pdf package does not support 45 degree angle. For the sake of implementation, I have logged this requirement as PDFNEWJAVA-34182 in our issue tracking system. The development team will further look into the details of this requirement and will keep you posted on the status of correction. Please be patient and spare us some time.

However, as a workaround, you may try using com.aspose.pdf.facades package. Please try using the following code snippet to accomplish your requirement.

[Java]

//open document
com.aspose.pdf.facades.PdfFileStamp fileStamp = new com.aspose.pdf.facades.PdfFileStamp();

//get total number of pages
int totalPages = new com.aspose.pdf.facades.PdfFileInfo("c:/pdftest/SpecialCharacters_test2.pdf").getNumberOfPages();

//create formatted text for page number
com.aspose.pdf.facades.FormattedText formattedText = new com.aspose.pdf.facades.FormattedText("Page # Of " + totalPages, java.awt.Color.BLUE, java.awt.Color.GRAY, com.aspose.pdf.facades.FontStyle.Courier, com.aspose.pdf.facades.EncodingType.Winansi, false, 14);

//set starting number for first page; you might want to start from 2 or more
fileStamp.setStartingNumber(1);

//add page number
//                   fileStamp.addPageNumber(formattedText, 0);

com.aspose.pdf.facades.Stamp stamp = new com.aspose.pdf.facades.Stamp();
stamp.bindLogo(formattedText);
stamp.setRotation(45f);
fileStamp.addStamp(stamp);

//save updated PDF file
fileStamp.save("c:/pdftest/Stamped_45_Degree.pdf");
fileStamp.close();

hi,


thanks for reply, i tried it and got successfully result. it useful for us.


thanks a lot,

Best regards.

Hi there,

Thanks for your feedback. It is good to know that your issue has been fixed.

In addition to the above suggested resolution, please note that to rotate a stamp to an arbitrary angle you need to use the setRotateAngle() method in the com.aspose.pdf package as follows.

Document pdfDocument = new Document(myDir + "helloworld.pdf");

 //create image stamp
ImageStamp imageStamp = new ImageStamp(myDir + "aspose_pdf.jpg");
imageStamp.setBackground(true);
imageStamp.setXIndent(100);
imageStamp.setYIndent(100);
imageStamp.setHeight(300);
imageStamp.setWidth(300);
//imageStamp.setRotate(Rotation.on270);
imageStamp.setRotateAngle(45);
imageStamp.setOpacity(0.5);
imageStamp.setStampId(421);
//add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(imageStamp);

//save output document
pdfDocument.save(myDir + "ImageStamp_output45.pdf")

Please feel free to contact us for any further assistance.

Best Regards,

hi Ahmed,


firstly thanks for suggest, i tried both of them. imageStamp not useful for us because we create watermark dynamically, and apply to all pages. in fact we need some properties both of them,

such as formattedText, setStartingNumber(), background(true), setRotation(), setWidth - Height

thanks for all supports,

Best regards.

Hi,


Thanks for sharing the feedback. We are glad to hear that your requirement can be accomplished while using either of the above stated approaches. Please continue using our API and in the event of any further query, please feel free to contact.

hi nayyer.


can you please provide code for this that does the automatic calculation based on the string given? the code above are hard coded coordinates.

i have successfully done it with text watermark in a vertical position. but my calculations are off when it is diagonal

Hi Chit,

Thanks for your inquiry. You may convert your string to float for the purpose, please check following code snippet. However, if there is some difference in your requirement and my understanding then please share some more details along with sample code, we will look into it and will guide you accordingly.

com.aspose.pdf.facades.Stamp stamp = new com.aspose.pdf.facades.Stamp();
stamp.bindLogo(formattedText);
**float** f = Float.**parseFloat**("60");
stamp.setRotation(f);
fileStamp.addStamp(stamp);

Best Regards,

hi tilal. thank you. i sent you email with my code

Hi Chit,


Thanks for your email, but I am afraid the code is incomplete. Please share your sample working code, it will help us to replicate the issue and guide you accordingly.

Best Regards,

Hi Chit,


I am afraid the code is again incomplete. Please share sample code as email attachment instead email body.

We are sorry for the inconvenience.

Best Regards,

Hi Chit,

Thanks for your inquiry. Please note for multiline Text Stamp use addNewLineText property to add new line in FormattedText object instead creating separate stamp for each line, and use Vertical and Horizontal alignment properties of TextStamp to adjust stamp text automatically. Hopefully following code will help you to accomplish the task. However if there is difference in your requirement and my understanding then please share some more details, so we will guide you accordingly.

ArrayList<String> watermark = new ArrayList<>();
watermark.add("Test Line 1");
watermark.add("Test Line 2");
watermark.add("Test Line 3");

Document pdfDocument = new Document();
pdfDocument.getPages().add();

ArrayList<TextStamp> textStamps = new ArrayList<>();

// arrange muliline text stamp text
FormattedText ft = new FormattedText(watermark.get(0), java.awt.Color.LIGHT_GRAY, "Helvetica", EncodingType.Winansi, false, 80);

for (int i = 1; i < watermark.size(); i++) {
    ft.addNewLineText(watermark.get(i));
}

TextStamp stamp = new TextStamp(ft);
stamp.setBackground(true);
stamp.setOpacity(0.5);
stamp.setRotateAngle(-45);
stamp.setTextAlignment(VerticalAlignment.Center);
stamp.setHorizontalAlignment(HorizontalAlignment.Center);
stamp.setVerticalAlignment(VerticalAlignment.Center);

// add stamp to page collection
pdfDocument.getPages().get_Item(1).addStamp(stamp);

pdfDocument.save("test.pdf");

Best Regards,

hi tilal. thank you for the code. it works


however, text is selectable. how to do this?

in my previous code, i called stamp.setDraw(true). however, only the last text gets shown in the page.

Hi Chit,


Thanks for the additional information. I have tested the scenario and noticed that setDraw property hides some contents of text stamp, so logged a ticket PDFJAVA-36390 in our issue tracking system for further investigation and rectification.

We are sorry for the inconvenience.

Best Regards,