使用iTextSharp的PDF文档中提取ID [英] Extract ID of a PDF document using iTextSharp

查看:133
本文介绍了使用iTextSharp的PDF文档中提取ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取的文件的尾部一节中提到的PDF标识符。但我没能获得该值。
例如。以下是我在PDF文件中提到的:

 预告片
<< /尺寸196 / preV 370761/160根0 R /信息158 0 R / ID [< 30EB7FCBB6756E461176FBBD0CEBA7B9>< D​​B67D6D43AE0FA4FBF8CC171FC66790A>>>

我需要提取值 30EB7FCBB6756E461176FBBD0CEBA7B9 。使用 PdfReader.Trailer 我得到一个字典类型的对象,如果一个键'ID',但我不能够从它那里得到上述所需的值。


解决方案

  

使用 PdfReader.Trailer 我得到一个字典类型的对象,如果一个键'ID',但我不能够从它那里得到上述所需的值。


看着 PdfReader.Trailer 你几乎有:

 公共PdfArray GETID(字符串文件名)
{
    使用(PdfReader pdfReader =新PdfReader(文件名))
    {
        返回pdfReader.Trailer.GetAsArray(PdfName.ID);
    }
}

该方法返回文档的ID,两字节的字符串数组。

您似乎是有意在ID的十六进制再presentation。你可以把它输出这样的:

 公共无效PrintId(PdfArray标识)
{
    如果(同上!= NULL)
    {
        StringBuilder的建设者=新的StringBuilder();
        builder.Append(ID);
        的foreach(在标识PdfObject O)
        {
            builder.Append(&下;);
            的foreach(BYTE B在((PdfString)O).GetBytes())
                builder.AppendFormat({0:X},B);
            builder.Append(>中);
        }
        Console.WriteLine(builder.ToString());
    }
}

(我不是在.net很精通,所以可能有很多更优雅的方式来创建一个字节数组的十六进制转储。)

I need to extract PDF identifier mentioned in the trailer section of the document. But I am not able to get that value. Eg. Following is mentioned in my pdf file:

trailer
<</Size 196/Prev 370761/Root 160 0 R/Info 158 0 R/ID[<30EB7FCBB6756E461176FBBD0CEBA7B9><DB67D6D43AE0FA4FBF8CC171FC66790A>]>>

I need to extract value 30EB7FCBB6756E461176FBBD0CEBA7B9. Using the PdfReader.Trailer I get a dictionary type of object if one key as 'ID' but I am not able to get the above required value from it.

解决方案

Using the PdfReader.Trailer I get a dictionary type of object if one key as 'ID' but I am not able to get the above required value from it.

Looking at PdfReader.Trailer you are almost there:

public PdfArray GetId(string FileName)
{
    using (PdfReader pdfReader = new PdfReader(FileName))
    {
        return pdfReader.Trailer.GetAsArray(PdfName.ID);
    }
}

This methods returns the ID of the document, an array of two byte strings.

You seem to be interested in the hexadecimal representation of the ID. You can output it like this:

public void PrintId(PdfArray Id)
{
    if (Id != null)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("ID: ");
        foreach (PdfObject o in Id)
        {
            builder.Append("<");
            foreach (byte b in ((PdfString)o).GetBytes())
                builder.AppendFormat("{0:X}", b);
            builder.Append(">");
        }
        Console.WriteLine(builder.ToString());
    }
}

(I'm not very proficient in .Net, so there may be many more elegant ways to create a hex dump of a byte array.)

这篇关于使用iTextSharp的PDF文档中提取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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