简体   繁体   中英

Extract ID of a PDF document using iTextSharp

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM