Приветствую вас, используя нижеприведенный код. Я могу добавить изображения в pdf-файл. В моем случае у меня есть 3 изображения. Я добавляю 3 изображения в pdf, теперь отображаются все три изображения в PDF-файл одной страницы, но мне нужно, чтобы отобразить 3 изображения в 3-х страниц в PDF-файл, ниже мой код вы можете любой предложить мне спасибо ..Как добавить изображения в pdf с несколькими страницами в android
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();
}
}
}