我想通過使用iText API將兩個或多個PDF文檔合併成一個PDF文件合併。但在結果我得到合併pdf與0字節size.I發佈我的代碼如下所示。我也嘗試了iText.jar文件,但給出相同的0尺寸的PDF。Android:使用iText API合併PDF文件不工作
,並得到這樣的: - 「找不到類 'com.itextpdf.text.pdf.PdfPrinterGraphics2D',從法com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes引用」。 我仍然沒有取得任何成功。
代碼:
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();
}
}
任何想法?
謝謝
當我useed itextgoogle-5.4.0.jar它給我的錯誤 「產生的原因:java.lang.NoClassDefFoundError:org.spongycastle.crypto.engines.AESFastEngine」 – kyogs 2013-05-03 07:55:35
你在你的應用程序需要spongycastle:HTTP: //rtyley.github.io/spongycastle/ – 2013-05-03 08:12:06
得到此錯誤com.itextpdf.license.LicenseKeyException:未加載許可證文件。並生成新的pdf文件與0size – kyogs 2013-05-03 09:19:01