Android に Sqlite データベースがあり、ボタンを押すと Android で動的に構築することにより、そのコンテンツを PDF ファイルに表示したいと考えています。
私はiTextを知っていますが、もっと簡単な解決策を取りたいと思っています..誰でも私を助けてくれますか?
Android 用の iText ライブラリ バージョン 2.1.7 のポートであるdroidtextを見てください。
例もたくさんあります。Helloworldから始めましょう。
public class HelloWorld {
/**
* Generates a PDF file with the text 'Hello World'
*
* @param args
* no arguments needed here
*/
public static void main(String[] args) {
System.out.println("Hello World");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "HelloWorld.pdf"));
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
document.add(new Paragraph("Hello World"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
}
Sqlite データベースのコンテンツを PDF に表示するには、itextpdf-5.2.1.jar を使用する必要があります。ここからダウンロードできます。
コード例:
DatabaseHandlerofdatabase dbHandler = new DatabaseHandlerofdatabase(this);
SQLiteDatabase db = dbHandler.getWritableDatabase();
Cursor c1 = db.rawQuery("SELECT * FROM tablename", null);
String filename="nameoffile.pdf";
Document document=new Document(); // create the document
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs(); // create root directory in sdcard
}
File gpxfile = new File(root,filename); // generate pdf file in that directory
PdfWriter.getInstance(document,new FileOutputStream(gpxfile));
document.open(); // open the directory
Paragraph p3=new Paragraph(); // to enter value you have to create paragraph and add value in it then paragraph is added into document
p3.add("Username : ");
document.add(p3);
// now for ad table in pdf use below code
PdfPTable table = new PdfPTable(3); // Code 1
// Code 2
table.addCell("CATEGORY");
table.addCell("BUDGET");
table.addCell("USED BUDGET");
// now fetch data from database and display it in pdf
while (c1.moveToNext()) {
// get the value from database
String ex_bdgt = c1.getString(3);
String used_bdgt = c1.getString(5);
table.addCell(type);
table.addCell(ex_bdgt);
table.addCell(used_bdgt);
int temp_ex=Integer.parseInt(ex_bdgt);
ttlbud=ttlbud+temp_ex;
int temp_used=Integer.parseInt(used_bdgt);
usdbud=usdbud+temp_used;
}
// add table into document
document.add(table);
document.addCreationDate();
document.close();
fpdf Tutorialsの助けを借りて、php での PDF 実装を完了しました。これにより、pdf でのグラフィカルな表現のための円グラフと棒グラフのヘルプも得られました。また、この形式はさまざまな方法で保存できるため、pdf ファイルを添付ファイルとして簡単に送信できます。