Getting null pointer exceptions when merging 2 pdfs using Aspose Java

Hi,

I am working on a prototype and trying merge 2 pdfs however I am not able to proceed with the following exception.

java.lang.NullPointerException

at com.aspose.pdf.a.g.a.e.a.a.a(Unknown Source)

at com.aspose.pdf.a.g.a.e.a.b.e(Unknown Source)

at com.aspose.pdf.a.g.a.e.a.b.a(Unknown Source)

Note : This issue is coming 6 out of 10 times when I run the program. Not sure why its working remaining 4 times. I am using trail version of the license.

FormattedText ft = new FormattedText("Page # ",

new Color(0, 0, 100), FontStyle.CourierBold,

EncodingType.Winansi, false, 10);

fileStamp.addPageNumber(ft); - This is the place where I see the erros throwing.

Quick help will be much appreciated!!

Thanks..

Hi Sateesh,


Thanks for your inquiry. Please share your sample code and problematic PDF files, so we will test the scenario at our end and will provide you information accordingly. Please also confirm your workflow, you are adding stamp to PDF files and later merging these files?

Sateeshn:


Note : This issue is coming 6 out of 10 times when I run the program. Not sure why its working remaining 4 times. I am using trail version of the license.

Moreover, please clear do you mean 30 days temporary license by trail version of the license?

We are truly sorry for the inconvenience caused.

Best Regards,

Thanks for the reply. Yes, I am using 30 days trail version.

Also I: would like to mentione here sometimes it is getting failed to merge the pdfs also. This is intermittent, can you please take a look ?

I am attaching file here. Please suggest us if we need to modify anything.

Thanks.

Hi Sateesh,


Thanks for sharing the code snippet.

I have tested the scenario using Aspose.Pdf for Java 4.5.0 in Eclipse Juno application running over Windows 7(x64) where I have used two of sample PDF files present over my system and I am unable to notice any problem. I have used the same code snippet which you have shared in your above post.

Can you please share the PDF files causing this problem so that we can again try replicating this issue in our environment. We are sorry for your inconvenience.

Hi Nayyer,

I am using RAD 7.5 on Windows 7 (32). This is an intermittent error, I am getting this 6 out of 10 times. I am using Aspose for Java pdf version is "aspose-pdf-4.5.1".

I am using just a text docs to merge for testing, that does not have any customised settings.

Thanks.

Hi Sateesh,


Thanks for your sharing addition information. We are investigating the issue with RAD IDE and will share the results ASAP.

Meanwhile, you may please try DOM approach to concatenate/stamp PDF documents and share the results.


We are sorry for the inconvenience caused.

Best Regards,

Hi Tilal,

Thanks for the new source. We still getting some exceptions while adding the PageNumberStamp, it is throwing null pointer exceptions and not able to proceed. Not sure if the problem with the Jars downloaded from the Aspose site. Please suggest us if you have any updated jars that can help us better.

com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document(inFile1);

// Open the source document

com.aspose.pdf.Document pdfDocument2 = new com.aspose.pdf.Document(inFile2);

// Add the pages of the source document to the target document

pdfDocument1.getPages().add(pdfDocument2.getPages());

// Save the concatenated output file (the target document)

pdfDocument1.save(outFile);

System.out.println("Successful...");

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(outFile);

//create page number stamp

com.aspose.pdf.PageNumberStamp pageNumberStamp = new com.aspose.pdf.PageNumberStamp();

//whether the stamp is background

pageNumberStamp.setBackground(false);

pageNumberStamp.setFormat("Page # of " + pdfDocument.getPages().size());

pageNumberStamp.setBottomMargin(10);

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

pageNumberStamp.setStartingNumber(1);

//set text properties

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

pageNumberStamp.getTextState().setFontSize(14.0F);

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

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

pageNumberStamp.getTextState().setForegroundColor(java.awt.Color.BLUE);

//add stamp to particular page

pdfDocument.getPages().get_Item(1).addStamp(pageNumberStamp); -- this is where I am getting the below exception.

//save output document

pdfDocument.save(outFile1);

Exception :

java.lang.NullPointerException

at com.aspose.pdf.TextStamp.a(Unknown Source)

at com.aspose.pdf.TextStamp.put(Unknown Source)

at com.aspose.pdf.PageNumberStamp.put(Unknown Source)

at com.aspose.pdf.Page.addStamp(Unknown Source)

Thanks.

Sateesh

Hi Tilal,

We are also working on the Table of conent and we see some compiling errors, looks like this jar( aspose-pdf-jdk16.jar) is not supported for Table of Contents and not able to find any API.

tocsec.getListFormatArray().getListLevelFormat(0).setLeftMargin(0); -- Getting errors for this method.

