iText merge pdf error in Android

让人想犯罪 __ 提交于 2019-12-25 07:37:40

问题


The code works well in Java application. But I can't compile when I transfer to Android. Error message: "The type java.awt.geom.AffineTransform cannot be resolved. It is indirectly referenced from required .class files" at the line:

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

Then I tried commenting out the above line...but what happened was, this time the merged pdf file was created in Android but there's nothing inside...only blank pages.

This is the code used. Any help is appreciated.(Tried also using both droidText and the normal iText jars separately...still no luck)

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

回答1:


Basically, no java.awt.* packages are supported by Android, the only package from awt included in Android is java.awt.font.

Reference: Android packages available




回答2:


I upvoted the previous answer, but I want to add that user614454 is using the Java version of iText instead of the official Android port. The official Android port can be found here: http://itextsupport.com/download/android.html

In this official Android port all java.awt classes that are needed by iText are replaced with light-weight alternatives.

Note that I'm the original author of iText. I removed a reference to DroidText because that's an Android port of an iText version that shouldn't be used anymore: http://lowagie.com/itext2



来源:https://stackoverflow.com/questions/15309434/itext-merge-pdf-error-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!