如何使pdf图像适合整个屏幕android

问题描述 投票:0回答:1

我正在使用this depo git应用程序,该应用程序允许您捕获或选择图像并将其保存在pdf文档中。除了保存的图像不适合整个屏幕之外,它的工作效果非常好。

enter image description here

所以我尝试更改这些数字Document document = new Document(PageSize.A4, 38, 38, 50, 38);image.setBorderWidth(15);

(documentRect.getWidth() - image.getScaledWidth()) / 2,
(documentRect.getHeight() - image.getScaledHeight()) / 2);

但没有运气...

此外,作者在布局上未使用任何imageview,因此我不得不进行有问题的编辑。有什么想法吗?

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;
}
java android itext pdf-generation
1个回答
0
投票

我认为您想要做的是更改大小

    Rectangle documentRect = document.getPageSize();

矩形具有四个可以在构造上设置的参数(x,y,宽度,高度)更改这些可能会解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.