【问题标题】:Display sqlite database content in pdf format in android在android中以pdf格式显示sqlite数据库内容
【发布时间】:2012-08-24 05:58:28
【问题描述】:

我在 Android 中有一个 Sqlite 数据库,我想在 PDF 文件中显示它的内容,方法是在 Android 中按下按钮时动态构建它。

我知道iText,但我想使用更简单的解决方案.. 谁能帮帮我!!

【问题讨论】:

    标签: android sqlite pdf


    【解决方案1】:

    查看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();
            }
    }
    

    【讨论】:

      【解决方案2】:

      要将Sqlite数据库的内容显示为pdf,你必须使用itextpdf-5.2.1.jar。你可以从here下载它

      示例代码:

      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();
      

      【讨论】:

        【解决方案3】:

        我尝试过 Ketul Patel 解决方案,该解决方案原则上没问题,但我需要更改其中的一些内容。我改变了在目录中创建文件的方式,我从here 得到了这个想法,并且效果很好。我的最终代码是:

        DatabaseHelper 是我在项目中创建的一个类,它扩展了 SQLiteOpenHelper。 read more

        public void createPdf() throws FileNotFoundException, DocumentException {
        
            String dir = Environment.getExternalStorageDirectory()+File.separator+"myLogs";
            File folder = new File(dir);
            folder.mkdirs();
        
            File file = new File(dir, "LogHistory.pdf");
        
        
            Cursor c1 = database.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_LOG, null);
            Document document = new Document();  // create the document
            PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();
        
            Paragraph p3 = new Paragraph();
            p3.add("Your Log History for \n");
            document.add(p3);
        
            PdfPTable table = new PdfPTable(4);
            table.addCell("Date");
            table.addCell("Start");
            table.addCell("End");
            table.addCell("Total");
        
            while (c1.moveToNext()) {
                String date = c1.getString(3);
                String start = c1.getString(1);
                String end = c1.getString(2);
                String total = c1.getString(4);
        
                table.addCell(date);
                table.addCell(start);
                table.addCell(end);
                table.addCell(total);
            }
        
            document.add(table);
            document.addCreationDate();
            document.close();
        }
        

        【讨论】:

          【解决方案4】:

          我已经在fpdf Tutorials 的帮助下完成了 php 中的 PDF 实现。有了这个,我还获得了饼图和条形图的帮助,以在我的 pdf 中进行图形表示。这种格式还简化了将我们的 pdf 文件作为附件邮寄的过程,因为它可以以多种方式存储。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-27
            • 1970-01-01
            • 2017-12-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多