This is the same code that we have got it from the samples from the site. Let us know if you have any other Jars available for this.

Sateeshn:

Hi Tilal,

Thanks for the new source. We still getting some exceptions while adding the PageNumberStamp, it is throwing null pointer exceptions and not able to proceed. Not sure if the problem with the Jars downloaded from the Aspose site. Please suggest us if you have any updated jars that can help us better.

com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document(inFile1);

// Open the source document

com.aspose.pdf.Document pdfDocument2 = new com.aspose.pdf.Document(inFile2);

// Add the pages of the source document to the target document

pdfDocument1.getPages().add(pdfDocument2.getPages());

// Save the concatenated output file (the target document)

pdfDocument1.save(outFile);

System.out.println("Successful...");

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(outFile);

//create page number stamp

com.aspose.pdf.PageNumberStamp pageNumberStamp = new com.aspose.pdf.PageNumberStamp();

//whether the stamp is background

pageNumberStamp.setBackground(false);

pageNumberStamp.setFormat("Page # of " + pdfDocument.getPages().size());

pageNumberStamp.setBottomMargin(10);

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

pageNumberStamp.setStartingNumber(1);

//set text properties

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

pageNumberStamp.getTextState().setFontSize(14.0F);

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

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

pageNumberStamp.getTextState().setForegroundColor(java.awt.Color.BLUE);

//add stamp to particular page

pdfDocument.getPages().get_Item(1).addStamp(pageNumberStamp); -- this is where I am getting the below exception.

//save output document

pdfDocument.save(outFile1);

Exception :

java.lang.NullPointerException

at com.aspose.pdf.TextStamp.a(Unknown Source)

at com.aspose.pdf.TextStamp.put(Unknown Source)

at com.aspose.pdf.PageNumberStamp.put(Unknown Source)

at com.aspose.pdf.Page.addStamp(Unknown Source)

Hi Sateesh,

I have tested the scenario and I am able to notice the same problem. For the sake of correction, I have logged this problem as PDFNEWJAV-34007 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Sateeshn:
We are also working on the Table of conent and we see some compiling errors, looks like this jar( aspose-pdf-jdk16.jar) is not supported for Table of Contents and not able to find any API.

tocsec.getListFormatArray().getListLevelFormat(0).setLeftMargin(0); – Getting errors for this method.

This is the same code that we have got it from the samples from the site. Let us know if you have any other Jars available for this.

Hi Sateesh,

Thanks for contacting support.

Please share the complete code snippet or point us to the location from where you have got this code, so that we can test this scenario in our environment. We are sorry for this inconvenience.

Hi,

Do you have any luck by chance on this issue on this issue ?

Hi

Do you have any luck by chance ?

Hi Nayyar,

Sorry for responding late, here is Table of Content code we are trying.

String outFile = "C:/Test.pdf";

Pdf pdf = new Pdf();

com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document(outFile);

//Create a list section

ListSection tocsec = new ListSection("Table of contents");

//Add the list section to the sections collection of the Pdf document

pdf.getSections().add(tocsec);

// Get the Text value of the title of ListSection.

Text text = tocsec.getTitle();

// set the font name for TOC title text.getSegments().getSegment(0).getTextInfo().setFontName("Helvetica");

// set the font size information for TOC title

text.getSegments().getSegment(0).getTextInfo().setFontSize(16);

// set the Bottom margin information for TOC title

text.getMargin().setBottom(20);

//Set the length of list levels to 4

tocsec.getListFormatArray().setLength(4);

// Set left margin information for first level list item

tocsec.getListFormatArray().getListLevelFormat(0).setLeftMargin(0);

// Set Font Name information for first level list item

tocsec.getListFormatArray().getListLevelFormat(0).getTextInfo().setFontName("Helvetica");

// Set Font Size information for first level list item

tocsec.getListFormatArray().getListLevelFormat(0).getTextInfo().setFontSize(24);

// Set left margin information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).setLeftMargin(5);

// Set Font Name information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).getTextInfo().setFontSize(20);

// Set Font color information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).getTextInfo().setColor(new aspose.pdf.Color("Red"));

// Set left margin information for Third level list item

tocsec.getListFormatArray().getListLevelFormat(2).setLeftMargin(10);

// Set Font Name information for Third level list item

tocsec.getListFormatArray().getListLevelFormat(2).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Third level list item

tocsec.getListFormatArray().getListLevelFormat(2).getTextInfo().setFontSize(14);

//tocsec.getListFormatArray().getListLevelFormat(2).getTextInfo().setIsTrueTypeFontBold(true);

// Set left margin information for Fourth level list item

tocsec.getListFormatArray().getListLevelFormat(3).setLeftMargin(15);

// Set Font Name information for Fourth level list item

tocsec.getListFormatArray().getListLevelFormat(3).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Fourth level list item

