如何以Pdf格式保存Sqlite数据

时间:2013-12-06 05:48:30

标签: android sqlite pdf-generation

我有一些sqlite数据集,即我有一个表,而这个表我在一个活动中有所不同。这个记录我必须以pdf格式保存并发送给另一个人。如果有人知道保存数据pdf fromat所以我可以将这个pdf邮寄给任何我想要的人,然后请建议我。

提前致谢...

4 个答案:

答案 0 :(得分:1)

查看droidtext iText库版本2.1.7 for Android的端口。

也有很多例子。开始使用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();
        }
}

答案 1 :(得分:1)

尝试使用此代码通过从您要生成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();
         }

    }      
}

activity_main.xml中

<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:"));

答案 2 :(得分:0)

你可以使用iText对于android它非常简单易用,有很多教程可供选择。像这样http://www.vogella.com/articles/JavaPDF/article.html

答案 3 :(得分:0)

你可以使用iText android库。我认为这是最好的一个,它提供了在android上编写pdf文件的工具。

首先,您必须获取某些SQLiteArrayListModel中的所有数据。

之后,您需要在iText Library

中写入该数据

使用iText Library,您可以执行许多内容,例如pdf的BoldItalicUnderline字体。你可以完成我们在pdf文件中要做的几乎所有任务。

此处RoseIndia.Net为谁不知道iText

提供了最好的例子

祝你好运