OutOfMemory Error while inserting image in PDF

Hi ,

I am getting OutOfMemory Error when I am trying to insert image in PDF, I am using aspose.pdf (java) version 3.1.0.

This happens when I try to insert table with data in one section and image in another section of pdf.
It generally happens when size of image is higher than particular value lets say 1 MB (it works fine for 200kb to 350 kb images)

Code snippet-
Section imageSection = pdf.getSections().add();
ImageInfo imgInfo = new ImageInfo();
imgInfo.setFile(imgUrl);
imgInfo.setFixWidth(imageSection.getPageInfo().getPageWidth()-10);
imgInfo.setAlignment(AlignmentType.Center);
aspose.pdf.Image pdfImg = new aspose.pdf.Image();
pdfImg.setImageInfo(imgInfo);
imageSection.getParagraphs().add(pdfImg);
Exception-

Aab.a(InputStream, Aac, Aab) line: 351
Aab.a(InputStream) line: 96
Aab.(InputStream) line: 83
Aab.(URL) line: 174
Abi.a(String, String, Abc, boolean, boolean) line: 222
Abi.a(Aba, int, ImageFileType, String, String, String, int, int, boolean, boolean, boolean, boolean, byte[], BasicStream) line: 93
Abi.a(Aba, int, ImageFileType, String, String, String, int, int, boolean, boolean, boolean, boolean, byte[], BasicStream, BufferedImage) line: 182
Aar(Aba).a(Image, ImageFileType, String, String, String, int, int, boolean, boolean, byte[], BasicStream, BufferedImage) line: 156
Abi.a(Pdf, Image) line: 212
Abi.a(Pdf, Section, HeaderFooter, Table, Row, Cell, Image, Aab, Abw, boolean) line: 618
Abj.a(Pdf, Section, HeaderFooter, Table, Row, Cell, Image, Aab, Abw, boolean) line: 578
Acd.a(Pdf, Section, Aab) line: 804
Aar.a(Pdf) line: 104
Aba.a(Aar, Pdf) line: 113
Pdf.save(BasicStream) line: 1172
Pdf.save(OutputStream) line: 1118

Regards,
Ankit

Hi Ankit,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for Java 3.1.0 over Windows 7(X64) where I have used JDK 1.7 and I am unable to notice any issue. I have used a sample Image file of 1.20MB. Can you please share the image file causing this issue so that we can test the scenario at our end. We are sorry for this inconvenience.

Hi Nayyer ,


Thanks for quick reply.

Please try creating table with few columns and some 10 to 12 data rows in one section
and create another section to insert this image .
I am using png image (is it a issue with png file)
PFA png image.


Regards,
Ankit

Hi Ankit,


Thanks for sharing the resource file.

I have tested the scenario using Aspose.Pdf for Java 3.1.0 while using following code snippet in Eclipse Juno project running over Windows 7(X64) and I have JDK 1.7 installed over my machine and during my testing, I am unable to notice any issue. For your reference, I have also attached the resultant PDF file being generated over my end. Please take a look.

Please share some further details which can help us in replicating this issue at our end. We are sorry for this inconvenience.

[Java]

Pdf pdf = new Pdf();<o:p></o:p>

//create a Section object and add it to PDF document

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

// create a sample text object as heading for PDF document

Text text = new Text(sec1, "This Demo shows how to create simple Table!\n");

// specify that heading should be Bold

text.getTextInfo().setIsTrueTypeFontBold(true);

// set the font face information as Arial

text.getTextInfo().setFontName("Arial");

// set the font size for text object

text.getTextInfo().setFontSize(14);

// specify font color information as Green

text.getTextInfo().setColor(new aspose.pdf.Color("Green"));

// add the text object to paragraphs collection of section

sec1.getParagraphs().add(text);

// ======= In following code we will create a Table =======//

// create a new Table object

Table Table1 = new Table(sec1);

// specify the Column width information for Table object

// Table1.setColumnWidths("70 70");

// specify the Border information for Table

Table1.setDefaultCellBorder(new BorderInfo(BorderSide.All.getValue(), 0.9F, new aspose.pdf.Color("Black")));

// Set the BackGround color information for Tabel as Yellow

Table1.setBackgroundColor(new aspose.pdf.Color("White"));

Table1.setIsFirstRowRepeated(true);

Table1.setIsVerticalBroken(true);

int rowCount = 0;

for (int i = 1; i < 100; i++)

{

Row row21 = Table1.getRows().add();

row21.setFixedRowHeight(15);

for (int j = 0; j < 10; j++)

{

row21.getCells().add("Col" + rowCount + j);

}

// add a cell to first row of Table

rowCount++;

}

// add table to paragraphs collection of section object

sec1.getParagraphs().add(Table1);

sec1.getPageInfo().setPageHeight(PageSize.A5Height);

MarginInfo info = new MarginInfo();

info.Left = 60;

info.Right = 20;

info.Top = 30;

info.Bottom = 20;

sec1.getPageInfo().setMargin(info);

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

ImageInfo imgInfo = new ImageInfo();

imgInfo.setFile("c:/pdftest/Map.png");

imgInfo.setFixWidth(imageSection.getPageInfo().getPageWidth() - 10);

imgInfo.setAlignment(AlignmentType.Center);

aspose.pdf.Image pdfImg = new aspose.pdf.Image();

pdfImg.setImageInfo(imgInfo);

imageSection.getParagraphs().add(pdfImg);

//Save the resultant PDF

pdf.save(“c:\pdftest\ResultantTable.pdf”);

Hi Nayyer,

I tried same piece of code with same image on my machine as well.

And I am able to generate PDF.

But when I just replaced image path with url of image deployed on local tomcat(same image ) I am able to reproduce this issue.

only below part is changed

imgInfo.setFile(“http://localhost:84/img/Map.png”);

Is there any way to avoid this issue.

Although when I tried with different image (imag is attached)which is also deployed on tomcat

imgInfo.setFile(“http://localhost:84/img/google-map-large.jpg”);

I was able to generate pdf.

Exception-

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space

at aspose.pdf.figure.Aab.a(SourceFile:351)

at aspose.pdf.figure.Aab.a(SourceFile:96)

at aspose.pdf.figure.Aab.(SourceFile:83)

at aspose.pdf.figure.Aab.(SourceFile:174)

at aspose.pdf.figure.Abi.a(SourceFile:222)

at aspose.pdf.figure.Abi.a(SourceFile:93)

at aspose.pdf.figure.Abi.a(SourceFile:182)

at aspose.pdf.figure.Aba.a(SourceFile:156)

at aspose.pdf.xml.Abi.a(SourceFile:212)

at aspose.pdf.xml.Abi.a(SourceFile:618)

at aspose.pdf.xml.Abj.a(SourceFile:578)

at aspose.pdf.xml.Acd.a(SourceFile:804)

at aspose.pdf.xml.Aar.a(SourceFile:104)

at aspose.pdf.xml.Aba.a(SourceFile:113)

at aspose.pdf.Pdf.save(SourceFile:929)

at examples.samples.table.MapExportTest.main(MapExportTest.java:78)

Regards,

Ankit

Hi Ankit,


Thanks for sharing the details. I am testing this scenario and will get back to you soon.