android 文字或者图片生成.pdf文件

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


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值