我正在使用库
pdf
在android中生成。pdf的格式显示在图像中
我用来生成这种格式的方法正在使用。itext
Pdfptable
描述标签是动态的,意味着它可以是多行的。当描述标签为 4-5 行时,一切都是完美的,但如果它是半页,则 PDF 看起来不太好,因为所有不必要的空白;见sample.pdf。
// TODO Auto-generated method stub
Document document = new Document(PageSize.A4);
File f = null;
try {
f = createFile("sample.pdf");
FileOutputStream ficheroPdf = new FileOutputStream(
f.getAbsolutePath());
PdfWriter writer = PdfWriter.getInstance(document, ficheroPdf);
document.open();
/*LineSeparator line = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
paragraph.add(line);*/
ArrayList<SampleModel> movies = new ArrayList<SampleModel>(15);
for (int i = 0; i < 13; i++) {
movies.add(new SampleModel());
}
for (int j = 0; j < movies.size(); j++) {
Bitmap bitmap = BitmapFactory.decodeResource(mActivity.getResources(),
movies.get(j).getDrawableId());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image img = Image.getInstance(stream.toByteArray());
img.setAlignment(Image.LEFT | Image.TEXTWRAP);
img.setBorder(Image.BOX);
img.setBorderWidth(20);
img.setBorderColor(Color.WHITE);
img.scaleAbsolute(200f, 200f);
//table with 2 columns
PdfPTable table = new PdfPTable(2);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell cell = new PdfPCell(img);
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(16777215));
cell.setRowspan(5);
table.addCell(cell);
table.addCell(new Paragraph(" Snag Name"));
table.addCell(new Paragraph(" Date : 25 Aug 2015"));
table.addCell(new Paragraph(" Sub Contractor : Test company 2"));
table.addCell(new Paragraph(" Status : Completed"));
table.addCell(new Paragraph(" Description : Description Description Description Description Description Description Description Description Description Description Description Description "
+ "Description"
+ "Description"
+ "Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description" ));
document.add(table);
// add line seperator
document.add(Chunk.NEWLINE);
LineSeparator line = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
document.add(line);
document.add(Chunk.NEWLINE);
}
Toast.makeText(mActivity, "Pdf generated", Toast.LENGTH_SHORT).show();
} catch (DocumentException e) {
Log.e("pdf error", e.getMessage());
} catch (IOException e) {
Log.e("pdf error", e.getMessage());
} finally {
document.close();
try {
File file = f;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(mActivity, "No app found to open pdf", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(mActivity, "Exception", Toast.LENGTH_SHORT).show();
}
}
`
介绍之后setSplitLate()
,我的 PDF 看起来像这个sample_new.pdf,这也不令人满意。