c#将PDF元数据CreationTime转换为DateTime

时间:2015-12-03 17:17:06

标签: c# pdf itext

我需要处理从PDF的元数据中检索的CreationTime,并将其与DataTime格式进行比较。

string path = e.Row.Cells[1].Text;
var pdfReader = new PdfReader(path);
var CreatedDate = pdfReader.Info["CreationDate"];
e.Row.Cells[13].Text = Convert.ToString(CreatedDate);

返回Date-Time-String,如:

  • d:20150710080410
  • d:20150209075651 + 01' 00'

并进行比较:

            DateTime Created = Convert.ToDateTime(CreatedDate);
            DateTime Compare = Convert.ToDateTime(e.Row.Cells[14].Text);
            if (Compare > Created)
            {
                e.Row.Cells[15].Text = "actualizar";
            }

马丁

2 个答案:

答案 0 :(得分:0)

如果您尝试转换的日期 - 时间字符串将以" D:"开头。每次,你可能会考虑为D:添加一个删除功能。当你尝试转换时,这可能会给你一个例外。试试这个:

// Gather the Info
string path = e.Row.Cells[1].Text;
var pdfReader = new PdfReader(path);
var CreatedDate = pdfReader.Info["CreationDate"];
e.Row.Cells[13].Text = Convert.ToString(CreatedDate);
string sCreatedDate = Convert.ToString(CreatedDate).Remove(0, 2)

// Convert and Compare
DateTime Created = Convert.ToDateTime(sCreatedDate);
DateTime Compare = Convert.ToDateTime(e.Row.Cells[14].Text);
if (Compare > Created)
{
    e.Row.Cells[15].Text = "actualizar";
}

您不必创建sCreatedDate,但以这种方式查看它会更加清晰。您还可以转换CreatedDate.ToString()。执行datetime转换时删除(0,2):

DateTime Created = Convert.ToDateTime(CreatedDate.ToString().Remove(0,2));

希望这有帮助。

答案 1 :(得分:0)

我真的需要一个解决方案,BBL Admin关于编写自己的函数的评论原来是我的出路。

通过此this itex support link,我能够以 D:YYYYMMDDHHmmSSOHH'mm'

的形式获取pdfDate格式。

接下来我需要知道的是,我可以使用this c-sharpcorner artical中的DateTime.Parse()解析的 c#中的支持日期格式,对我来说最理想的是” yyyy'-'MM'-'dd'T'HH':'mm':ss“

在知道我得到的输入和我可以解析的格式之后,我创建了下面的函数来构造日期,基本上是从pdfDate中获取零件,并为“可分析的”日期字符串建造零件...

private string CreateDateTime(string date) //use the pdfDate as parameter to the date argument
  {
            string dateStr = date.Remove(0, 2).Remove(14, 6); //Remove D: & OHH'mm
            string tmpDateStr = dateStr.Substring(0, 4) //Get year i.e yyyy
                + "-" + dateStr.Substring(4, 2) // Get month i.e mm & prepend - (hyphen)
                + "-" + dateStr.Substring(6, 2) // Get day i.e dd & prepend -
                + "T" + dateStr.Substring(8, 2) // Get hour and prepend T
                + ":" + dateStr.Substring(10, 2) // Get minutes and prepend :
                + ":" + dateStr.Substring(12, 2); //Get seconds and prepend :

            return DateTime.Parse(tmpDateStr);  
  }

好吧,我希望您在提问时能找到一种方法,其他面临同样挑战的人都可以尝试我的方法,看看是否有帮助。不过,问题得到了解答。

注意::可能还有其他/更好的方法。

相关问题