Hi,
We have been using Aspose PDF to generate online reports through our web application. However, lately we are getting OutofMemory exception and as per IBM the culprit is Aspose PDF object.
Any help to optimize the code would be helpful
Here is the code snippet.
private Pdf getPdfFinnoneExportReport(FinnoneExportReportOut fout) {
Row header = null;
Row row2 = null;
Row row22 = null;
Table table1 = null;
Table maintable=null;
Table casedesctable = null;
Pdf pdf1 =null;
Section sec1 =null;
try
{
//Create PDF document
pdf1 = new Pdf();
pdf1.setIsLandscape(true);
sec1 = pdf1.getSections().add();
//Section and Table Margin
MarginInfo marginInfo = new MarginInfo();
marginInfo.setTop(2);
marginInfo.setBottom(0);
marginInfo.setLeft(5);
marginInfo.setRight(5);
sec1.getPageInfo().setMargin(marginInfo);
MarginInfo marginInfo1 = new MarginInfo();
marginInfo1.setTop(2);
marginInfo1.setBottom(0);
marginInfo1.setLeft(0);
marginInfo1.setRight(0);
Text text = null;
Text textBlanks = new Text(sec1, "");
//===================================================================//
// Header Footer to show Page Number and Header Data
//===================================================================//
HeaderFooter footer = new HeaderFooter(sec1);
HeaderFooter headers = new HeaderFooter(sec1);
sec1.setOddFooter(footer);
sec1.setEvenFooter(footer);
sec1.setOddHeader(headers);
sec1.setEvenHeader(headers);
Text t1 = new Text(footer, "Page: $p of $P #$NL #$NP ");
t1.getTextInfo().setIsTrueTypeFontBold(true);
t1.getTextInfo().setFontName("Arial");
t1.getTextInfo().setFontSize(10);
t1.getTextInfo().setAlignment(AlignmentType.Center);
casedesctable = new Table(sec1);
casedesctable.setDefaultCellPadding(marginInfo);
footer.getParagraphs().add(casedesctable);
casedesctable.setColumnWidths("139 556 139");
casedesctable.setBorder(new BorderInfo(BorderSide.Bottom.getValue(), 1F));
casedesctable.setBorder(new BorderInfo(BorderSide.Top.getValue(), 1F));
header = casedesctable.getRows().add();
header.getCells().add().getParagraphs().add(textBlanks);
header.getCells().add().getParagraphs().add(t1);
header.getCells().add().getParagraphs().add(textBlanks);
header.getCells().getCell(0).setAlignment(AlignmentType.Left);
header.getCells().getCell(1).setAlignment(AlignmentType.Left);
header.getCells().getCell(2).setAlignment(AlignmentType.Left);
casedesctable.setBorder(new BorderInfo(BorderSide.Bottom.getValue(), 1F));
casedesctable.setBorder(new BorderInfo(BorderSide.Top.getValue(), 1F));
Text headerTest1 = new Text(headers, "XXX Application\n");
headerTest1.getTextInfo().setIsTrueTypeFontBold(true);
headerTest1.getTextInfo().setFontName("Arial");
headerTest1.getTextInfo().setFontSize(14);
// date on which report is generated --- right aligned
DateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date date1 = new Date();
String currDate1 = dateFormat1.format(date1);
Text headerDate = new Text(headers, currDate1+"\n");
headerDate.getTextInfo().setIsTrueTypeFontBold(false);
headerDate.getTextInfo().setFontName("Arial");
headerDate.getTextInfo().setFontSize(10);
headerDate.getTextInfo().setAlignment(AlignmentType.Right);
//add table and add CUR Application header and date in a single row in header
table1 = new Table(sec1);
headers.getParagraphs().add(table1);
table1.setDefaultCellPadding(marginInfo1);
table1.setColumnWidths("140 80 200 200 70 140");
table1.setDefaultCellBorder(new BorderInfo(BorderSide.All.getValue(), 0F));
table1.setBorder(new BorderInfo(BorderSide.All.getValue(), 0F));
header = table1.getRows().add();
header.getCells().add().getParagraphs().add(headerTest1);
header.getCells().add().getParagraphs().add(textBlanks);
header.getCells().add().getParagraphs().add(textBlanks);
header.getCells().add().getParagraphs().add(textBlanks);
header.getCells().add().getParagraphs().add(textBlanks);
header.getCells().add().getParagraphs().add(headerDate);
header.getCells().getCell(0).setAlignment(AlignmentType.Right);
header.getCells().getCell(1).setAlignment(AlignmentType.Left);
header.getCells().getCell(2).setAlignment(AlignmentType.Left);
header.getCells().getCell(3).setAlignment(AlignmentType.Left);
header.getCells().getCell(4).setAlignment(AlignmentType.Left);
header.getCells().getCell(5).setAlignment(AlignmentType.Right);
headerTest1 = new Text(headers, "\nPending Case Details\n\n");
headerTest1.getTextInfo().setIsTrueTypeFontBold(true);
headerTest1.getTextInfo().setFontName("Arial");
headerTest1.getTextInfo().setFontSize(14);
table1 = new Table(sec1);
headers.getParagraphs().add(table1);
table1.setDefaultCellPadding(marginInfo1);
table1.setColumnWidths("140 80 400 70 140");
row2 = table1.getRows().add();
row2.getCells().add().getParagraphs().add(textBlanks);
row2.getCells().add().getParagraphs().add(textBlanks);
row2.getCells().add().getParagraphs().add(headerTest1);
row2.getCells().add().getParagraphs().add(textBlanks);
row2.getCells().add().getParagraphs().add(textBlanks);
row2.getCells().getCell(0).setAlignment(AlignmentType.Left);
row2.getCells().getCell(1).setAlignment(AlignmentType.Left);
row2.getCells().getCell(2).setAlignment(AlignmentType.Center);
row2.getCells().getCell(3).setAlignment(AlignmentType.Left);
row2.getCells().getCell(4).setAlignment(AlignmentType.Left);
table1.setBorder(new BorderInfo(BorderSide.Bottom.getValue(), 1F));
table1.setBorder(new BorderInfo(BorderSide.Top.getValue(), 1F));
//adding case data in report
maintable = new Table(sec1);
maintable.setTop(100F);
maintable.setDefaultCellPadding(marginInfo);
sec1.getParagraphs().add(maintable);
maintable.setColumnWidths("93.55 93.55 93.55 93.55 93.55 93.55 93.55 93.55 93.55");
String str = new String(fout.filecontent, "UTF-8");
List<String> dataList = Arrays.asList(str.split(lineSep));
int headValue=0;
String headDesc = null;
for(String s:dataList)
{
List<String> dataList1 = Arrays.asList(s.split(","));
row2 = maintable.getRows().add();
int i=0;
if(headValue==0)
{
headDesc=s;
for (String s1:dataList1)
{
text = new Text(sec1,"\n\n\n\n\n");
row2.getCells().add().getParagraphs().add(text);
text.getTextInfo().setIsTrueTypeFontBold(true);
text.getTextInfo().setFontName("Arial");
text.getTextInfo().setFontSize(9);
row2.getCells().getCell(i++).setAlignment(AlignmentType.Left);
}
headValue++;
}
else
{
for (String s1:dataList1)
{
text = new Text(sec1, s1+"\n");
row2.getCells().add().getParagraphs().add(text);
text.getTextInfo().setIsTrueTypeFontBold(false);
text.getTextInfo().setFontName("Arial");
text.getTextInfo().setFontSize(9);
row2.getCells().getCell(i++).setAlignment(AlignmentType.Center);
}
}
}
//adding End of Report at the end
casedesctable = new Table(sec1);
casedesctable.setDefaultCellPadding(marginInfo);
sec1.getParagraphs().add(casedesctable);
casedesctable.setColumnWidths("421 421");
casedesctable.setBorder(new BorderInfo(BorderSide.Bottom.getValue(), 1F));
casedesctable.setBorder(new BorderInfo(BorderSide.Top.getValue(), 1F));
Text textBlank = new Text(sec1, "\n");
header = casedesctable.getRows().add();
header.getCells().add().getParagraphs().add(textBlank);
header.getCells().add().getParagraphs().add(textBlank);
header.getCells().getCell(0).setAlignment(AlignmentType.Left);
header.getCells().getCell(1).setAlignment(AlignmentType.Left);
text = new Text(sec1, "\n\nEnd Of Report\n\n");
text.getTextInfo().setIsTrueTypeFontBold(true);
text.getTextInfo().setFontName("Arial");
text.getTextInfo().setFontSize(10);
text.getTextInfo().setAlignment(AlignmentType.Center);
sec1.getParagraphs().add(text);
//adding headers of maintable in pdf header
List<String> dataList2 = Arrays.asList(headDesc.split(","));
int j=0;
Table maintables = new Table(sec1);
maintables.setDefaultCellPadding(marginInfo);
headers.getParagraphs().add(maintables);
maintables.setColumnWidths("93.55 93.55 93.55 93.55 93.55 93.55 93.55 93.55 93.55");
maintables.setBorder(new BorderInfo(BorderSide.Bottom.getValue(), 1F));
maintables.setBorder(new BorderInfo(BorderSide.Top.getValue(), 1F));
row22 = maintables.getRows().add();
for (String s1:dataList2)
{
text = new Text(headers, s1+"\n");
row22.getCells().add().getParagraphs().add(text);
text.getTextInfo().setIsTrueTypeFontBold(true);
text.getTextInfo().setFontName("Arial");
text.getTextInfo().setFontSize(9);
row22.getCells().getCell(j++).setAlignment(AlignmentType.Center);
}
System.out.println("PDF generation completed.");
}
catch(Exception e)
{
e.printStackTrace();
}
return pdf1;
}