【问题标题】:PDF size not showing 100%PDF 大小未显示 100%
【发布时间】:2016-06-02 10:14:53
【问题描述】:

我通过电子邮件以 PDF 格式共享图像,但当我以 PDF 格式共享时,PDF 大小未完全显示,只有部分 PDF 显示。这是我用来共享为 PDF 的代码,请帮助我在这里停留了一段时间,我愿意接受立即有效的答案。

private void sharePdf() {
    try {
        Bitmap bitmap;
        View v1;
        // File path;
        File att = null;
        root = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name));
        if (!root.exists())
            root.mkdir();
        {
            menu_layout.setVisibility(View.GONE);
            bottom_layout.setVisibility(View.GONE);
            menus.setVisibility(View.GONE);
            v1 = rl_layout.getRootView();
            v1.setDrawingCacheEnabled(true);

            bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
            time = new SimpleDateFormat("yyyy-MM-dd_HHmmss", Locale.ENGLISH).format(new Date());
            path1 = new File(root.getAbsolutePath() + File.separator + time + ".png");
            path1.createNewFile();
            FileOutputStream fo = new FileOutputStream(path1);
            fo.write(bytes.toByteArray());
            fo.close();

            Bitmap bgBit = null;
            try {
                Uri uri = Uri.fromFile(new File(custombackground));
                if (uri != null && custombackground != null && !custombackground.trim().equalsIgnoreCase("")) {
                    bgBit = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                }
            } catch (Exception ex) {

            }
            if (bgBit != null) {
                bitmap = overlay(bgBit, drawingPanelView.getBitmap());
                bytes = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
            }

            att = new File(root.getAbsolutePath() + File.separator
                    + "note.png");
            if (att.exists()) {
                att.delete();
            }
            att.createNewFile();
            FileOutputStream fo_att = new FileOutputStream(att);
            fo_att.write(bytes.toByteArray());
            fo_att.close();

            menu_layout.setVisibility(View.VISIBLE);
            bottom_layout.setVisibility(View.VISIBLE);
            menus.setVisibility(View.VISIBLE);
        }

        File pdfFile = new File(root.getAbsolutePath() + File.separator + "share_temp.pdf");
        if (!pdfFile.exists()) {
            pdfFile.createNewFile();
        }
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(pdfFile.getAbsoluteFile()));
        document.open();
        document.add(new Paragraph(content.getText().toString()));

        Image image = Image.getInstance(att.getAbsolutePath());
        document.add(image);
        for (HashMap<String, String> item : list) {
            List<String> vales = new ArrayList<>(item.values());
            for (String path : vales) {
                File fileIn = new File(path);
                if (fileIn.exists()) {
                    image = Image.getInstance(fileIn.getAbsolutePath());
                    document.add(image);
                }
            }
        }

        document.close();


        final Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/plain");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
        emailIntent.putExtra(android.content.Intent.EXTRA_CC, "");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pdfFile));
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));

    } catch (Exception e) {
    }

}

【问题讨论】:

  • 您不需要ByteArrayOutputStreampath1.createNewFile(); 调用。直接压缩到FileOutputStream即可。
  • @EJP 你可以通过编辑代码来回答,所以如果它有效,我可以接受答案。谢谢
  • 如果我认为这是一个答案,我会将其发布为答案,不,我不会编辑您的代码。改变是微不足道的。

标签: java android pdf bitmap itext


【解决方案1】:

实际上,您必须将图像缩放到文档的宽度和高度以适合文档来执行此操作,只需在代码中添加以下行。

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

在添加图片之前,先缩放该图片

image.scalePercent(scaler);

然后将此图像添加到文档中

document.add(image);

【讨论】:

  • @rekesh,你太棒了!!你是上帝派来给我的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多