gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:32:38 31 4
gpt4 key购买 nike

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

}
}

最佳答案

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

关于android - 如何在android中将图像添加到具有多个页面的pdf中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17186854/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com