【问题标题】:Remove outer print marks on PDF iTextSharp删除 PDF iTextSharp 上的外部打印标记
【发布时间】:2013-07-26 14:43:53
【问题描述】:

我有一个 pdf 文件,其封面如下所示:

现在,我需要去除封面边缘周围的所谓“厨房痕迹”。我正在将 iTextSharp 与 C# 一起使用,我需要使用 iTextSharp 的代码来创建一个只有预期封面的新文档,或者使用 PdfStamper 来删除它。或任何其他使用 iTextSharp 的解决方案,可以提供结果。

到目前为止,我在搜索中找不到任何好的代码示例。

【问题讨论】:

标签: c# .net pdf itextsharp itext


【解决方案1】:

您必须实际删除它们还是可以将它们裁剪掉?如果您可以将它们裁剪掉,那么下面的代码将起作用。如果您必须从文件中实际删除它们,那么据我所知,没有一种简单的方法可以做到这一点。据我所知,这些对象并未明确标记为元对象。我能想到的删除它们的唯一方法是检查所有内容,看看它是否适合文档的活动区域。

下面的示例代码读取输入文件中的每一页并查找可能存在的各种框,trimartbleed。 (见this page。)

只要找到至少一个,它就会将页面的裁剪框设置为列表中的第一项。在您的情况下,您实际上可能必须执行一些逻辑来找到所有这些项目中的“最小”,或者您可能只知道“艺术”将始终为您工作。有关其他 cmets,请参见代码。这针对 iTextSharp 5.4.0.0。

//Sample input file
var inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Binder1.pdf");

//Sample output file
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Cropped.pdf");

//Bind a reader to our input file
using (var r = new PdfReader(inputFile)) {

    //Get the number of pages
    var pageCount = r.NumberOfPages;

    //See this for a list: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#getBoxSize(int, java.lang.String)
    var boxNames = new string[] { "trim", "art", "bleed" };

    //We'll create a list of all possible boxes to pick from later
    List<iTextSharp.text.Rectangle> boxes;

    //Loop through each page
    for (var i = 1; i <= pageCount; i++) {

        //Initialize our list for this page
        boxes = new List<iTextSharp.text.Rectangle>();

        //Loop through the list of known boxes
        for (var j = 0; j < boxNames.Length; j++) {

            //If the box exists
            if(r.GetBoxSize(i, boxNames[j]) != null){

                //Add it to our collection
                boxes.Add(r.GetBoxSize(i, boxNames[j]));
            }
        }

        //If we found at least one box
        if (boxes.Count > 0) {

            //Get the page's entire dictionary
            var dict = r.GetPageN(i);

            //At this point we might want to apply some logic to find the "inner most" box if our trim/bleed/art aren't all the same
            //I'm just hard-coding the first item in the list for demonstration purposes
            //Set the page's crop box to the specified box
            dict.Put(PdfName.CROPBOX, new PdfRectangle(boxes[0]));
        }
    }

    //Create our output file
    using (var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
        //Bind a stamper to our reader and output file
        using(var stamper = new PdfStamper(r,fs)){
            //We did all of our PDF manipulation above so we don't actually have to do anything here
        }
    }
}

【讨论】:

  • 我如何将其居中,当我运行该代码时,它将标记的内容放在右上角。显示全部内容,但我想我的问题是如何使用此代码设置偏移量。
  • 你能提供一个样本PDF吗?
  • 很遗憾,我们公司的政策禁止这样做。您是否知道定位 PdfStamper 以便我可以针对偏移问题进行调整的方法?
  • 您能否模拟没有专有信息的 PDF 样本?上面的代码只是告诉 PDF 实际上不显示裁剪框之外的内容。真的没有什么可以“居中”的。你能检查一下你的各种盒子,看看你能不能选择一个“更好”的盒子来裁剪?
猜你喜欢
  • 2011-08-31
  • 2016-06-02
  • 2012-02-04
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
相关资源
最近更新 更多