iTextSharp - 裁剪 PDF 文件 (C#)

raf*_*wpt 5 c# pdf itext

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

这是我的代码示例:

        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();
    }
Run Code Online (Sandbox Code Playgroud)

编辑:这是有效的代码,我花了几个小时,但最终我做到了:P
首先,将以下内容添加到项目中:

using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup;
Run Code Online (Sandbox Code Playgroud)


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

    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();
    }
Run Code Online (Sandbox Code Playgroud)


不幸的是,如果我想在不支付许可证的情况下将我的应用程序商业化,我就无法使用该代码,所以我不得不考虑不同的库......

pli*_*nth 2

您所做的是设置页面的 CropBox,这对文档的内容完全没有任何作用。这是设计使然,自 Acrobat 1.0 以来一直如此。

您想要做的称为编辑(或者在您的情况下,称为独占编辑,因为您想要删除矩形边界之外的所有内容)。正确地做到这一点绝对是不简单的,主要是因为内容的问题部分重叠了要编辑的边界(图像、文本和路径)。

  • @firstTimeCaller 我编辑了我的问题,你可以在那里看到工作方法。 (2认同)