当我按下按钮时,我正在尝试创建PDF文件。我正在使用iText库。
我的活动:
package com.pdf;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class PDFActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void GenerarPDF(View view)throws Exception{
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("generando.pdf"));
document.open();
document.add(new Paragraph("Testing testing and testing"));
document.close();
}
}
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#555">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:onClick="GenerarPDF"
android:text="Button" />
</RelativeLayout>
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pdf"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>
<uses-permission android:name="android.permission.BIND_INPUT_METHOD"/>
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PDFActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
日志猫:
05-31 12:08:58.778: E/AndroidRuntime(662): FATAL EXCEPTION: main
05-31 12:08:58.778: E/AndroidRuntime(662): java.lang.IllegalStateException: Could not execute method of the activity
05-31 12:08:58.778: E/AndroidRuntime(662): at android.view.View$1.onClick(View.java:3044)
05-31 12:08:58.778: E/AndroidRuntime(662): at android.view.View.performClick(View.java:3511)
05-31 12:08:58.778: E/AndroidRuntime(662): at android.view.View$PerformClick.run(View.java:14105)
05-31 12:08:58.778: E/AndroidRuntime(662): at android.os.Handler.handleCallback(Handler.java:605)
05-31 12:08:58.778: E/AndroidRuntime(662): at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 12:08:58.778: E/AndroidRuntime(662): at android.os.Looper.loop(Looper.java:137)
05-31 12:08:58.778: E/AndroidRuntime(662): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-31 12:08:58.778: E/AndroidRuntime(662): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 12:08:58.778: E/AndroidRuntime(662): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 12:08:58.778: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-31 12:08:58.778: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-31 12:08:58.778: E/AndroidRuntime(662): at dalvik.system.NativeStart.main(Native Method)
05-31 12:08:58.778: E/AndroidRuntime(662): Caused by: java.lang.reflect.InvocationTargetException
05-31 12:08:58.778: E/AndroidRuntime(662): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 12:08:58.778: E/AndroidRuntime(662): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 12:08:58.778: E/AndroidRuntime(662): at android.view.View$1.onClick(View.java:3039)
05-31 12:08:58.778: E/AndroidRuntime(662): ... 11 more
05-31 12:08:58.778: E/AndroidRuntime(662): Caused by: java.lang.NoClassDefFoundError: com.itextpdf.text.Document
05-31 12:08:58.778: E/AndroidRuntime(662): at com.pdf.PDFActivity.GenerarPDF(PDFActivity.java:22)
05-31 12:08:58.778: E/AndroidRuntime(662): ... 14 more
添加了logcat错误:
05-31 15:26:00.411: E/dalvikvm(792): Could not find class 'com.itextpdf.text.Document', referenced from method com.pdf.PDFActivity.GenerarPDF
我认为这是真正的问题,但不知道解决方案。
答案 0 :(得分:4)
您到底想要创建pdf文件的确切位置?我使用了itextpdf-5.2.1.jar,并且能够在SD卡中成功创建pdf。我在您的程序中添加了以下代码行:
public void GenerarPDF(View view)throws Exception{
Document document=new Document();
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, "generando.pdf");
PdfWriter.getInstance(document,new FileOutputStream(gpxfile));
document.open();
document.add(new Paragraph("Testing testing and testing"));
document.close();
}
希望这个答案有所帮助。
修改强>: 您在评论中提到的错误(06-01 23:32:37.178:E / AndroidRuntime(535):引起:java.io.FileNotFoundException:/ generando.pdf:open failed:EROFS(只读文件系统) )正是我运行你提供的代码时得到的。原因是您没有指定可以创建/存储新文件的位置。使用已粘贴的GenerarPDF(视图视图),程序将创建新的pdf。
此错误的另一个原因可能是您的设备中没有SD卡。
答案 1 :(得分:1)
将文件根目录更改为此行
File root = new File(android.os.Environment.getExternalStorageDirectory(), "Notes");
答案 2 :(得分:0)
看起来你忘了从onCreate()调用generatePDF()... 希望它有所帮助!