Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't import com.itextpdf.text.Document class

I'm building an android app and I want to use iText for creating pdf file, but I can't use Document class. As I seen in tutorials, there should be import com.itextpdf.text.Document for using Document class. For this app, I'm using com.itextpdf:itext-pdfa:5.5.9 library. I want to create a simple pdf file with 2 paragraphs, something like this:

    try{
        File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS), "pdfdemo");
        if (!pdfFolder.exists()) {
            pdfFolder.mkdir();
        }

        Date date = new Date() ;
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);

        File myFile = new File(pdfFolder + timeStamp + ".pdf");

        OutputStream output = new FileOutputStream(myFile);

        Document document = new Document();

        PdfAWriter.getInstance(document, output);

        document.open();

        document.add(new Paragraph(mSubjectEditText.getText().toString()));
        document.add(new Paragraph(mBodyEditText.getText().toString()));

        document.close();
    }catch (Exception e) {}

'

Could anyone help me with this problem? What am I doing wrong?

like image 540
micdo94 Avatar asked Sep 20 '25 00:09

micdo94


1 Answers

You say:

I'm using com.itextpdf:itext-pdfa:5.5.9 library

That is wrong for two reasons:

  1. itext-pdfa is an addon to iText that is meant for writing or manipulating PDF/A documents. It requires the core iText libary. Read about the different parts of iText on the official web site: https://developers.itextpdf.com/itext-java
  2. You say you want to use iText on Android, but you are referring to iText for Java. iText for Java contains classes that are not allowed on Android (java.awt.*, javax.nio,...). You should use the Android port for iText, which is called iTextG: https://developers.itextpdf.com/itextg-android

It's as if you're using iText without having visited the official iText web site. How is that even possible?

like image 81
Bruno Lowagie Avatar answered Sep 22 '25 14:09

Bruno Lowagie