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

时间:2012-10-11 15:51:10

标签: 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 个答案:

答案 0 :(得分:2)

由于两个原因,我投了这个问题:

  1. 您没有阅读官方文档。请参阅Edit DirectContent of iTextSharp PdfSmartCopy class,了解阅读文档错误的原因。
  2. 我是iText的原始开发者,我不赞同使用DroidText。这不是官方的iText版本。我强烈反对使用它:http://lowagie.com/itext2请注意,我住在比利时,根据比利时法律,我对我制作的所有版权享有道德权利。这包括iText 2.1.7。
  3. 至于你的问题:你正在创建A4格式的页面。在这些页面中,您可以添加未知大小的导入页面。如果这些页面大小也是A4,那么它们就适合。如果他们有不同的尺寸,他们就不会。要么他们会被剪裁,要么他们会有不必要的边缘。

相关问题