简体   繁体   English

iText:在android中创建pdf文件时遇到问题

[英]iText: having trouble creating a pdf file in android

i have a problem with iText in creating a pdf file in android. 我有一个问题,iText在Android中创建一个PDF文件。 It keeps crashing and this error keeps appearing in log cat: 它一直崩溃,这个错误一直出现在log cat中:

java.lang.NoClassDefFoundError: com.itextpdf.text.Document

this is my java file: 这是我的java文件:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class MainActivity extends Activity {

public static Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.Submit);

    button.setOnClickListener(new OnClickListener() 
    {           
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub

            toPDF();

        }
    });
}

public void toPDF(){

    Document document=new Document();
    try {
        PdfWriter.getInstance(document,new FileOutputStream("try.pdf"));
        document.open();
        document.add(new Paragraph("Hello Android!! :)"));
        document.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

and i already included <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in AndroidManifest.xml 我已经在AndroidManifest.xml中包含了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and this is my .classpath.. 这是我的.classpath ..

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
     <classpathentry kind="src" path="src"/>
     <classpathentry kind="src" path="gen"/>
     <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
     <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itextpdf-5.3.4.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itextpdf-5.3.4-javadoc.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itextpdf-5.3.4-sources.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-pdfa-5.3.4.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-pdfa-5.3.4-javadoc.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-pdfa-5.3.4-sources.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-xtra-5.3.4.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-xtra-5.3.4-javadoc.jar"/>
     <classpathentry kind="lib" path="C:/Users/joan/Documents/Eclipse Workspace/itext-5.3.4/itext-xtra-5.3.4-sources.jar"/>
     <classpathentry kind="output" path="bin/classes"/>
</classpath>

I also tried to insert SD card because someone says that it might be the problem, but still, it crashed... 我也尝试插入SD卡,因为有人说它可能是问题所在,但它仍然崩溃了......

hope anyone can help me.. i'm new to android.. :| 希望任何人都可以帮助我..我是新来的Android ..:|

There are only three reasons you will ever get this error: 您将遇到此错误的原因只有三个:

  1. The class genuinely doesn't exist. 这堂课真的不存在。 If you are using code from an official example and getting this, make sure you have the latest build of the library 如果您使用的是官方示例中的代码并获得此代码,请确保您拥有该库的最新版本
  2. You have not added the jar to your build path. 您尚未将jar添加到构建路径中。 To fix this, right click on the jar in Eclipse, and do Build Path ► Add to Build Path. 要解决此问题,请右键单击Eclipse中的jar,然后执行BuildPath►Addto Build Path。
  3. Your jar is not in the /libs folder. 你的jar不在/ libs文件夹中。 This happens when you have added the jar to the build path, but newer versions of ADT need it to be in /libs. 将jar添加到构建路径时会发生这种情况,但较新版本的ADT需要将它放在/ libs中。 Put it there and re-add it to the build path. 把它放在那里并重新添加到构建路径。

Mostly, such errors occur because newer versions of the ADT require all external jars to be in the libs folder. 大多数情况下,会出现此类错误,因为较新版本的ADT要求所有外部jar都位于libs文件夹中。 Your colleague was probably on a different version than you, and hence the error. 您的同事可能与您的版本不同,因此错误。

In your case, simple move all the necessary jar files from itext-5.3.4 into libs . 在您的情况下,简单地将所有必要的jar文件从itext-5.3.4libs

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

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