【问题标题】:How to make pdf image fit the whole screen android如何使pdf图像适合整个屏幕android
【发布时间】: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


【解决方案1】:

代替image.scaleAbsolute(width,height) 试试image.scaleToFit(width,height)

【讨论】:

    【解决方案2】:

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

        Rectangle documentRect = document.getPageSize();
    

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

    【讨论】:

    • 不幸的是它没有帮助。
    • 您是否尝试过不仅更改 Document 的数量 document = new Document(PageSize.A4, 38, 38, 50, 38);或 image.setBorderWidth(15);还要将 PageSize 更改为 A0?
    • 很遗憾,我无法自己测试它,因为此应用无法在我的模拟器上运行
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      来自this answer的解决方案。

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

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

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

      【讨论】:

      • 有一个 scaleToFit 可以做大致相同的事情,但您无需手动计算。
      猜你喜欢
      • 2015-01-27
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2014-01-30
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      相关资源
      最近更新 更多