android - 使用白色位置的 PDF 文件结果生成新 PDF

标签 android pdf-generation

我没有给这些问题适当的标题。 :)

我需要如何从现有 PDF 文件中拆分(获取)页面。为此,我正在使用 droidtext。

我的代码是

  try {

        String path = Environment.getExternalStorageDirectory()
                + "/test123.pdf";
                    /*Read Existing PDF*/ 
        PdfReader reader = new PdfReader(path);

        Document doc = new Document();
        doc.open();

        File outfile = new File(Environment.getExternalStorageDirectory()
                + "/test_new.pdf");
        if (!outfile.exists())
            outfile.createNewFile();

        FileOutputStream decfos = new FileOutputStream(outfile);

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, decfos);
        document.open();
        /*Getting First page*/  
        PdfImportedPage page = writer.getImportedPage(reader, 1);

        Image instance = Image.getInstance(page);

        document.add(instance);
        document.close();

    } catch (DocumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我想从“test123.pdf”文件创建一页 pdf。它正在创建新的 PDF。

但问题是在新的 PDF 文件中有白色边框。我怎样才能删除这些空格。 在原始 PDF 中没有这样的白色边框。

enter image description here

编辑 我再次尝试使用以下代码。但它在 copy.addPage(page);

给出空指针异常
String path = Environment.getExternalStorageDirectory()
                + "/test123.pdf";
        PdfReader reader = new PdfReader(path);

        PdfImportedPage page;
        PdfSmartCopy.PageStamp stamp;
        File outfile = new File(Environment.getExternalStorageDirectory()
                + "/test_new.pdf");
        Document doc = new Document();
        if (!outfile.exists())
            outfile.createNewFile();

        FileOutputStream decfos = new FileOutputStream(outfile);
        PdfSmartCopy copy = new PdfSmartCopy(doc, decfos);
        page = copy.getImportedPage(reader, 5);

        stamp = copy.createPageStamp(page);

        stamp.alterContents();
        copy.addPage(page);

最佳答案

我否决了这个问题有两个原因:

  1. 您没有阅读官方文档。参见 Edit DirectContent of iTextSharp PdfSmartCopy class找出为什么不阅读文档是错误的。
  2. 我是 iText 的原始开发者,我不赞成使用 DroidText。这不是正式的 iText 版本。我强烈反对使用它:http://lowagie.com/itext2请注意,我住在比利时,根据比利时法律,我对我制作的所有版权拥有精神权利。这包括 iText 2.1.7。

至于您的问题:您正在创建 A4 格式的页面。您向这些页面添加了未知大小的导入页面。如果这些页面也是 A4 尺寸,它们将适合。如果它们的尺寸不同,则不会。它们要么被剪裁,要么会有不必要的边距。

关于android - 使用白色位置的 PDF 文件结果生成新 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12843623/

相关文章:

javascript - 在 React Native 的 Webview 中嵌入 youtube 视频

android - 对齐选定的微调项

java - Android打开失败: EACCES (Permission denied) when writing file in data/data/package/files

pdf-generation - 将 html 转换为 PDF 时,iTextSharp 中未设置表格宽度

c# - 寻找适用于 Windows 8 Metro 应用程序的 PDF 编写器

java - 如何制作像这样的自定义导航 View 动画

android - ContentResolver 是否通知所有路径变体?

java - 使用 Spring Boot 在单个 PDF 中渲染表格和图表

java - 如何使用 chunk.setUnderline 实现虚线下划线?

java - 将 PDF/A-1b 转换为 PDF/A-2