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