gpt4 book ai didi

java - Android 中的 iText 合并 pdf 错误

转载 作者:行者123 更新时间:2023-12-01 18:52:37 25 4
gpt4 key购买 nike

该代码在 Java 应用程序中运行良好。但是转Android后就无法编译了。错误消息:“无法解析类型 java.awt.geom.AffineTransform。它是从所需的 .class 文件间接引用的”,位于以下行:

cb.addTemplate(page, 0, 0);//compile error at this line

然后我尝试注释掉上面的行...但发生的事情是,这次合并的 pdf 文件是在 Android 中创建的,但里面什么也没有...只有空白页。

这是使用的代码。任何帮助都是值得赞赏的。(也尝试过分别使用 droidText 和普通的 iText jar...仍然没有运气)

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();
}
}
}

最佳答案

基本上,Android 不支持 java.awt.* 软件包,Android 中包含的唯一来自 awt 的软件包是 java.awt.font

引用: Android packages available

关于java - Android 中的 iText 合并 pdf 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15309434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com