java - 如何在android中使用itext生成pdf

标签 java android pdf itext

我是 Android 开发新手,才两天,我想通过以 pdf 格式显示数据来从数据库中检索数据。我之前在 netbeans 上做过这个(有效),但是当我在 android 中尝试我的代码时,它给了我一个错误。我有 SQLitExample.java 文件,它触发我的 btnPdf 的操作,还有 createPDF.java 文件,它应该创建我的 pdf。我尝试通过创建新 Intent 来调用 createPDF.java 文件。但是当我点击 btnPdf 时,它给了我这个错误:

dalvikvm(6097): Could not find class 'com.example.hotornot.createPDF$1', referenced from method com.example.hotornot.createPDF.pdf

这是我在 SQLitExample.java 上的代码

package com.example.hotornot;

import java.io.IOException;
import java.sql.SQLException;

import com.lowagie.text.DocumentException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SQLitExample extends Activity implements OnClickListener {

Button btnUpdate, btnView, btnPdf;
EditText SQLName,SQLNum;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqliteexample); 
    btnUpdate = (Button) findViewById(R.id.btnUpdate);
    SQLName = (EditText) findViewById(R.id.etSQLName);
    SQLNum = (EditText) findViewById(R.id.etSQLNum);

    btnView = (Button) findViewById(R.id.btnView);
    btnPdf = (Button) findViewById(R.id.btnPdf);
    btnPdf.setOnClickListener(this);        
    btnView.setOnClickListener(this);
    btnUpdate.setOnClickListener(this);
}


public void onClick(View arg0){
    switch (arg0.getId())
    {
    case R.id.btnUpdate:
        boolean diditwork = true;
        try{
        String name = SQLName.getText().toString();
        String num = SQLNum.getText().toString();

        Example entry = new Example(SQLitExample.this);
        entry.open();   
        entry.createEntry(name,num);
        entry.close();

        }catch(Exception e){
            diditwork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("OW men! :(");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{
            if(diditwork){
                Dialog d = new Dialog(this);
                d.setTitle("yehey");
                TextView tv = new TextView(this);
                tv.setText("Success");
                d.setContentView(tv);
                d.show();       
            }
        }
    break;

    case R.id.btnView:
        Intent i = new Intent("com.example.hotornot.SQLView");
        startActivity(i);
    break;

    case R.id.btnPdf:    
        Intent i2 = new Intent("com.example.hotornot.createPDF"); 
        startActivity(i2);
    break;
    }
}
}

这是我的 createPDF.java 代码:

package com.example.hotornot;

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.example.hotornot.*;
import com.example.hotornot.Example.DbHelper;

public class createPDF extends Activity {

String DATABASE_NAME = Example.DATABASE_NAME;
String DATABASE_TABLE = Example.DATABASE_TABLE;
String KEY_NAME = Example.KEY_NAME;
String KEY_NUM = Example.KEY_NUM;
String KEY_ROWID = Example.KEY_ROWID;

 public void pdf() throws ClassNotFoundException, SQLException, DocumentException{

    try {

        System.out.println("THIS SHOULD CREATE PDF!");
        Document document=new Document () {};
        PdfWriter.getInstance(document,new FileOutputStream("C:/Users/SERVER02/workspace/HotOrNot/samplePDF"));
        document.open();

        PdfPTable table = new PdfPTable(3);
        table.addCell("Row_Id");
        table.addCell("Name");
        table.addCell("Number");

        String[] columns = new String[]{ KEY_ROWID,KEY_NAME, KEY_NUM};

        Example entry = new Example(createPDF.this);
        Cursor c = entry.ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iNum = c.getColumnIndex(KEY_NUM);

        String rownum = c.getString(iRow);
        String name = c.getString(iName);
        String num = c.getString(iNum);

        for (c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()){
        result = result + rownum + " " + name + " " + num + "\n";
        table.addCell(c.getString(iRow));
        table.addCell(c.getString(iName));
        table.addCell(c.getString(iNum));
        }

        document.add(table);
        document.close();
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:\\samplePDF.pdf");
    } catch (IOException ex) {
        Logger.getLogger(createPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
}


}

最佳答案

在 Manifest.xml 文件中添加您要启动的其他 Activity - createPDF。

    <activity android:name=".createPDF" />

Intent语法如下,不是你用过的:

    Intent pdfIntent = new Intent(SQLitExample.this,createPDF.class);
    startActivity(pdfIntent);

Android 资源:

Intent Developer Docs

查看上面的 Intent 构造函数。您应该使用Intent(Context context, Class<?> cls)构造函数。

关于java - 如何在android中使用itext生成pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14212630/

相关文章:

java.lang.Exception : Test class should have exactly one public zero-argument constructor:

android - 应用程序图标丢失 Android

Android MediaPlayer 不播放声音

java - 停止此线程并更改 Activity

java - Android - 从Webview拦截链接

security - 防止PDF文件下载和打印

java - AJAX上传图片,不能用jQuery的serialize()方法?

java - <html :errors> struts tutorial or example

java - 为什么Java配置后找不到这个Spring Security AuthenticationProvider?

excel - 显示文本链接以从 Excel 打开特定的 PDF 页面