android 文字或者图片生成.pdf文件
2014-03-07 13:50
507 查看
public void createPDF()
{
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
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----joe");
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.pdf);
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) {
} catch (IOException e) {
}
finally
{
doc.close();
}
}
类库:http://download.csdn.net/detail/u011636207/7005107
{
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
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----joe");
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.pdf);
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) {
} catch (IOException e) {
}
finally
{
doc.close();
}
}
类库:http://download.csdn.net/detail/u011636207/7005107
相关文章推荐
- Android开发之Intent.Action
- android APK签名汇总整理
- android4.2源码编译调试出错
- Android瀑布流照片墙实现,体验不规则排列的美感
- Android照片墙应用实现,再多的图片也不怕崩溃
- android 开发入门(一)
- android 输入系统
- android APK签名过程之CERT.SF分析
- Android中图表AChartEngine学习使用与例子
- Android中图表AChartEngine学习使用与例子
- Android应用开发SharedPreferences存储数据的使用方法
- Android 进程和线程详解
- 编译Android4.4系统总结
- Android学习笔记(四十):Preference的使用
- Android Bind Service 使用Messenger
- 在android上使用valgrind检测内存泄漏
- 关于Android 获得图片的总结
- android学习和广告平台赚钱
- 编译不同类型Android程序时,Android.mk文件的定义
- android 程序中选择图片的代码