iTextSharp - 裁剪 PDF 文件 (C#)
iTextSharp - Crop PDF File (C#)
我想使用 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();
}
编辑:
这是有效的代码,我花了几个小时但终于做到了:P
首先,将以下内容添加到项目中:
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup;
那你可以用我的代码:
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();
}
不幸的是,如果我想在不支付许可的情况下将我的应用程序商业化,我将无法使用该代码,所以我不得不考虑使用不同的库...
您所做的是设置页面的 CropBox,这对文档的内容绝对没有任何影响。这是设计使然,自 Acrobat 1.0 以来一直如此。
你想要做的是所谓的编辑(或者在你的情况下,独占编辑,因为你想删除矩形边界之外的所有内容)。正确地做到这一点绝对不是一件容易的事,主要是因为内容的问题与要编辑的边界(图像、文本和路径)部分重叠。
我想使用 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();
}
编辑:
这是有效的代码,我花了几个小时但终于做到了:P
首先,将以下内容添加到项目中:
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup;
那你可以用我的代码:
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();
}
不幸的是,如果我想在不支付许可的情况下将我的应用程序商业化,我将无法使用该代码,所以我不得不考虑使用不同的库...
您所做的是设置页面的 CropBox,这对文档的内容绝对没有任何影响。这是设计使然,自 Acrobat 1.0 以来一直如此。
你想要做的是所谓的编辑(或者在你的情况下,独占编辑,因为你想删除矩形边界之外的所有内容)。正确地做到这一点绝对不是一件容易的事,主要是因为内容的问题与要编辑的边界(图像、文本和路径)部分重叠。