使用iText 7获取PDF文件的创建日期 [英] Get the Creation Date of a PDF file using iText 7
问题描述
我必须创建一个工具,该工具会将其创建日期添加到多个.pdf文件名中. 我想使用内部存储在pdfs中的creationdate,为此,我下载了iText Community Edition.
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.
现在,我的代码以(VB)开头
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
我获得了GetAuthor和GetCreator以及其他几个Get方法,但是找不到诸如GetCreationDate之类的东西,只有AddCreationDate.
I got the GetAuthor and GetCreator together with several other Get method, but I can't find something like GetCreationDate, only AddCreationDate.
如果我进一步进入mypdfobject,则会在映射中找到一个/Creationdate标记,因此我想使用该标记,但是尽管它通常采用D:20160704132234 + 02'00'格式,但有时我会发现一些看起来像二进制的东西数据,我不知道如何解码.
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 ?
谢谢
Stefano
推荐答案
创建日期是PDF字符串值.有两种表示字符串的方法.您已经知道这种方式:(D:20160704132234+02'00')
,但是还有一个十六进制表示法,例如:<443A32303136303730343133323233342b303227303027>
.
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>
.
拥有PdfString
时,可以通过不同的方式获取值:有toString()
,但也有toUnicodeString()
.如果您使用的是String
版本,则可以从PdfDate
类中获取一个Calendar
对象:
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);
如果您希望使用W3C格式的日期,则可以执行以下操作:
If you want the date in W3C format, you can do something like this:
string w3cDate = PdfDate.getW3CDate(s);
这篇关于使用iText 7获取PDF文件的创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!