繁体   English   中英

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

[英]How to make pdf image fit the whole screen android

我正在使用这个 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); 诀窍(我必须玩数字以适应页面)。 质量图像的缺点是可怕的......

实用工具

    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;
}

而不是image.scaleAbsolute(width,height)试试image.scaleToFit(width,height)

我认为你想要做的是改变大小

    Rectangle documentRect = document.getPageSize();

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

如果我正确理解问题,我会使用 PDF24 Creator。

来自这个答案的解决方案。

我改变了image.scaleAbsolute(bmp.getWidth(), bmp.getHeight()); image.scalePercent(scaler); ofc 你必须包括

float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
                    - document.rightMargin() - indentation) / image.getWidth()) * 100;

现在它适合。 虽然图像质量并不完美。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM