如何在android中将图像添加到带有多个页面的pdf中

时间:2013-06-19 08:50:19

标签: android android-emulator

嗨朋友使用下面的代码我能够将图像添加到pdf文件中,在我的情况下我有3个图像我将3个图像添加到pdf中,现在所有三个图像都显示在pdf文件单页中,但我需要显示将3张图片分成3页进入pdf文件,下面是我的代码,你能不能建议我谢谢你。

 MainActivity.class:
    public class MainActivity extends Activity {
Integer imagesf[] = { R.drawable.apple_50, R.drawable.ic_launcher};
Bitmap bitmap;
Image myImg;
private Button b;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b= (Button)findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            createPDF();

        }
    });

}


public void createPDF()
{
    Document doc = new Document();


     try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";

            File dir = new File(path);
                if(!dir.exists())
                    dir.mkdirs();

            Log.d("PDFCreator", "PDF Path: " + path);


            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");
            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.bg_w);
             bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
             Image myImg = Image.getInstance(stream.toByteArray());
             myImg.setAlignment(Image.MIDDLE);
                  doc.add(myImg);

             for(int i=0;i<imagesf.length;i++)
               {
                  ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
                bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), imagesf[i]);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream2);
                 myImg = Image.getInstance(stream2.toByteArray());
                myImg.setAlignment(Image.MIDDLE);
                doc.add(myImg);

                }

            } catch (DocumentException de) {
             Log.e("PDFCreator", "DocumentException:" + de);
     } catch (IOException e) {
             Log.e("PDFCreator", "ioException:" + e);
     } 
     finally
     {
             doc.close();
     }

}      
 }

1 个答案:

答案 0 :(得分:1)

使用Document.newPage()在插入每个图像之前开始新页面