主界面就一个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();
}
}
}
