简体   繁体   中英

Get the Creation Date of a PDF file using iText 7

I have to create a tool which adds to several .pdf file names their creation date. I'd like to use the creationdate stored internally in pdfs and for this I downloaded iText Community Edition.

Now, my code starts like this (VB)

Module Module1

    Sub Main()

        Dim filename As String = My.Application.CommandLineArgs(0)

        Dim PDFReader = New Pdf.PdfReader(filename)
        Dim PDFDocument = New Pdf.PdfDocument(PDFReader)

        Dim documentinfo As Pdf.PdfDocumentInfo = PDFDocument.GetDocumentInfo

        Dim author As String = documentinfo.GetAuthor
        Dim creator As String = documentinfo.GetCreator
        Dim mypdfobject = documentinfo.GetPdfObject

    End Sub

End Module

I got the GetAuthor and GetCreator together with several other Get method, but I can't find something like GetCreationDate, only AddCreationDate.

If I go further into mypdfobject I find into map a /Creationdate tag, so I thought to use that, but, while it is often in the format D:20160704132234+02'00', sometimes I find something which seems binary data and I don't know how to decode that.

Is there any better way to get the creation date ?

Thanks

Stefano

The creation date is a PDF string value. There are two ways to represent a string. You already know this way: (D:20160704132234+02'00') , but there's also a hexadecimal notation, for instance: <443A32303136303730343133323233342b303227303027> .

When you have a PdfString , you can get the value in different ways: there's toString() , but there's also toUnicodeString() . When you have a String version, you can get a Calendar object from the PdfDate class:

Calendar date = PdfDate.decode(s);

If you want the date in W3C format, you can do something like this:

string w3cDate = PdfDate.getW3CDate(s);

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