eldoz12
asked on
PDF Concatenate problem
hello Experts ,
I have 2 PDF files , I need to append the content of one file to the of the other file ,
if the two PDF files are output file and scr file ,
The prog needs to read from scr file and add the content to the output file at a specified page . currently I am using a Itext sample prog to do it ,
The only problem is that the content of the output file is completely over-written by the scr file , and the output file becomes a copy of the scr file .
public boolean PdfConcatenate(int start_page,FormFile scr_filename) {
System.out.println("PdfCop y example");
try {
int pageOffset = 0;
ArrayList master = new ArrayList();
int f = 0;
/**
* Get output file from location at which cool file is downloaded to
**/
String outFile = "Z:\\pdf\\app\\coolpdffile s\\716048. PDF";
Document document = null;
PdfCopy writer = null;
// we create a reader for a certain document
PdfReader reader = new PdfReader(scr_filename.get InputStrea m());
reader.consolidateNamedDes tinations( );
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
pageOffset += n;
List bookmarks = SimpleBookmark.getBookmark (reader);
if (bookmarks != null) {
if (pageOffset != 0)
SimpleBookmark.shiftPageNu mbers(book marks, pageOffset, null);
master.addAll(bookmarks);
}
// step 1: creation of a document-object
document = new Document(reader.getPageSiz eWithRotat ion(1));
// step 2: we create a writer that listens to the document
writer = new PdfCopy(document, new FileOutputStream(outFile)) ;
// step 3: we open the document
document.open();
// step 4: we add content
PdfImportedPage page;
for (int i = start_page; i < n; ) {
++i;
page = writer.getImportedPage(rea der, i);
writer.addPage(page);
}
PRAcroForm form = reader.getAcroForm();
if (form != null)
writer.copyAcroForm(reader );
if (master.size() > 0)
writer.setOutlines(master) ;
// step 5: we close the document
document.close();
return true;
}
catch(Exception e) {
e.printStackTrace();
return false;
}
plz advice
I have 2 PDF files , I need to append the content of one file to the of the other file ,
if the two PDF files are output file and scr file ,
The prog needs to read from scr file and add the content to the output file at a specified page . currently I am using a Itext sample prog to do it ,
The only problem is that the content of the output file is completely over-written by the scr file , and the output file becomes a copy of the scr file .
public boolean PdfConcatenate(int start_page,FormFile scr_filename) {
System.out.println("PdfCop
try {
int pageOffset = 0;
ArrayList master = new ArrayList();
int f = 0;
/**
* Get output file from location at which cool file is downloaded to
**/
String outFile = "Z:\\pdf\\app\\coolpdffile
Document document = null;
PdfCopy writer = null;
// we create a reader for a certain document
PdfReader reader = new PdfReader(scr_filename.get
reader.consolidateNamedDes
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
pageOffset += n;
List bookmarks = SimpleBookmark.getBookmark
if (bookmarks != null) {
if (pageOffset != 0)
SimpleBookmark.shiftPageNu
master.addAll(bookmarks);
}
// step 1: creation of a document-object
document = new Document(reader.getPageSiz
// step 2: we create a writer that listens to the document
writer = new PdfCopy(document, new FileOutputStream(outFile))
// step 3: we open the document
document.open();
// step 4: we add content
PdfImportedPage page;
for (int i = start_page; i < n; ) {
++i;
page = writer.getImportedPage(rea
writer.addPage(page);
}
PRAcroForm form = reader.getAcroForm();
if (form != null)
writer.copyAcroForm(reader
if (master.size() > 0)
writer.setOutlines(master)
// step 5: we close the document
document.close();
return true;
}
catch(Exception e) {
e.printStackTrace();
return false;
}
plz advice
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, you should be maintaining a reference to the FileOutputStream and closing it in a finally block.
ASKER
I will try what u asked me to ....
ASKER
>>> Try new FileOutputStream ( outFile, true )
I tried this but still same effect , Its overwrite of the entier output file and not a append
I tried this but still same effect , Its overwrite of the entier output file and not a append
ASKER
plz advice
ASKER
mayankeagle , i checked the api and the advice you gave looks correct , but for some reason , I am still having a problem
i believe, the start_page parameter is not correctly handled.
Try debugging the start_page handling..here
for (int i = start_page; i < n; ) {
++i;
page = writer.getImportedPage(rea der, i);
writer.addPage(page);
}
see what page is getting processed and added.
Otherwise all looks fine.
fargo
Try debugging the start_page handling..here
for (int i = start_page; i < n; ) {
++i;
page = writer.getImportedPage(rea
writer.addPage(page);
}
see what page is getting processed and added.
Otherwise all looks fine.
fargo
ASKER
the start_page here is used to read from the src_file , it just say that start reading from the (start_page) in the src_file ,
Like if the person wants to append the scr_file from the 3 rd page then it starts reading from the 3 rd page .
Like if the person wants to append the scr_file from the 3 rd page then it starts reading from the 3 rd page .
So that worked?
ASKER