如何获取PDF页面的出血盒大小?

时间:2017-01-05 13:21:07

标签: itext

我正试图从PDF中获取所有可能的页面边界,但是当我要求“出血”时,我得到一个Null值:

private void button2_Click(object sender, EventArgs e)
    {
        string source = @"C:\\Users\\numx\\Desktop\\TestPDF.pdf";
        PdfReader reader = new PdfReader(source);

        iTextSharp.text.Rectangle cropBox = reader.GetCropBox(1);
        iTextSharp.text.Rectangle mediaBox = reader.GetBoxSize(1, "media");
        iTextSharp.text.Rectangle trimBox = reader.GetBoxSize(1, "trim");          
        iTextSharp.text.Rectangle artBox = reader.GetBoxSize(1, "art");

        iTextSharp.text.Rectangle bleedBox = reader.GetBoxSize(1, "bleed");

        MessageBox.Show(bleedBox.ToString());
        reader.Close();

    }

1 个答案:

答案 0 :(得分:0)

回顾我们在评论中讨论的内容。

执行此操作时:

_UNKNOWN

当PDF定义出血框时,iTextSharp.text.Rectangle bleedBox = reader.GetBoxSize(1, "bleed"); bleedBox类的实例;如果未定义出血框,则RectanglebleedBox。在后一种情况下,当您调用Null方法时,您将收到异常。

因此,如果您需要ToString(),请先检查bleedBox是否为bleedBox。如果不是,请使用Null对象。如果不是,请改用裁剪框。如果没有裁剪框,​​请使用媒体框。

在所有页面边界中,只有媒体框是必需的。所有其他类型的页面边界都是可选的。