0

I have come across a runtime exception. I wanted to create a PDF document from android device. For that I used iText library.

This my code for creating PDF

Document document = new Document();
PdfWriter.getInstance(document, outStream);

document.open();
document.add(new Paragraph(data));

document.close();

The code works fine. It is creating PDF successfully. but it gives me exception on runtime saying

06-14 10:09:20.491: W/dalvikvm(764): Unable to resolve superclass of Lcom/itextpdf/awt/PdfGraphics2D; (1251)
06-14 10:09:20.491: W/dalvikvm(764): Link of class 'Lcom/itextpdf/awt/PdfGraphics2D;' failed
06-14 10:09:20.491: E/dalvikvm(764): Could not find class 'com.itextpdf.awt.PdfGraphics2D', referenced from method com.itextpdf.text.pdf.PdfContentByte.createGraphics
06-14 10:09:20.491: W/dalvikvm(764): VFY: unable to resolve new-instance 480 (Lcom/itextpdf/awt/PdfGraphics2D;) in Lcom/itextpdf/text/pdf/PdfContentByte;
06-14 10:09:25.280: E/dalvikvm(764): Could not find class 'org.bouncycastle.cert.X509CertificateHolder', referenced from method com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj
06-14 10:09:25.280: W/dalvikvm(764): VFY: unable to resolve new-instance 1612 (Lorg/bouncycastle/cert/X509CertificateHolder;) in Lcom/itextpdf/text/pdf/PdfReader;

I have done clean and build, added jar to libs folder and make it selected on order and export and i done lot of research for past 2 days. but nothing helped me. Based upon my knowledge there should be these possibilities.

  • The external jar does not loaded properly
  • The class PdfGraphics2D extends java.awt.Graphics2D which is not available in android

any help would be appreciable.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
R9J
  • 6,605
  • 4
  • 19
  • 25

2 Answers2

8

You've discovered that PdfGraphics2D extends java.awt.Graphics2D, and as you already know Graphics2D is a forbidden class on Android.

You've also encountered problems related to BouncyCastle.

This tells me that you're using the Java version of iText instead of the Android port. In the Android port, we replaced BouncyCastle by SpongyCastle (as recommended when using encryption on Android) and we removed all references to forbidden classes (for instance in the awt and nio packages).

Please switch to using the Android port of iText.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • i tried using android version. no exception now. but the file is not opening. it says 'there was an error opening the document' – R9J Jun 14 '13 at 06:54
  • What do the bytes look like when you open the file in a text editor? – Bruno Lowagie Jun 14 '13 at 07:08
  • how can i do that? while i am running in emulator – R9J Jun 14 '13 at 07:18
  • Well... the file is saved somewhere, isn't it? If not, how can you ever open the document? Maybe that's the problem: Adobe Reader can't open a document that doesn't exist on file anywhere. – Bruno Lowagie Jun 14 '13 at 08:54
  • the file exists. but the problem is, it has no content in it. – R9J Jun 14 '13 at 09:35
  • I don't know whether your *code* is correct, I only know that the *snippet* you've shared is correct. There's much more to the code than that snippet, isn't there? – Bruno Lowagie Jun 14 '13 at 15:46
  • Well this library works on lollipop device correctly, however this will not add content to the PDF file on kitkat device that I have tested it on and the link posted above for the android ported version is down, do you have another active link? – kabuto178 Feb 05 '15 at 17:46
  • Here are two links to iTextG: http://sourceforge.net/projects/itextg/files/ and https://github.com/itext/itextg/releases – Bruno Lowagie Feb 05 '15 at 18:18
  • @kabuto178 Thank you for reporting the broken link. We have found the problem and the site is up again. The link is no longer broken. – Bruno Lowagie Feb 06 '15 at 08:55
  • @BrunoLowagie Thank you, the link is indeed fixed now. I have incorporated it in my project and pressing forward. Thanks again. – kabuto178 Feb 06 '15 at 20:47
  • Can't you please just give us the gradle statement instead of all this roundabout stuff? – G_V Aug 08 '15 at 09:58
  • @G_V Please ask someone who is familiar with gradle. I've never worked with gradle, so don't expect an answer from me. If nobody replies, there's always paid support at iText Software (several engineers know gradle there). – Bruno Lowagie Aug 08 '15 at 10:23
  • Gradle is like Maven but with Groovy instead of XML, right? (I don't know very much about Gradle so correct me if I'm wrong). Well, iTextG is not on Maven Central (yet) so you will have to add it manually and edit your build script accordingly. One of these days I'll get around to upload iTextG to Maven Central and then you'll be able to set it as a dependency in any Maven-based build system: Maven, Gradle, Ivy, or whatever you like best. I hope to get that done by the next iText release (we're on a loose 3-month release schedule). – Amedee Van Gasse Dec 21 '15 at 13:20
0

If you are using iText pdf library in android, so simply write these three lines in your Proguard file to avoid warnings,

-dontwarn com.itextpdf.text.pdf.**
-dontwarn org.bouncycastle.**
-dontwarn com.sun.mail.**
Aman Gupta - ΔMΔN
  • 2,971
  • 2
  • 19
  • 39