tocsec.getListFormatArray().getListLevelFormat(3).getTextInfo().setFontSize(10);

//tocsec.getListFormatArray().getListLevelFormat(3).getTextInfo().setIsTrueTypeFontBold(true);

//Set its list type as table of of contents

tocsec.setListType(ListType.TableOfContents);

// Create a section in the Pdf document

Section sec = pdfDocument1.getSections().add();

//Add four headings in the section

for (int level = 1; level != 5; level++)

{

// Create a heading object with heading level according to loop iterative variable

Heading heading2 = new Heading(pdfDocument1, sec, level);

// Create a segment object and pass heading object as argument

Segment seg2 = new Segment(heading2);

// add the segment to segments collection of heading object

heading2.getSegments().add(seg2);

//set the value that indicates that the number of the heading is added in automatic sequence

heading2.setIsAutoSequence(true);

// set the content value for segment object

seg2.setContent("this is heading of level");

// Specify that the heading paragraph is used as a list item

heading2.setIsInList(true);

// add heading to paragraphs collection of section

sec.getParagraphs().add(heading2);

}

//Save the resultant PDF

pdf.save("D:Resultant.pdf");

I am getting compilation error for this method : getListLevelFormat(X)

Thanks

Sateesh

Hi Sateesh,


We are working on testing this scenario and will get back to you soon.

Sateeshn:
We are also working on the Table of conent and we see some compiling errors, looks like this jar( aspose-pdf-jdk16.jar) is not supported for Table of Contents and not able to find any API.

tocsec.getListFormatArray().getListLevelFormat(0).setLeftMargin(0); – Getting errors for this method.

This is the same code that we have got it from the samples from the site. Let us know if you have any other Jars available for this.

Hi Sateesh,

Thanks for your patience.

There has been a little change in product API. Instead of using getListLevelFormat(..) method, please try using the following code line.

tocsec.getListFormatArray().get_Item(0).setLeftMargin(0);

Sateeshn:


String outFile = “C:/Test.pdf”;


Pdf pdf = new Pdf();


com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document(outFile);

//Create a list section

ListSection tocsec = new ListSection("Table of contents");

//Add the list section to the sections collection of the Pdf document

pdf.getSections().add(tocsec);

// Get the Text value of the title of ListSection.

Text text = tocsec.getTitle();

// set the font name for TOC title text.getSegments().getSegment(0).getTextInfo().setFontName("Helvetica");

// set the font size information for TOC title

text.getSegments().getSegment(0).getTextInfo().setFontSize(16);

// set the Bottom margin information for TOC title

text.getMargin().setBottom(20);

//Set the length of list levels to 4

tocsec.getListFormatArray().setLength(4);

// Set left margin information for first level list item

tocsec.getListFormatArray().getListLevelFormat(0).setLeftMargin(0);

// Set Font Name information for first level list item

tocsec.getListFormatArray().getListLevelFormat(0).getTextInfo().setFontName("Helvetica");

// Set Font Size information for first level list item

tocsec.getListFormatArray().getListLevelFormat(0).getTextInfo().setFontSize(24);

// Set left margin information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).setLeftMargin(5);

// Set Font Name information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).getTextInfo().setFontSize(20);

// Set Font color information for Second level list item

tocsec.getListFormatArray().getListLevelFormat(1).getTextInfo().setColor(new aspose.pdf.Color("Red"));

// Set left margin information for Third level list item

tocsec.getListFormatArray().getListLevelFormat(2).setLeftMargin(10);

// Set Font Name information for Third level list item

tocsec.getListFormatArray().getListLevelFormat(2).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Third level list item

tocsec.getListFormatArray().getListLevelFormat(2).getTextInfo().setFontSize(14);

//tocsec.getListFormatArray().getListLevelFormat(2).getTextInfo().setIsTrueTypeFontBold(true);

// Set left margin information for Fourth level list item

tocsec.getListFormatArray().getListLevelFormat(3).setLeftMargin(15);

// Set Font Name information for Fourth level list item

tocsec.getListFormatArray().getListLevelFormat(3).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Fourth level list item

tocsec.getListFormatArray().getListLevelFormat(3).getTextInfo().setFontSize(10);

//tocsec.getListFormatArray().getListLevelFormat(3).getTextInfo().setIsTrueTypeFontBold(true);

//Set its list type as table of of contents

tocsec.setListType(ListType.TableOfContents);

// Create a section in the Pdf document

Section sec = pdfDocument1.getSections().add();

//Add four headings in the section

for (int level = 1; level != 5; level++)

