如何利用iText生成在android系统PDF [英] how to generate pdf in android using itext
问题描述
进出口新的android开发,短短两天老,我想通过在PDF格式显示它检索我的数据库中的数据。我的NetBeans(它的工作)之前完成这一项,但是,当我想我的code android系统中它给了我一个错误。我有应该创造我的PDF的SQLitExample.java文件,它触发动作对我btnPdf和createPDF.java文件。我打过电话通过创建一个新的意图createPDF.java文件。但是当我点击我的btnPdf它给了我这个错误:
dalvikvm(6097):找不到类的com.example.hotornot.createPDF $ 1',从方法com.example.hotornot.createPDF.pdf引用
块引用>下面是我的SQLitExample.java code
包com.example.hotornot;进口java.io.IOException异常;
进口值java.sql.SQLException;进口com.lowagie.text.DocumentException;进口android.app.Activity;
进口android.app.Dialog;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;公共类SQLitExample扩展活动实现OnClickListener {按钮btnUpdate,btnView,btnPdf;
的EditText SQLName,SQLNum;@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.sqliteexample);
btnUpdate =(按钮)findViewById(R.id.btnUpdate);
SQLName =(EditText上)findViewById(R.id.etSQLName);
SQLNum =(EditText上)findViewById(R.id.etSQLNum); btnView =(按钮)findViewById(R.id.btnView);
btnPdf =(按钮)findViewById(R.id.btnPdf);
btnPdf.setOnClickListener(本);
btnView.setOnClickListener(本);
btnUpdate.setOnClickListener(本);
}
公共无效的onClick(查看为arg0){
开关(arg0.getId())
{
案例R.id.btnUpdate:
布尔diditwork = TRUE;
尝试{
字符串名称= SQLName.getText()的toString()。
。字符串NUM = SQLNum.getText()的toString(); 示例条目=新实例(SQLitExample.this);
entry.open();
entry.createEntry(名称,NUM);
entry.close(); }赶上(例外五){
diditwork = FALSE;
字符串错误= e.toString();
对话D =新的对话框(本);
d.setTitle(OW男:(!);
TextView的电视=新的TextView(本);
tv.setText(错误);
d.setContentView(电视);
d.show();
}最后{
如果(diditwork){
对话D =新的对话框(本);
d.setTitle(yehey);
TextView的电视=新的TextView(本);
tv.setText(成功);
d.setContentView(电视);
d.show();
}
}
打破; 案例R.id.btnView:
意图I =新意图(com.example.hotornot.SQLView);
startActivity(ⅰ);
打破; 案例R.id.btnPdf:
意图I2 =新意图(com.example.hotornot.createPDF);
startActivity(I 2);
打破;
}
}
}这是我的createPDF.java code:
包com.example.hotornot;进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口java.sql.Connection中;
进口java.sql.DriverManager中;
进口java.sql.ResultSet中;
进口值java.sql.SQLException;
进口java.sql.Statement中;
进口java.util.logging.Level中;
进口java.util.logging.Logger中;进口android.app.Activity;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;
进口android.widget.TextView;进口com.lowagie.text.Cell;
进口com.lowagie.text.Document;
进口com.lowagie.text.DocumentException;
进口com.lowagie.text.Element;
进口com.lowagie.text.Font;
进口com.lowagie.text.FontFactory;
进口com.lowagie.text.Paragraph;
进口com.lowagie.text.Table;
进口com.lowagie.text.pdf.PdfPCell;
进口com.lowagie.text.pdf.PdfPTable;
进口com.lowagie.text.pdf.PdfWriter;
导入com.example.hotornot *。
进口com.example.hotornot.Example.DbHelper;公共类createPDF延伸活动{字符串DATABASE_NAME = Example.DATABASE_NAME;
字符串DATABASE_TABLE = Example.DATABASE_TABLE;
字符串KEY_NAME = Example.KEY_NAME;
字符串KEY_NUM = Example.KEY_NUM;
字符串KEY_ROWID = Example.KEY_ROWID; 公共无效PDF()抛出ClassNotFoundException的,的SQLException,DocumentException { 尝试{ 的System.out.println(这应该创建一个PDF!);
文档的文档=新的文件(){};
PdfWriter.getInstance(文件,新的FileOutputStream(C:/用户/ SERVER02 /工作区/ HotOrNot / samplePDF));
document.open(); PdfPTable表=新PdfPTable(3);
table.addCell(ROW_ID);
table.addCell(姓名);
table.addCell(编号); 的String [] =列新的String [] {KEY_ROWID,KEY_NAME,KEY_NUM}; 示例条目=新实例(createPDF.this);
光标C = entry.ourdatabase.query(DATABASE_TABLE,列,NULL,NULL,NULL,NULL,NULL);
字符串结果=; INT iRow = c.getColumnIndex(KEY_ROWID);
INT INAME = c.getColumnIndex(KEY_NAME);
INT INUM = c.getColumnIndex(KEY_NUM); 字符串的rownum = c.getString(iRow);
字符串名称= c.getString(INAME);
字符串NUM = c.getString(INUM); 为(c.moveToFirst();!c.isAfterLast(); c.moveToNext()){
结果=结果+ ROWNUM ++姓名++ NUM +\\ n;
table.addCell(c.getString(iRow));
table.addCell(c.getString(INAME));
table.addCell(c.getString(INUM));
} document.add(表);
document.close();
调用Runtime.getRuntime()EXEC(RUNDLL32 URL.DLL,FileProtocolHandler+C:\\\\ samplePDF.pdf)。
}赶上(IOException异常前){
。Logger.getLogger(createPDF.class.getName())日志(Level.SEVERE,空,前);
}
}
}
解决方案添加你想要启动的其他活动 - 在Manifest.xml文件createPDF
<活动机器人:createPDFNAME = />
和意图的语法如下,不是你所使用的:
意图pdfIntent =新意图(SQLitExample.this,createPDF.class);
startActivity(pdfIntent);Android的资源:
退房的意向构造之上。您应该使用
意图。(上下文的背景下,类<> CLS)
构造Im new to android development,just two days old and I want to retrieve data from my database by displaying it on pdf format. I've done this one before on netbeans(which worked), but when i tried my code in android it gives me an error. i have the SQLitExample.java file which triggers the action for my btnPdf, and createPDF.java file which should create my pdf. I tried calling the createPDF.java file by creating a new intent. but when i click my btnPdf it gives me this error:
dalvikvm(6097): Could not find class 'com.example.hotornot.createPDF$1', referenced from method com.example.hotornot.createPDF.pdf
Here is my code on 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; } } }
here is my createPDF.java code:
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); } } }
解决方案Add the other activity you want to start - createPDF in the Manifest.xml file.
<activity android:name=".createPDF" />
And Intent syntax is as follows, not the one you have used:
Intent pdfIntent = new Intent(SQLitExample.this,createPDF.class); startActivity(pdfIntent);
Android resource:
Check out the Intent constructor above. You should use the
Intent(Context context, Class<?> cls)
constructor.这篇关于如何利用iText生成在android系统PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!