NullPointerException Add TextStamp to pdf

Hi there,


i got below error when add text watermark to pdf (only 1 page). it is works for multiple pages.
And attached my testing pdf file.

aspose.pdf version: aspose-pdf-9.5.2-jdk16.jar
OS: Windows 7

java.lang.NullPointerException
at com.aspose.pdf.internal.p427.z28.m9(Unknown Source)
at com.aspose.pdf.internal.p427.z28.m2(Unknown Source)
at com.aspose.pdf.internal.p427.z28.m1(Unknown Source)
at com.aspose.pdf.PageCollection.getUnrestricted(Unknown Source)
at com.aspose.pdf.PageCollection.m1(Unknown Source)
at com.aspose.pdf.PageCollection.get_Item(Unknown Source)



source code snippet:
----------------------------
com.aspose.pdf.Document pdfDocument = (com.aspose.pdf.Document)obj;
com.aspose.pdf.TextStamp textStamp = new com.aspose.pdf.TextStamp(watermarkText);
textStamp.setWidth(500);
textStamp.setHeight(100);
textStamp.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);
textStamp.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);
textStamp.setRotate(-40);
new com.aspose.pdf.FontRepository();
textStamp.getTextState().setFont(FontRepository.findFont(“Arial”));
textStamp.getTextState().setFontSize(50.0F);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
textStamp.getTextState().setForegroundColor(com.aspose.pdf.Color.getDarkGray());
// iterate through all pages of PDF file
int pageSize = pdfDocument.getPages().size();
if(pageSize >= 1){
for (int page_counter = 1; page_counter <=pageSize; page_counter++)
{
//add stamp to all pages of PDF file
com.aspose.pdf.PageCollection pages = pdfDocument.getPages();
if(null != pages){
com.aspose.pdf.Page page = pages.get_Item(page_counter);
if(null != page){
page.addStamp(textStamp);
}
}
}
}

Hi Su,


Thanks for using our API’s.

I have tested the scenario using Aspose.Pdf for Java 10.4.0 in Eclipse Juno project with JDK 1.7 and I am unable to notice any issue. Watermark is properly being added to PDF file. For your reference, I have also attached the PDF file generated over my end.

Hi Nayyer,


i just tried Aspose.pdf for java 10.4.0 with JDK 1.6, same error.
Can you use my source snippet to reproduce this error? i need to confirm whether caused by code issue.

Hi,


After many times testing, i found the reason, but still cannot find out root cause. please advise ASAP.

i using below code applying Aspose license for multiple Aspose products.
when i comment pdfLicense section, means use evaluation version. then test again, no error anymore.
but when i comment pdfLicense section off, means applying license for aspose.pdf. then test again, this error appeared.

--------------------------------------------------------------------------------------------------------------
InputStream pdfStream = this.getClass().getClassLoader().getResourceAsStream(“licences/Aspose.Total.Java.lic”);
com.aspose.pdf.License pdfLicense = new com.aspose.pdf.License();
pdfLicense.setLicense(pdfStream);
pdfStream.close();

InputStream wordStream = this.getClass().getClassLoader().getResourceAsStream(“licences/Aspose.Total.Java.lic”);
com.aspose.words.License wordLicense = new com.aspose.words.License();
wordLicense.setLicense(wordStream);
wordStream.close();
InputStream excelStream = this.getClass().getClassLoader().getResourceAsStream(“licences/Aspose.Total.Java.lic”);
com.aspose.cells.License excelLicense = new com.aspose.cells.License();
excelLicense.setLicense(excelStream);
excelStream.close();
InputStream slideStream = this.getClass().getClassLoader().getResourceAsStream(“licences/Aspose.Total.Java.lic”);
com.aspose.slides.License slideLicense = new com.aspose.slides.License();
slideLicense.setLicense(slideStream);
slideStream.close();

sudongpo1:
Hi Nayyer,

i just tried Aspose.pdf for java 10.4.0 with JDK 1.6, same error.
Can you use my source snippet to reproduce this error? i need to confirm whether caused by code issue.
Hi Su,

Sorry for the delayed response.

I have used exactly the same code snippet which you have shared in first post.

[Java]

com.aspose.pdf.Document pdfDocument = new
com.aspose.pdf.Document();<o:p></o:p>

pdfDocument.getPages().add();

com.aspose.pdf.TextStamp textStamp = new com.aspose.pdf.TextStamp("watermarkText");

textStamp.setWidth(500);

textStamp.setHeight(100);

textStamp.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);

textStamp.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);

textStamp.setRotate(-40);

new com.aspose.pdf.FontRepository();

textStamp.getTextState().setFont(com.aspose.pdf.FontRepository.findFont("Arial"));

textStamp.getTextState().setFontSize(50.0F);

textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);

textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);

textStamp.getTextState().setForegroundColor(com.aspose.pdf.Color.getDarkGray());

// iterate through all pages of PDF file

int pageSize = pdfDocument.getPages().size();

if(pageSize >= 1)

{

for (int page_counter = 1; page_counter <=pageSize; page_counter++)

{

//add stamp to all pages of PDF file

com.aspose.pdf.PageCollection pages = pdfDocument.getPages();

if(null != pages){

com.aspose.pdf.Page page = pages.get_Item(page_counter);

if(null != page){

page.addStamp(textStamp);

}

}

}

}

pdfDocument.save("c:/pdftest/Watermark_Test.pdf");

Hi Su,


During my testing, I have used a valid Aspose.Pdf for Java license during the PDF file generation. However please share where you are trying to initialize your license i.e. is in the beginning of code or you are initializing the license for multiple times in a single program.

Meanwhile you may consider sharing the license file, so that we can test the scenario and determine if the problem is pertaining to particular license file. In order to share the license file, please follow the instructions specified over How to send a license?

Hi Nayyer,


Sorry for reply late.
i have send to you my licence file via email.
And i believe we initialized licence one time whiling web server start up.
Please use my licence and testing again to add text watermark for single page PDF file.

thanks.

Hi Su,


Thanks for sharing the resource file.

I have tested the scenario while using the license file which you have shared and I am unable to notice any issue. Please share some further details so that we can again try replicating the issue in our environment.