你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中生成PDF

[日期:2013-04-26] 来源:Linux社区  作者:拳四郎 [字体:大 中 小]

主界面就一个Button,按下之后就开始生产PDF。

package com.example.itexttest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Method;

import Android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ITextActivity extends Activity {
 private Button mButton;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_itext);
  mButton = (Button)findViewById(R.id.button1);
  mButton.setOnClickListener(new OnClickListenerImpl());
 }

 private class OnClickListenerImpl implements View.OnClickListener
 {

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   //Toast.makeText(getApplicationContext(), "Run", Toast.LENGTH_SHORT).show();
   // Create droidtext directory for storing results
   File file = new File(
     android.os.Environment.getExternalStorageDirectory()
     + File.separator + "iTextTest");
   if (!file.exists()) {
    file.mkdir();
   }
   System.out.println("Click!");

   Thread t = new Thread() {

    public void run() {
     int success = 0;
     int count = 1;
     String className = "com.example.itexttest.HelloWprld";

     String result = null;
     try {
      // Set output streams to bytearray streams so we can
      // display the output of examples
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      PrintStream errorStream = new PrintStream(bos, true);
      System.setErr(errorStream);

      ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
      PrintStream outStream = new PrintStream(bos2, true);
      System.setOut(outStream);

      // Find the main method
      Class<?> c = Class.forName(className);
      Method main = c.getDeclaredMethod("main",String[].class);
      System.out.println("GetMain"+main.getName());

      // Emulate CLI parameters if necessary
      String[] params = null;
      if (className
        .equals("com.lowagie.examples.objects.tables.pdfptable.FragmentTable")) {
       params = new String[] { "3" };
      } else if (className
        .equals("com.lowagie.examples.objects.images.tiff.OddEven")) {
       params = new String[] { "odd.tif", "even.tif",
       "odd_even.tiff" };
      } else if (className
        .equals("com.lowagie.examples.objects.images.tiff.Tiff2Pdf")) {
       params = new String[] { "tif_12.tif" };
      } else if (className
        .equals("com.lowagie.examples.objects.images.DvdCover")) {
       params = new String[] { "dvdcover.pdf", "Title",
         "0xff0000", "hitchcock.png" };
      } else if (className
        .equals("com.lowagie.examples.forms.ListFields")) {
       params = new String[] {};
      } else if (className
        .equals("com.lowagie.examples.general.read.Info")) {
       params = new String[] { "RomeoJuliet.pdf" };
      } else if (className
        .equals("com.lowagie.examples.objects.anchors.OpenApplication")) {
       params = new String[] { "" };
      }

      main.invoke(null, (Object) params);

      // Parse results
      String string = new String(bos.toByteArray());
      String string2 = new String(bos2.toByteArray());
      if (string.length() > 0) {
       result = "Failed: " + string;
      } else if (string2.contains("Exception")) {
       result = "Failed: " + string2;
      } else if ("Images.pdf" != null) {
       File pdf = new File(
         Environment.getExternalStorageDirectory()
         + File.separator + "iTextTest"
         + File.separator
         + "Images.pdf");
       System.out.println("Create Pdf@");
       if (!pdf.exists()) {
        result = "Failed: Resulting pdf didn't get created";
       } else if (pdf.length() <= 0) {
        result = "Failed: Resulting pdf is empty";
       } else {
        success++;
        result = "Successful";
       }
      } else {
       success++;
       result = "Successful";
      }
     } catch (Exception e) {
      result = "Failed with exception: "
        + e.getClass().getName() + ": "
        + e.getMessage();
      System.out.println(result);
     }
     if (result.startsWith("Failed")) {
      System.out.println("Failed!");
     } else {
      System.out.println("Success!");
     }
     System.out.println(result);
    }

   };
   t.start();
  }

 }

}

linux
相关资讯       iText  Android生成PDF 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款