Stuck on creating pdf file with iText

2019-08-16 08:00发布

问题:

I am trying to create pdf files using iText.

I included itextpdf-5.5.3.jar into my project and following steps in this tutorial:

http://blog.mettletech.co/wp/blog/generate-pdf-file-in-android/

But when this code i getting following errors:

10-30 16:52:57.163: E/dalvikvm(28969): Could not find class 'com.itextpdf.awt.PdfPrinterGraphics2D', referenced from method com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes
10-30 16:52:57.213: E/dalvikvm(28969): Could not find class 'java.awt.image.PixelGrabber', referenced from method com.itextpdf.text.Image.getInstance
10-30 16:52:57.223: E/dalvikvm(28969): Could not find class 'java.awt.image.BufferedImage', referenced from method com.itextpdf.text.Image.getInstance

I dont get any errors on simplier pdf but i am getting thesee errors while creating more complex pdfs including tables etc.

Can you give me a lead on this?

回答1:

The problem you are experiencing isn't caused by iText, but by a specific design choice made by the people who created Android. If you look at the official page about iText for Android (better known as iTextG), you learn that some classes are forbidden on Android. That's why it's important to use the iTextG jar and not the ordinary iText jar:

References to any of the classes not on the Google App Engine whitelist have been removed.

As you know from your Android lessons, all classes in the awt packages (and in the nio packages, etc...) can not be used on Android. Hence you can not use classes such as PdfGraphics2D because that class is an implementation of the abstract java.awt.Graphics2D class that is not on Google's white list for Android and GAE.

So either you are making the mistake of introducing AWT-related functionality, or you are not using the Android port of iText.