【发布时间】:2013-12-06 05:48:29
【问题描述】:
我有一些 sqlite 数据,即我有一个表,我在一个活动中显示这个表。这个记录我必须以 pdf 格式保存并通过电子邮件发送给另一个人。如果有人知道将数据保存在pdf fromat 以便我可以将此 pdf 邮寄给任何我想要的人,然后请建议我。
提前谢谢...
【问题讨论】:
标签: android sqlite pdf-generation
我有一些 sqlite 数据,即我有一个表,我在一个活动中显示这个表。这个记录我必须以 pdf 格式保存并通过电子邮件发送给另一个人。如果有人知道将数据保存在pdf fromat 以便我可以将此 pdf 邮寄给任何我想要的人,然后请建议我。
提前谢谢...
【问题讨论】:
标签: android sqlite pdf-generation
尝试使用此代码通过从您要生成 PDF 的表中传递您的数据库值。这里传递一些字符串值,如段落,您可以根据您的要求更改它
package com.example;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button b;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
createPDF();
}
});
}
public void createPDF()
{
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "sample.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using DroidText");
Font paraFont= new Font(Font.COURIER);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
//add paragraph to document
doc.add(p1);
Paragraph p2 = new Paragraph("This is an example of a simple paragraph");
Font paraFont2= new Font(Font.COURIER,14.0f,Color.GREEN);
p2.setAlignment(Paragraph.ALIGN_CENTER);
p2.setFont(paraFont2);
doc.add(p2);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
//add image to document
doc.add(myImg);
//set footer
Phrase footerText = new Phrase("This is an example of a footer");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally
{
doc.close();
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Generate PDF" />
</RelativeLayout>
这将生成 PDF 文件,您可以附加并邮寄
要发送邮件,你可以试试这个代码:
String[] mailto = {"me@gmail.com"};
Uri uri = Uri.parse("PATH WHERE THE PDF FILE YOU GENERATED");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My Body");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email using:"));
【讨论】:
查看droidtext,它是适用于 Android 的 iText 库版本 2.1.7 的一个端口。
也有很多例子。开始使用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();
}
}
【讨论】:
您可以使用 iText For android,它非常简单易用,并且有很多教程可用。喜欢这个http://www.vogella.com/articles/JavaPDF/article.html
【讨论】:
您可以使用iText android 库。我认为这是最好的一个,它提供了以编程方式在android 中编写pdf 文件的工具。
首先,您必须在 Some ArrayList 或 Model 类中获取 SQLite 中的所有数据。
之后,您需要将该数据写入iText Library。
使用iText Library,你可以做很多事情,比如pdf的Bold、Italic或Underline字体。您几乎可以完成我们需要在 pdf 文件中完成的所有任务。
这里RoseIndia.Net 为不知道iText 的人提供了最好的例子
祝你好运
【讨论】: