1
我正在使用iText庫來生成PDF文檔。在這樣的PDF中,我每頁添加多個圖像。Android:如何設置圖像大小將與文檔頁面大小相同?
下面是生成PDF的代碼。
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.setPageEvent(new PageWithRectangle());
document.open();
for (int i = 0; i < listSelected.size(); i++) {
//PdfPTable table = new PdfPTable(1);
//table.setWidthPercentage(100);
document.newPage();
Bitmap bMap = BitmapFactory.decodeFile(listSelected.get(i));
// Bitmap rotated = Bitmap.createBitmap(bMap, 0, 0,
// bMap.getWidth(),bMap.getHeight(), null, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bMap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setBorder(Image.BOX);
image.setBorderWidth(10);
float documentWidth = document.getPageSize().getWidth()
- document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().getHeight()
- document.topMargin() - document.bottomMargin();
image.scaleToFit(documentWidth, documentHeight);
Log.e("Document - Image = Height", document.getPageSize().getHeight()+" - "+image.getScaledHeight());
float leftMargin = document.getPageSize().getWidth() - image.getScaledWidth();
float lMargin = leftMargin/2 ;
float topMargin = document.getPageSize().getHeight() - image.getScaledHeight();
float tMargin = topMargin/2 ;
image.setAbsolutePosition(lMargin,tMargin);
/*PdfPCell cell = new PdfPCell(image);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);*/
document.add(image);
}
document.close();
- 是否可以設置頁面大小是相同的圖像大小,因此,有沒有白色的頂部和底部邊緣?
- 如何將頁面設置爲LANDSCAPE?
可能重複的[寫入圖像到PDF文件在java](http://stackoverflow.com/questions/16786026/writing-image-into-pdf-file-in -java) –