Hi
I need to put a header and footer to an existing PDF document. This works
fine but when the PDF gets large (>18.9 MB) then I get the out of memory
problem.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.io.BufferedOutputStream.<init>(BufferedOutputStream.java:59)
at
org.apache.pdfbox.cos.COSStream.createUnfilteredStream(COSStream.java:459)
at
org.apache.pdfbox.pdmodel.common.PDStream.createOutputStream(PDStream.java:218)
at
org.apache.pdfbox.pdmodel.edit.PDPageContentStream.<init>(PDPageContentStream.java:240)
at com.akimeka.jmar.pdfwriter.AddFOUOToReport.doIt(AddFOUOToReport.java:83)
at com.akimeka.jmar.pdfwriter.AddFOUOToReport.main(AddFOUOToReport.java:137)
It is the same code as I had reported a bug in PDF Box. Here is the code for
your ref:
// the document
PDDocument doc = null;
File file1 = new File(file);
try
{
//RandomAccessFile raf = new RandomAccessFile(file, "r");
//RandomAccess ra = new RandomAcsess
doc = PDDocument.loadNonSeq(file1, null);//load( file );
List<PDPage> allPages = doc.getDocumentCatalog().getAllPages();
PDFont font = PDType1Font.HELVETICA;
float fontSize = 15.0f;
for( int i=0; i<allPages.size(); i++ )
//int i=0;
{
System.out.println("i = " + i);
PDPage page = (PDPage)allPages.get( i );
PDRectangle pageSize = page.findMediaBox();
float stringWidth = font.getStringWidth( message
)*fontSize/1000f;
// calculate to center of the page :
int rotation = page.findRotation();
boolean rotate = rotation == 90 || rotation == 270;
float pageWidth = rotate ? pageSize.getHeight() :
pageSize.getWidth();
float pageHeight = rotate ? pageSize.getWidth() :
pageSize.getHeight();
double centeredXPosition = rotate ? pageHeight/2f : (pageWidth
- stringWidth)/2f;
double centeredYPosition = rotate ? (pageWidth -
stringWidth)/2f : pageHeight/2f;
// append the content to the existing stream
PDPageContentStream contentStream = new
PDPageContentStream(doc, page, true, true,true);
page.getResources().getFonts();
contentStream.beginText();
// set font and font size
contentStream.setFont( font, fontSize );
//set text color to red
//contentStream.setNonStrokingColor(255, 0, 0);
if (rotate)
{
// rotate the text according to the page rotation
contentStream.setTextRotation(Math.PI/2, centeredXPosition,
0);
}
else
{
contentStream.setTextTranslation(centeredXPosition, 0);
}
//contentStream.moveTextPositionByAmount(pageWidth/2 - 130,
pageHeight-20);
contentStream.moveTextPositionByAmount(0, pageHeight-50);
contentStream.drawString( message);
contentStream.endText();
//Now repeat the same for footer
//page.getResources().getFonts();
contentStream.beginText();
// set font and font size
//contentStream.setFont( font, fontSize );
contentStream.moveTextPositionByAmount((int)centeredXPosition,
50);
contentStream.drawString( message);
contentStream.endText();
//Close contentStream.
contentStream.close();
}
doc.save( outfile );
}
Any suggestions you can provide is greatly appreciated.
-Anita