iTextSharp - 裁剪PDF文件(C#) [英] iTextSharp - Crop PDF File (C#)

查看:1632
本文介绍了iTextSharp - 裁剪PDF文件(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用iTextSharp和矩形(0,1000,600,155)裁剪PDF文件。一切都很好,当你打开创建的* .pdf文件时,你只能看到那个裁剪的内容,但是!如果您解析该pdf,仍然有来自文档不可见部分的信息和文本,我不能接受。如何完整地删除该数据?

I want to crop PDF File using iTextSharp and rectangle (0,1000,600,155). Everything is fine and when you open created *.pdf file you can see only that cropped content, BUT! If you parse that pdf, there are still information and text from not visible part of document, I can't accept that. How can I remove that data completly?

这是我的代码示例:

        static void cropiTxtSharp(){
        string file ="C:\\testpdf.pdf";
        string oldchar = "testpdf.pdf";
        string repChar = "test.pdf";
        PdfReader reader = new PdfReader(file);
        PdfDictionary pageDict;
        PdfRectangle rect = new PdfRectangle(0, 1000, 600, 115);
        pageDict = reader.GetPageN(1);
        pageDict.Put(PdfName.CROPBOX, rect);
        PdfStamper stamper = new PdfStamper(reader, new FileStream(file.Replace(oldchar, repChar), FileMode.Create, FileAccess.Write));
        stamper.Close();
        reader.Close();
    }

编辑:
这是有效的代码,我花了几个小时但最后我做到了:P

首先,在项目中添加以下内容:

Here is code which works, I spend some hours but finally I did it :P
First, add following to project:

using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup;


然后你可以使用我的代码:


Then you can use my code:

    static void textsharpie()
    {
        string file = "C:\\testpdf.pdf";
        string oldchar = "testpdf.pdf";
        string repChar = "test.pdf";
        PdfReader reader = new PdfReader(file);
        PdfStamper stamper = new PdfStamper(reader, new FileStream(file.Replace(oldchar, repChar), FileMode.Create, FileAccess.Write));
        List<PdfCleanUpLocation> cleanUpLocations = new List<PdfCleanUpLocation>();
        cleanUpLocations.Add(new PdfCleanUpLocation(1, new iTextSharp.text.Rectangle(0f, 0f, 600f, 115f), iTextSharp.text.BaseColor.WHITE));
        PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
        cleaner.CleanUp();
        stamper.Close();
        reader.Close();
    }

如果我想要的话,我不能使用该代码商业化我的应用程序而无需支付许可证,所以我不得不考虑不同的库......


Unfortunatelly I can't use that code if I want to commercialize my application without paying for license, so I had to think on different library...

推荐答案

你所做的是设置页面的CropBox,它对文档的内容完全没有任何作用。这是设计上的,并且自从Acrobat 1.0以来总是如此。

What you're doing is setting the CropBox of the page, which does absolutely nothing to the content of the document. This is by design and was always like that since Acrobat 1.0.

您想要做的事情被称为编辑(或者在您的情况下,由于您想要删除所有内容而进行独占编辑)在矩形的边界之外)。正确地做是非常重要的,主要是因为内容的问题部分地与想要编辑的边界重叠(图像,文本和路径)。

What you want to do is called redaction (or in your case, exclusive redaction since you want to remove everything outside the bounds of a rectangle). It is decidedly non-trivial to do correctly, mostly because of issues with content that partially overlaps the bounds to which to want to redact (images, text, and paths).

这篇关于iTextSharp - 裁剪PDF文件(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