Hi,
I have used following code and verified memory optimization and attached the resultant memory graph.
public static void main(String[] args) {
//Instantiate a table object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
page.getPageInfo().setHeight( PageSize.getA4().getHeight() );
page.getPageInfo().setWidth( PageSize.getA4().getWidth() );
MarginInfo info = new MarginInfo( 15, 15, 15, 15 );
page.getPageInfo().setMargin( info );
for( int i = 0 ; i < 100; i++ ) {
Table table = getNestedTable();
page.getParagraphs().add(table);
doc.processParagraphs();
MemoryCleaner.clear();
}
doc.save( tempPath );
}
private static Table getNestedTable() {
Table outermostTable = new Table();
Row outermostrow = outermostTable.getRows().add();
Cell outermostcell = new Cell();
Table rowTable = new Table();
Row rowTableRow = rowTable.getRows().add();
Cell rowTableCell = new Cell();
Table cellTable = new Table();
Row cellTableRow = cellTable.getRows().add();
Cell cellTableCell = new Cell();
Table dataTable = getDataTable();
cellTableCell.getParagraphs().add( dataTable );
cellTableRow.getCells().add( cellTableCell );
rowTableCell.getParagraphs().add( cellTable );
rowTableRow.getCells().add( rowTableCell );
outermostcell.getParagraphs().add( rowTable );
outermostrow.getCells().add( outermostcell );
return outermostTable;
}
private static Table getDataTable() {
Table table = new Table();
for( int i = 0; i < 1000; i++ ) {
Row row = table.getRows().add();
for( int j = 0; j < 5; j++ ){
Cell cell = getCell( i, j );
row.getCells().add(cell);
}
System.out.println("writing "+i);
}
table.setColumnWidths( "100 100 100 100 100 " );
return table;
}
private static Cell getCell( int i, int j ) {
TextSegment segment = new TextSegment( “This is some sample text” );
TextState state = segment.getTextState();
state.setFont( FontRepository.findFont(“Verdana”) );
state.setForegroundColor( Color.getBlack() );
state.setBackgroundColor( Color.getWheat() );
state.setFontSize( 15f );
TextFragment textFragment = new TextFragment();
textFragment.getSegments().add( segment );
Cell cell = new Cell();
cell.getParagraphs().add( textFragment );
BorderInfo info = new BorderInfo();
GraphInfo ginfo = new GraphInfo();
ginfo.setLineWidth( 2 );
ginfo.setColor( Color.getBlack() );
info.setTop( ginfo );
info.setLeft( ginfo );
info.setRight( ginfo );
info.setBottom( ginfo );
cell.setBorder( info );
MarginInfo mInfo = new MarginInfo( 5, 5, 5, 5 );
cell.setMargin( mInfo );
return cell;
}
The results were heavy and not acceptable.( img - 1000 table after memory cleaner.png)
So I tried another way using following code.
I just changed the main block, rest is the same.
public static void main(String[] args) {
long start = System.currentTimeMillis();
for( int i = 0 ; i < 100; i++ ) {
//Instantiate a table object
Document doc = new Document();
// Add page to PDF document
Page page = doc.getPages().add();
page.getPageInfo().setHeight( PageSize.getA4().getHeight() );
page.getPageInfo().setWidth( PageSize.getA4().getWidth() );
MarginInfo info = new MarginInfo( 15, 15, 15, 15 );
page.getPageInfo().setMargin( info );
System.out.println("executing "+i);
Table table = getNestedTable();
page.getParagraphs().add(table);
doc.processParagraphs();
MemoryCleaner.clear();
String temp = tempPath + i + “.pdf”;
doc.save( temp );
}
long end = System.currentTimeMillis();
System.out.println("Generating took "+(end - start));
start = System.currentTimeMillis();
//Instantiate a table object/*
Document doc = new Document();
for( int i = 0; i < 100; i++ ) {
String temp = tempPath + i + “.pdf”;
Document d1 = new Document( temp );
doc.getPages().add( d1.getPages() );
}
end = System.currentTimeMillis();
System.out.println("Merging took "+(end - start));
doc.save( path );
System.out.println(“Generation complete”);
}
In this way I created 1000 rows table and saved the PDF.
At last when 100 PDF’s were created then I merged all 100 PDF’s in one big PDF.
The memory graph is as follows ( img - multiple pdf solution.png )
The memory sure was optimized, But the time was heavily compromised.
The above main block provided below console output-
Generating took 3478148
Merging took 4713
Generation complete
Can you suggest some way where I can optimize both time and memory ?