{

// Create a heading object with heading level according to loop iterative variable

Heading heading2 = new Heading(pdfDocument1, sec, level);

// Create a segment object and pass heading object as argument

Segment seg2 = new Segment(heading2);

// add the segment to segments collection of heading object

heading2.getSegments().add(seg2);

//set the value that indicates that the number of the heading is added in automatic sequence

heading2.setIsAutoSequence(true);

// set the content value for segment object

seg2.setContent("this is heading of level");

// Specify that the heading paragraph is used as a list item

heading2.setIsInList(true);

// add heading to paragraphs collection of section

sec.getParagraphs().add(heading2);

}

//Save the resultant PDF

pdf.save("D:Resultant.pdf");

I am getting compilation error for this method : getListLevelFormat(X)

Hi Sateesh,

There has been a change in product API and in order to generate TOC inside PDF document, please try using the following code snippet. However I have observed that during PDF generation, StackOverFlowError is occurring. For the sake of correction, I have logged this problem as PDFNEWJAVA-34033 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

[Java]

Pdf pdf = new Pdf();

// com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document(outFile);

//Create a list section

ListSection tocsec = new ListSection("Table of contents");

//Add the list section to the sections collection of the Pdf document

pdf.getSections().add(tocsec);

// Get the Text value of the title of ListSection.

aspose.pdf.Text text = tocsec.getTitle();

// set the font name for TOC title text.getSegments().getSegment(0).getTextInfo().setFontName("Helvetica");

// set the font size information for TOC title

text.getSegments().get_Item(0).getTextInfo().setFontSize(16);

// set the Bottom margin information for TOC title

text.getMargin().setBottom(20);

//Set the length of list levels to 4

tocsec.getListFormatArray().setLength(4);

// Set left margin information for first level list item

tocsec.getListFormatArray().get_Item(0).setLeftMargin(0);

// Set Font Name information for first level list item

tocsec.getListFormatArray().get_Item(0).getTextInfo().setFontName("Helvetica");

// Set Font Size information for first level list item

tocsec.getListFormatArray().get_Item(0).getTextInfo().setFontSize(24);

// Set left margin information for Second level list item

tocsec.getListFormatArray().get_Item(1).setLeftMargin(5);

// Set Font Name information for Second level list item

tocsec.getListFormatArray().get_Item(1).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Second level list item

tocsec.getListFormatArray().get_Item(1).getTextInfo().setFontSize(20);

// Set Font color information for Second level list item

tocsec.getListFormatArray().get_Item(1).getTextInfo().setColor(new aspose.pdf.Color("Red"));

// Set left margin information for Third level list ite

tocsec.getListFormatArray().get_Item(2).setLeftMargin(10);

// Set Font Name information for Third level list item

tocsec.getListFormatArray().get_Item(2).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Third level list item

tocsec.getListFormatArray().get_Item(2).getTextInfo().setFontSize(14);

//tocsec.getListFormatArray().getListLevelFormat(2).getTextInfo().setIsTrueTypeFontBold(true);

// Set left margin information for Fourth level list item

tocsec.getListFormatArray().get_Item(3).setLeftMargin(15);

// Set Font Name information for Fourth level list item

tocsec.getListFormatArray().get_Item(3).getTextInfo().setFontName("Helvetica");

// Set Font Size information for Fourth level list item

tocsec.getListFormatArray().get_Item(3).getTextInfo().setFontSize(10);

//tocsec.getListFormatArray().getListLevelFormat(3).getTextInfo().setIsTrueTypeFontBold(true);

//Set its list type as table of of contents

tocsec.setListType(ListType.TableOfContents);

// Create a section in the Pdf document

Section sec = pdf.getSections().add();

//Add four headings in the section

for (int level = 1; level != 5; level++)

{

// Create a heading object with heading level according to loop iterative variable

Heading heading2 = new Heading(pdf, sec, level);

// Create a segment object and pass heading object as argument

Segment seg2 = new Segment(heading2);

// add the segment to segments collection of heading object

heading2.getSegments().add(seg2);

//set the value that indicates that the number of the heading is added in automatic sequence

heading2.isAutoSequence(true);

// set the content value for segment object

seg2.setContent("this is heading of level");

// Specify that the heading paragraph is used as a list item

heading2.isInList(true);

// add heading to paragraphs collection of section

sec.getParagraphs().add(heading2);

}

//Save the resultant PDF

pdf.save(“c:/pdftest/TOCSample_Resultant.pdf”);

The issues you have found earlier (filed as PDFNEWJAVA-34007) have been fixed in Aspose.Pdf for Java 4.6.0.

Details of what’s New and What’s fixed can be found in [this blog post](https://blog.aspose.com/2014/03/11/svg-to-pdf-optimize-pdf-size-setting-more-than-one-font-path-in-aspose.pdf-for-java-4.6.0)

The issues you have found earlier (filed as PDFNEWJAVA-34033) have been fixed in this update.