简体   繁体   English

如何从Android以编程方式创建pdf

[英]How to create a pdf programmatically from Android

I'm building an app with Android Studio. 我正在使用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. 在我的活动中,我想创建一个PDF文件,因此我已经从互联网上复制了此代码以创建并打开一个简单的PDF文件。

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: . 这是我的Grandle文件:。

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. 这个错误非常奇怪,因为我不使用awt.Color。

You are using iText for Java, while you should use iTextG for Android and GoogleAppEngine. 您正在使用iText for Java,而应该使用iTextG for Android和GoogleAppEngine。 See http://itextgroup.com/product/itextg 参见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. 在iText for Java中,您对Java包java.awtjavax.nio等中的类具有依赖性。 These classes are forbidden on Android and that explains why you get that error. 这些类在Android上被禁止,这说明了为什么会出现该错误。

In iTextG, we removed all those classes, keeping most of the functionality intact. 在iTextG中,我们删除了所有这些类,并保留了大部分功能。

Update: looking at your dependencies, I see 'com.itextpdf:itextg:5.5.9' which means you are already using iTextG. 更新:查看您的依赖项,我看到'com.itextpdf:itextg:5.5.9' ,这意味着您已经在使用iTextG。 Nevertheless, you are using java.awt.Color somewhere and that is forbidden. 但是,您在某个地方使用java.awt.Color ,这是禁止的。

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). Lowagie,就是我,您正在使用iText 2.1.7(不应再使用的库)以及iTextG(您需要的库)。 You should check how you introduced that old iText version as it shouldn't be used on Android. 您应该检查一下如何引入该旧iText版本,因为它不应在Android上使用。

Solution: 解:

  • Make sure that you don't import com.lowagie classes. 确保您不导入com.lowagie类。 Only import com.itextpdf classes. 仅导入com.itextpdf类。 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. 确保删除所有对iText 2.1.7的引用。 You shouldn't use two different versions of iText next to each other. 您不应该相邻使用两个不同版本的iText。
  • Make sure you don't introduce any of the forbidden classes ( java.awt.* , javax.nio.* ) in your code. 确保您的代码中没有引入任何禁止的类( java.awt.*javax.nio.* )。

You are using a library that has been designed for pure Java. 您正在使用专为纯Java设计的库。 There are some minor but still notable differences between Java APIs and Android APIs, mostly related to gfx. Java API和Android API之间存在一些细微但仍值得注意的差异,主要与gfx有关。 As you can see the Java Color class doesn't have a strict equivalent on Android. 如您所见,Java Color类在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. 您可以找到支持Android的PDF库,也可以使用远程服务来转换文档并将其直接下载为PDF。

This thread might be of interest to you : PDF Library for Android - PDFBox? 您可能对此线程感兴趣:Android的PDF库-PDFBox?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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