简体   繁体   中英

How to create a pdf programmatically from Android

I'm building an app with Android Studio. In my activity, I want to create a PDF file, so I have copy from internet this code to create and open a simple PDF file.

public void createandDisplayPdf(String text) {

        Document doc = new Document();

        try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";

            File dir = new File(path);
            if(!dir.exists())
                dir.mkdirs();

            File file = new File(dir, "newFile.pdf");
            FileOutputStream fOut = new FileOutputStream(file);

            PdfWriter.getInstance(doc, fOut);

            //open the document
            doc.open();

            Paragraph p1 = new Paragraph(text);
            Font paraFont= new Font(Font.COURIER);
            p1.setAlignment(Paragraph.ALIGN_CENTER);
            p1.setFont(paraFont);

            //add paragraph to document
            doc.add(p1);

        } catch (DocumentException de) {
            Log.e("PDFCreator", "DocumentException:" + de);
        } catch (IOException e) {
            Log.e("PDFCreator", "ioException:" + e);
        }
        finally {
            doc.close();
        }

        viewPdf("newFile.pdf", "Dir");
    }

// Method for opening a pdf file
    private void viewPdf(String file, String directory) {

        File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file);
        Uri path = Uri.fromFile(pdfFile);

        // Setting the intent for pdf reader
        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(path, "application/pdf");
        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            startActivity(pdfIntent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(creaOrdine.this, "Can't read pdf file", Toast.LENGTH_SHORT).show();
        }
    }

This is my Grandle file: .

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.mcsolution.easymanagementandroid"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile files('libs/gson-2.2.4.jar')
    compile 'com.itextpdf:itextg:5.5.9'
}

But if I try to execute this code, I received this strange error.

11-04 14:21:11.014 10336-10336/com.mcsolution.easymanagementandroid.easymanagementandroid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mcsolution.easymanagementandroid.easymanagementandroid, PID: 10336
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Color;
at com.lowagie.text.pdf.PdfChunk.color(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.writeLineToContent(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.flushLines(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.newPage(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.close(Unknown Source)
at com.lowagie.text.Document.close(Unknown Source)
at com.mcsolution.easymanagementandroid.ordine.creaOrdine.createandDisplayPdf(creaOrdine.java:710)
at com.mcsolution.easymanagementandroid.ordine.creaOrdine.onCreate(creaOrdine.java:99)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.awt.Color" on path: 
DexPathList[[dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-support-annotations-23.3.0_469f8f3ca3f9e0c059b9c6f2d04acc51933988a6-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_9-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_8-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_7-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_6-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_5-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_4-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_3-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_2-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_1-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-slice_0-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-lowagie-2.1.7_6c71a44cd1fb2c264f7135848cfb4fd4358dd002-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-itextg-5.5.9_cd19495ec54a0d85948e5ccfd0ef953b783cde4e-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-internal_impl-23.3.0_459b0ff04633a2c5ffc128ab96df15303edb9d39-classes.dex", 
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-gson-2.2.4_e859ceafcaa49a8708d05a8e64ce9bdc0fd2e6a0-classes.dex",
dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.3.0_32569b030fbf7638ea

This error is very strange, beacuse, I don't use awt.Color.

You are using iText for Java, while you should use iTextG for Android and GoogleAppEngine. See http://itextgroup.com/product/itextg

In iText for Java you have dependencies on classes in the Java packages java.awt , javax.nio and so on. These classes are forbidden on Android and that explains why you get that error.

In iTextG, we removed all those classes, keeping most of the functionality intact.

Update: looking at your dependencies, I see 'com.itextpdf:itextg:5.5.9' which means you are already using iTextG. Nevertheless, you are using java.awt.Color somewhere and that is forbidden.

When I look at your error message, I see

dex file "/data/data/com.mcsolution.easymanagementandroid.easymanagementandroid/files/instant-run/dex/slice-lowagie-2.1.7

Lowagie, that's me, and you are using iText 2.1.7 (a library that should no longer be used) as well as iTextG (the library you need). You should check how you introduced that old iText version as it shouldn't be used on Android.

Solution:

  • Make sure that you don't import com.lowagie classes. Only import com.itextpdf classes. The general rule is: if you see my name in your code, you're doing something wrong.
  • Make sure you remove all references to iText 2.1.7. You shouldn't use two different versions of iText next to each other.
  • Make sure you don't introduce any of the forbidden classes ( java.awt.* , javax.nio.* ) in your code.

You are using a library that has been designed for pure Java. There are some minor but still notable differences between Java APIs and Android APIs, mostly related to gfx. As you can see the Java Color class doesn't have a strict equivalent on Android. That's what causes your bug here.

Either you find an Android-able PDF library or you use a remote service to convert your document and download it as PDF directly.

This thread might be of interest to you : PDF Library for Android - PDFBox?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM