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

问题描述 投票:1回答:2

我需要处理从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";
            }

马丁

c# pdf itext
2个回答
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));

希望这可以帮助。


0
投票

我真的需要一个解决方案,BBL管理员的评论写你自己的功能结果是我的出路。

从这个this itex support link我能够得到pdfDate格式的迭代为D:YYYYMMDDHHmmSSOHH'mm'

接下来我需要知道的是c#中的支持日期格式,我可以使用来自DateTime.Parse()this c-sharpcorner artical进行解析,对我来说最理想的是“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);  
  }

嗯,我希望你在问的时候能找到一种方法,任何面对同样挑战的人都可以尝试我的方法,看看它是否有帮助。不过,问题回答了。

注意:可能有其他/更好的方法来做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.