gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:04:01 26 4
gpt4 key购买 nike

我正在使用 Android Studio 构建应用。在我的 Activity 中,我想创建一个 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();
}
}

这是我的 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'
}

但是如果我尝试执行这段代码,我会收到这个奇怪的错误。

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

这个错误很奇怪,因为我没有使用 awt.Color。

最佳答案

您使用的是用于 Java 的 iText,而您应该使用用于 Android 和 GoogleAppEngine 的 iTextG。参见 http://itextgroup.com/product/itextg

在 iText for Java 中,您依赖于 Java 包 java.awtjavax.nio 等中的类。这些类在 Android 上是被禁止的,这就解释了为什么会出现该错误。

在 iTextG 中,我们删除了所有这些类,保留了大部分功能。

更新:查看您的依赖项,我看到 'com.itextpdf:itextg:5.5.9' 这意味着您已经在使用 iTextG。然而,您在某处使用了 java.awt.Color,这是被禁止的。

当我查看您的错误消息时,我明白了

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

Lowagie,就是我,您正在使用 iText 2.1.7(不应再使用的库)以及 iTextG(您需要的库)。您应该检查您是如何引入旧的 iText 版本的,因为它不应该在 Android 上使用。

解决方案:

  • 确保您没有导入 com.lowagie 类。仅导入 com.itextpdf 类。一般规则是:如果您在代码中看到我的名字,那么您做错了什么。
  • 确保删除所有对 iText 2.1.7 的引用。您不应同时使用两个不同版本的 iText。
  • 确保您没有在代码中引入任何禁止类(java.awt.*javax.nio.*)。

关于android - 如何从 Android 以编程方式创建 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40423996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com