該代碼在Java應用程序中運行良好。但是當我轉移到Android時我無法編譯。錯誤信息:「該類型java.awt.geom.AffineTransform中不能得到解決它間接地從需要的.class文件中引用」在該行:iText在Android中合併pdf錯誤
cb.addTemplate(page, 0, 0);//compile error at this line
然後我試圖註釋掉上面的行...但發生了什麼事情,這次合併的PDF文件是在Android中創建的,但內部沒有內容......只有空白頁。
這是使用的代碼。任何幫助表示讚賞。(嘗試也同時使用droidText並單獨正常iText的罐子...仍然沒有運氣)
public void concatPDFs() {
Document document = new Document();
try {
uploadedFile.setVisibility(View.VISIBLE);
File sdCard = Environment.getExternalStorageDirectory();
uploadedFile.setText(sdCard.getAbsolutePath());
List<InputStream> pdfs = new ArrayList<InputStream>();
pdfs.add(new FileInputStream("/storage/extSdCard/1.pdf"));
pdfs.add(new FileInputStream("/storage/extSdCard/2.pdf"));
outputStream = new FileOutputStream("/storage/extSdCard/merge.pdf");
uploadedFile.setText("Added Files");
List<PdfReader> readers = new ArrayList<PdfReader>();
int totalPages = 0;
Iterator<InputStream> iteratorPDFs = pdfs.iterator();
// Create Readers for the pdfs.
while (iteratorPDFs.hasNext()) {
InputStream pdf = iteratorPDFs.next();
PdfReader pdfReader = new PdfReader(pdf);
readers.add(pdfReader);
totalPages += pdfReader.getNumberOfPages();
}
// Create a writer for the outputstream
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
uploadedFile.setText("PdfWriter");
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
// data
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();
// Loop through the PDF files and add to the output.
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader,
pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
uploadedFile.setText("getImportedPage");
// Code for pagination.
if (true) {
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
+ currentPageNumber + " of " + totalPages, 520,
5, 0);
cb.endText();
}
}
pageOfCurrentReaderPDF = 0;
}
outputStream.flush();
document.close();
outputStream.close();
uploadedFile.setText("Done Pdf");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document.isOpen())
document.close();
try {
if (outputStream != null)
outputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
感謝您的澄清! – kaderud 2013-03-09 10:47:26
第一個鏈接不工作 – Azahar 2014-04-12 11:29:08
請參閱http://www.itextpdf.com/product/itextg – 2014-04-12 11:32:16