我想通过使用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 list = new ArrayList();
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 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();
}
}
任何想法?
谢谢
-05-03
kyogs
如果觉得《android图片合并pdf Android:使用iText API合并PDF文件不工作》对你有帮助,请点赞、收藏,并留下你的观点哦!