简体   繁体   中英

iText7 Reading CreateDate

I am replacing the use of AsposePdf to iText7 . And I need to read CreateDate property of a pdf document with itext7 library as well.

For the time being, I implemented it this way:

PdfDocument doc = new PdfDocument(pdfReader);
string xmp = Encoding.UTF8.GetString(doc.GetXmpMetadata());
XElement xmpXml = XElement.Parse(xmp);

XElement dateElement = (from e in xmpXml.Descendants()
                                    where 0 == string.Compare(e.Name.LocalName, "CreateDate", true)
                                    select e).SingleOrDefault();

DateTime? createDateTime = null;
if (dateElement != null)
{
    createDateTime = DateTime.Parse(dateElement.Value);
}

But I am not sure this is a proper way of reading such properties like CreateDate. Theoretically: In different versions of a pdf document, CreateDate might be written in different places. Or XML structure of an XMP part might be changed.

Therefore I would expect iText7 handled the logic of extracting CreateDate property.

So my question is:

Is there any better way to do what I have implemented in the code section with iText7?

Thank you!

There are two places where CreateDate / CreationDate can reside: document info dictionary and document XMP Metadata .

Theoretically those can be out of sync and you will have to choose which one to give preference to, but normally all the major PDF producers keep those dates in sync.

For extracting both values iText offers standard functions:

// Info dictionary
String date = pdfDocument.getDocumentInfo().getMoreInfo("CreationDate");

// XMP metadata
if (pdfDocument.getXmpMetadata() != null) {
    XMPMeta meta = XMPMetaFactory.parseFromBuffer(pdfDocument.getXmpMetadata());
    XMPDateTime dateTime = meta.getPropertyDate(XMPConst.NS_XMP, PdfConst.CreateDate);
}

Please mind that the code is in Java but should be very easy to adapt to .NET

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