【发布时间】:2020-05-21 16:32:58
【问题描述】:
我正在使用this depo git 应用程序,该应用程序允许您捕获或选择图像并将它们保存在 pdf 文档中。它工作得很好,只是保存的图像不适合整个屏幕。
所以我尝试更改这些数字 Document document = new Document(PageSize.A4, 38, 38, 50, 38); 或 image.setBorderWidth(15);
(documentRect.getWidth() - image.getScaledWidth()) / 2,
(documentRect.getHeight() - image.getScaledHeight()) / 2);
但没有运气......
此外,作者没有在布局上使用任何imageview,所以我必须进行有问题的编辑。有什么想法吗?
更新: 显然将 image.scaleAbsolute(bmp.getWidth(), bmp.getHeight()); 更改为
image.scaleAbsolute(500f, 500f); 或 image.scalePercent(500f); 可以解决问题(我必须使用数字来适应页面)。
图像质量的缺点是可怕的......
PdfUtils
public static final String LOG_ACTIVITY = "PdfUtils";
public static String ImgPdf(Activity activity, ArrayList<String> listPathImg,String folder, String pdfName) {
String result ="";
Image image;
String path = FileUtils.createFolderApp(folder);
path = path + pdfName + ".pdf";
Document document = new Document(PageSize.A4, 38, 38, 50, 38);
Log.v(LOG_ACTIVITY, "Document Created");
Rectangle documentRect = document.getPageSize();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
document.open();
for (int i = 0; i < listPathImg.size(); i++) {
Bitmap bmp = MediaStore
.Images
.Media
.getBitmap(
activity.getContentResolver(),
Uri.fromFile(new File(listPathImg.get(i))));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 70, stream);
image = Image.getInstance(listPathImg.get(i));
if (bmp.getWidth() > documentRect.getWidth() || bmp.getHeight() > documentRect.getHeight()) {
//bitmap is larger than page,so set bitmap's size similar to the whole page
image.scaleAbsolute(documentRect.getWidth(), documentRect.getHeight());
} else {
//bitmap is smaller than page, so add bitmap simply.
//[note: if you want to fill page by stretching image,
// you may set size similar to page as above]
image.scaleAbsolute(bmp.getWidth(), bmp.getHeight());
}
image.setAbsolutePosition(
(documentRect.getWidth() - image.getScaledWidth()) / 2,
(documentRect.getHeight() - image.getScaledHeight()) / 2);
image.setBorder(Image.BOX);
image.setBorderWidth(15);
document.add(image);
document.newPage();
}
result=path;
} catch (Exception err) {
err.printStackTrace();
result="";
} finally {
document.close();
}
return result;
}
【问题讨论】:
-
代码中甚至还有一条注释告诉您如何填充页面。
-
@M.Prokhorov 我试过这个建议没有帮助
-
您并没有说您尝试过 - 只是您尝试更改边框或绝对位置。
-
是的,抱歉。我可能做错了
标签: java android itext pdf-generation