Hi,
This is pretty urgent and appreciate a quick response. You can reach me on my cell +91-9920014033 in case you have any questions or seek more clarification.
Per your instruction checked with the latest jar and we are not seeing any change.
Pasting the source code.
package com.dms.test;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.io.IOUtils;
import aspose.pdf.Bookmark;
import aspose.pdf.Bookmarks;
import aspose.pdf.Pdf;
import aspose.pdf.Section;
import aspose.pdf.Text;
import com.aspose.pdf.kit.PdfContentEditor;
import com.aspose.pdf.kit.PdfFileEditor;
import com.aspose.pdf.kit.PdfFileInfo;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.tools.concat_pdf;
public class TestBookMarks {
public void init() {
System.out.println("Loading Aspose License");
aspose.pdf.License licAsposePDF = new aspose.pdf.License();
com.aspose.words.License licAsposeWords = new com.aspose.words.License();
com.aspose.pdf.kit.License licAsposePDFKit = new com.aspose.pdf.kit.License();
try {
licAsposePDF.setLicense(new FileInputStream(new File(
"/4.Workspace/TIFFGeneration/src/Aspose.Total.Java.lic")));
licAsposeWords.setLicense(new FileInputStream(new File(
"/4.Workspace/TIFFGeneration/src/Aspose.Total.Java.lic")));
licAsposePDFKit.setLicense(new FileInputStream(new File(
"/4.Workspace/TIFFGeneration/src/Aspose.Total.Java.lic")));
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String args[]) {
System.out.println("Loading Aspose License");
aspose.pdf.License licAsposePDF = new aspose.pdf.License();
com.aspose.words.License licAsposeWords = new com.aspose.words.License();
com.aspose.pdf.kit.License licAsposePDFKit = new com.aspose.pdf.kit.License();
try {
licAsposePDF.setLicense(new FileInputStream(new File(
"/4.Workspace/TIFFGeneration/src/Aspose.Total.Java.lic")));
licAsposeWords.setLicense(new FileInputStream(new File(
"/4.Workspace/TIFFGeneration/src/Aspose.Total.Java.lic")));
licAsposePDFKit.setLicense(new FileInputStream(new File(
"/4.Workspace/TIFFGeneration/src/Aspose.Total.Java.lic")));
} catch (Exception e)
{
System.out.println(e.getMessage());
}
// Bind target pdf document .
try {
String rootPath = "E:\\Projects\\DMS\\PDF_Input\\";
File dir = new File(rootPath);
String[] chld = dir.list();
if (chld == null) {
System.out
.println("Specified directory does not exist or is not a directory.");
System.exit(0);
} else {
InputStream[] inputStreams = new InputStream[chld.length +1 ];
//ArrayList inputPDFArray = new ArrayList();
int countForSummary = 0;
for (int i = 0; i < chld.length; i++) {
// New a object of Class PdfContentEditor.
PdfContentEditor editor = new PdfContentEditor();
String fileName = chld[i];
System.out.println("File Count --- File name : " + i + "---" + rootPath + fileName);
if (fileName.indexOf("pdf") > 0) {
FileInputStream fileStreamInput = new FileInputStream(rootPath
+ fileName);
PdfFileInfo pdfFileInfo = new PdfFileInfo(fileStreamInput);
int count = pdfFileInfo.getNumberofPages();
countForSummary = countForSummary + count;
fileStreamInput.close();
fileStreamInput = new FileInputStream(rootPath
+ fileName);
editor.bindPdf(fileStreamInput);
editor.deleteBookmarks();
fileStreamInput.close();
for (int k = 0; k < count; k++) {
TestBookMarks tb = new TestBookMarks();
// StringBuffer[] sb = tb.fetchBookMarkDataSingleLine(k);
// editor.createBookmarkOfPage(sb[0].toString() + ";" + sb[1].toString(), k+1);
com.aspose.pdf.kit.Bookmark parentBM = tb.fetchBookMarkDataNested(i, k);
editor.createBookmarks(parentBM);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//FileOutputStream fs = new FileOutputStream(rootPath + fileName);
editor.save(byteArrayOutputStream);
editor.close();
//editor.save(fs);
inputStreams[i+1] = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
// inputPDFArray.add(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
byteArrayOutputStream.close();
}
}
System.out.println("Out of For loop Safely" +inputStreams.length);
PdfFileEditor pdfFileEditor = new PdfFileEditor();
ByteArrayInputStream summarySheet = generatePDFSummary(countForSummary);
inputStreams[0]= summarySheet;
summarySheet.close();
pdfFileEditor.concatenate(inputStreams, new FileOutputStream("E:\\Projects\\DMS\\PDF_Merged\\Rajesh_1.pdf"));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public com.aspose.pdf.kit.Bookmark fetchBookMarkDataNested(int i, int k )
{
com.aspose.pdf.kit.Bookmark parentBM = new com.aspose.pdf.kit.Bookmark();
parentBM.setPageNumber(k+1);
StringBuffer[] sb =fetchBookMarkDataSingleLine(parentBM.getPageNumber());
parentBM.setTitle("Page Number : " + parentBM.getPageNumber());
com.aspose.pdf.kit.Bookmark childBM1 = new com.aspose.pdf.kit.Bookmark();
childBM1.setPageNumber(k+1);
childBM1.setTitle(sb[0].toString());
com.aspose.pdf.kit.Bookmark childBM2 = new com.aspose.pdf.kit.Bookmark();
childBM2.setPageNumber(k+1);
childBM2.setTitle(sb[1].toString());
Bookmarks allChildBM = new Bookmarks();
allChildBM.add(childBM1);
allChildBM.add(childBM2);
parentBM.setChildItem(allChildBM);
return parentBM;
}
public StringBuffer[] fetchBookMarkDataSingleLine(int i)
{
StringBuffer[] sbArray = new StringBuffer[2];
StringBuffer sb = new StringBuffer();
StringBuffer bl = new StringBuffer();
sb.append("NOP_Versand");
sb.append(";");
sb.append("00000000" + i);
sb.append(";");
sb.append("0001");
sb.append(";");
sb.append("CIF5501400");
sb.append(";");
sb.append("00000000000" + i);
sb.append(";");
sb.append("001");
sb.append(";");
sb.append("001");
sb.append(";");
sb.append("00");
sb.append(";");
sb.append("00");
sb.append(";");
sb.append("0");
sb.append(";");
sb.append("0");
sb.append(";");
sb.append("0");
sb.append(";");
sb.append("000000000000");
sb.append(";");
sb.append("000000000000");
sb.append(";");
sb.append("000000000000");
sb.append(";");
sb.append("GEMB_DMS_0001_ " +i);
sb.append(";");
sb.append("IR21" +i);
sb.append(";");
sb.append("GE_20.117 ");
sb.append(";");
sb.append("0");
sb.append(";");
sb.append("F");
bl.append("NOP_Beliage");
bl.append(";");
bl.append("00000000");
sbArray[0]=sb;
sbArray[1]=bl;
// System.out.println("Bookmark 1 -- " +sb.toString());
// System.out.println("Bookmark 2 -- " +bl.toString());
return sbArray;
}
private static ByteArrayInputStream generatePDFSummary(int count)
{
ByteArrayInputStream bi =null;
try
{
// bi = new ByteArrayInputStream(IOUtils.toByteArray(new FileInputStream(new File("D:\\4.Workspace\\TIFFGeneration_old\\resources\\pdf-merged\\SheetStaistic_0.pdf"))));
//Instantiate a Pdf object by calling its empty constructor
Pdf pdf = new Pdf();
//add a section
Section sec1 = pdf.getSections().add();
// create text object to be added to main document section
Text text = new Text("AFP SUMMARY PAGE ...." + '\n' + "Page Count : " +count);
// add text object to paragraphs collection of section
sec1.getParagraphs().add(text);
// specify the font color information for segment object
text.getTextInfo().setColor(new aspose.pdf.Color("Black"));
// specify the BackGround color information for segment object
//text.getTextInfo().setBackgroundColor(new aspose.pdf.Color("Blue"));
// specify the font name for text object
text.getTextInfo().setFontName("GE Inspira");
// set the font size information
text.getTextInfo().setFontSize(11);
ByteArrayOutputStream summStream = new ByteArrayOutputStream();
//Save the resultant PDF
pdf.save(summStream);
bi = new ByteArrayInputStream(summStream.toByteArray());
}catch (Exception excep)
{
System.out.println(excep.getMessage());
}
return bi;
}
private static void concatenate(ArrayList streamOfPDFFiles, FileOutputStream outputStream, boolean paginate)
{
Document document = new Document();
try {
List pdfs = streamOfPDFFiles;
List readers = new ArrayList();
int totalPages = 0;
Iterator iteratorPDFs = pdfs.iterator();
// Create Readers for the pdfs.
while (iteratorPDFs.hasNext()) {
InputStream pdf = iteratorPDFs.next();
iteratorPDFs.remove();
PdfReader pdfReader = new PdfReader(pdf);
readers.add(pdfReader);
totalPages += pdfReader.getNumberOfPages();
pdfReader.close();
pdf.close();
}
// Create a writer for the outputstream
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
// data
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator iteratorPDFReader = readers.iterator();
// Loop through the PDF files and add to the output.
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader,
pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
// Code for pagination.
if (paginate) {
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
+ currentPageNumber + " of " + totalPages, 520,
5, 0);
cb.endText();
}
}
pageOfCurrentReaderPDF = 0;
}
outputStream.flush();
document.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document.isOpen())
document.close();
try {
if (outputStream != null)
outputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
The error message we get.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:95)
at com.aspose.pdf.kit.po.a(Unknown Source)
at com.aspose.pdf.kit.po.(Unknown Source)
at com.aspose.pdf.kit.oi.(Unknown Source)
at com.aspose.pdf.kit.oi.(Unknown Source)
at com.aspose.pdf.kit.PdfFileEditor.a(Unknown Source)
at com.aspose.pdf.kit.PdfFileEditor.concatenate(Unknown Source)
I also tried using append method but it is unable to append pages to the same target file
JVM size increased to 1024 and using Aspose.pdf 4.0.0 (new build)
Machine details
-------------------
Windows XP
Jdk 1.5
Thanks,
Raji R