Android : Merge PDFs file using iText API not working
我试图通过使用iText API将两个或多个PDF文档合并为一个来合并pdf文件,但结果是我得到了0字节大小的合并pdf。我将代码如下所示。我也尝试了iText.jar文件 但给出相同的0尺寸pdf。
并得到了这一点:-"找不到类com.itextpdf.text.pdf.PdfPrinterGraphics2D",该类从方法com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes引用。
我仍然没有获得任何成功。
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | public class ItextMerge { public static void main() { List<InputStream> list = new ArrayList<InputStream>(); try { // Source pdfs list.add(new FileInputStream(new File("mnt/sdcard/nocturia.pdf"))); list.add(new FileInputStream(new File("mnt/sdcard/Professional Android Application Development.pdf"))); // Resulting pdf OutputStream out = new FileOutputStream(new File("mnt/sdcard/newmerge.pdf")); doMerge(list, out); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Merge multiple pdf into one pdf * * @param list * of pdf input stream * @param outputStream * output file output stream * @throws DocumentException * @throws IOException */ public static void doMerge(List<InputStream> list, OutputStream outputStream) throws DocumentException, IOException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); for (InputStream in : list) { PdfReader reader = new PdfReader(in); for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); //import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); //add the page to the destination pdf // cb.addTemplate(page, 0, 0); // cb.addTemplate(page, 0, 0); } } outputStream.flush(); document.close(); outputStream.close(); } } |
任何的想法?
谢谢
我赞成迈克尔的答案,因为这是对您问题的正确答案。
但是,阅读代码时,您还有一个未知的问题:您使用了错误的代码来合并PDF。您应该使用
之前已经对此进行了多次解释:
- 写完另一个后如何在pdf上保持相同格式
- itextsharp:复制页面上的意外元素
- itext pdf合并:在pdf(文本截断)页面外出现文档溢出并且不显示
- ...
您正在使用PdfWriter的事实表明您没有阅读文档。
此外,您的问题听起来好像您不知道Lowagie是一个人的名字。实际上,这是我的名字,当有人说Lowagie iText API不起作用时,这很尴尬。首先,因为我一直在要求停止使用那些旧版本的iText,而且还因为它听起来像是个人指责,使产品与人混淆。请参见lowagie和iText有什么区别?
请使用iText的Android端口:
http://repo.itextsupport.com//android_gae/com/itextpdf/itextgoogle/
您需要试用许可证才能在Android上使用iText。
http://demo.itextsupport.com/newslicense/
以下是合并两个pdf文件的简单代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | try{ String doc1 = FOLDER_PATH +"Doc1.pdf"; String doc2 = FOLDER_PATH +"Doc2.pdf"; String resultDocFile = FOLDER_PATH +"ResultDoc.pdf"; PdfReader reader1 = new PdfReader(doc1); Document resultDoc = new Document(); PdfCopy copy = new PdfCopy(resultDoc, new FileOutputStream(resultDocFile)); resultDoc.open(); //Copying First Document for(int i = 1; i <= reader1.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(reader1, i)); } PdfReader reader2 = new PdfReader(doc2); //Copying Second Document for(int i = 1; i <= reader2.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(reader2, i)); } resultDoc.close(); } catch (Exception e){ e.printStackTrace(); } |