您的位置:首页 > 移动开发 > Android开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: