1

嗨朋友们,使用下面的代码我可以将图像添加到 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();
     }

}      
 }
4

1 回答 1

1

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

于 2013-06-20T00:56:42.367 回答