Extract ID of a PDF document using iTextSharp

元气小坏坏 提交于 2019-12-12 03:22:39

问题


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.


回答1:


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.)



来源:https://stackoverflow.com/questions/33253523/extract-id-of-a-pdf-document-using-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!