Hi Adam,
I have tried to create the same logic. But I am unable to save the output file.
Please find the attachment of the file . I am using Apache PDFBox -- Version
0.8.0-incubating. The exception thrown is:
Output:
Else J and insertPageAt ---1 2
J and insertPageAt ---2 2
Else J and insertPageAt ---3 2
mainDoc total pages-----3
org.apache.pdfbox.exceptions.COSVisitorException: The handle is invalid
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:939)
at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:201)
at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:206)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:430)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:361)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:768)
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:379)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1070)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:893)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:874)
at com.ge.sample.fotopdf.PDFBoxSplitPdf.main(PDFBoxSplitPdf.java:48)
java.io.IOException: The handle is invalid
at java.io.RandomAccessFile.seek(Native Method)
at org.apache.pdfbox.io.RandomAccessFile.seek(RandomAccessFile.java:59)
at
org.apache.pdfbox.io.RandomAccessFileInputStream.read(RandomAccessFileInputStream.java:96)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:926)
at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:201)
at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:206)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:430)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:361)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:768)
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:379)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1070)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:893)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:874)
at com.ge.sample.fotopdf.PDFBoxSplitPdf.main(PDFBoxSplitPdf.java:48)
Please suggest me where I was wrong. The new document is creating the pages.
But it is not getting saved in the pdf file.
Regards
Deepthi.
--- On Thu, 29/10/09, a...@swmc.com <a...@swmc.com> wrote:
From: a...@swmc.com <a...@swmc.com>
Subject: Re: Regarding merging pdfs
To: pdfbox-users@incubator.apache.org
Cc: pdfbox-users@incubator.apache.org
Date: Thursday, 29 October, 2009, 9:29 PM
Get the pages one at a time, and then write them to a new document in the
desired order. IIRC, getPages() is in the PDDocument class.
--Adam
k deepthi <kdeeps...@yahoo.co.in>
10/29/2009 02:01
Please respond to
pdfbox-users@incubator.apache.org
To
pdfbox-users@incubator.apache.org
cc
Subject
Regarding merging pdfs
Hi,
I have two pdfs . One pdf with more than one page. Another with only one
page. I have to add this page to the first pdf at 3rd page. Can anyone
suggest me the solution how to do it.
Regards
Deepthi.
Try the new Yahoo! India Homepage. Click here.
http://in.yahoo.com/trynew
? Click here to submit conditions
This email and any content within or attached hereto from Sun West Mortgage
Company, Inc. is confidential and/or legally privileged. The information is
intended only for the use of the individual or entity named on this email. If
you are not the intended recipient, you are hereby notified that any
disclosure, copying, distribution or the taking of any action in reliance on
the contents of this email information is strictly prohibited, and that the
documents should be returned to this office immediately by email. Receipt by
anyone other than the intended recipient is not a waiver of any privilege.
Please do not include your social security number, account number, or any other
personal or financial information in the content of the email. Should you have
any questions, please call (800) 453 7884.
Connect more, do more and share more with Yahoo! India Mail. Learn more.
http://in.overview.mail.yahoo.com/
package com.ge.sample.fotopdf;
import java.io.FileInputStream;
import java.util.List;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
public class PDFBoxSplitPdf {
private static int insertPageAt = 2;
public static void main(String[] args){
try{
FileInputStream doc2 = new FileInputStream("D:/MainDoc.pdf");
PDFParser parser1 = new PDFParser(doc2);
parser1.parse();
PDDocument mainDoc1 = new PDDocument();
FileInputStream doc3 = new FileInputStream("D:/Deepthi_Official/Reports/july30act5-005.pdf");
PDFParser parser2 = new PDFParser(doc3);
parser2.parse();
PDDocument doc = parser1.getPDDocument();
PDDocumentCatalog cat = doc.getDocumentCatalog();
PDDocument newP = parser2.getPDDocument();
PDDocumentCatalog newPCat = newP.getDocumentCatalog();
List nPages = newPCat.getAllPages();
List pages = cat.getAllPages();
for(int j = 0,i = 0; i < pages.size() || j < (pages.size() + nPages.size()) ; j++ ){
if(j+1 == insertPageAt){
System.out.println("J and insertPageAt ---"+(j+1)+" "+insertPageAt);
for(int k = 0; k < nPages.size() ; k++ ){
mainDoc1.addPage((PDPage)nPages.get(k));
}
newP.close();
i++;
}
else{
System.out.println("Else J and insertPageAt ---"+(j+1)+" "+insertPageAt);
mainDoc1.addPage((PDPage)pages.get(i));
if(i == pages.size() - 1){
i++;
}
}
}
System.out.println("mainDoc total pages-----"+mainDoc1.getNumberOfPages());
mainDoc1.save("D:/Deepthi_Official/Reports/MainPdf.pdf"); //Exception thrown for save method of PDDoument
//mainDoc1.save(mainDoc);
doc.close();
mainDoc1.close();
System.out.println("Document merged at Page "+insertPageAt);
}catch(Exception e){
e.printStackTrace();
}
}
}