c# convert PDF metadata CreationTime to DateTime

I need to process the CreationTime I retrieve from the PDF's metadata and compare it to DataTime format.

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);

This returns a Date-Time-String like:

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

and to compare:

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

Martin

Answers


If your Date-Time string that you're trying to convert is going to start with "D:" every time, then you might think about adding in a remove function for D:. That's what's probably giving you the exception when you try to convert. Try this:

// 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";
}

You don't have to create sCreatedDate, but it's a little bit cleaner to view it that way. You could also convert CreatedDate.ToString().Remove(0,2) when you do the datetime convert:

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

Hope this helps.


I really needed a solution for this, BBL Admin 's Comment on writing your own function turned out to be my way out.

From this this itex support link I was able to get the intepratation of the pdfDate format as D:YYYYMMDDHHmmSSOHH'mm'

Next thing I needed to know is the supportade date formats in c# that I may Parse using DateTime.Parse() from this c-sharpcorner artical and the most ideal for me was "yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss"

Having known the input I get and the format I can parse, I created the function below to construct the date, basically getting parts from the pdfDate and building parts for the 'parsable' date string...

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);  
  }

Well, I hope you found a way at the time of asking, anyone else facing the same challange could try my approach and see if it helps. Nevertheless, question answered.

NB: There could be other/better ways to do it.


Need Your Help

Can't connect to memcache

php apache caching memcached

I am trying to connect to memcache as they suggest:

Add to an array while in a loop with a key

php arrays loops

I'm trying to populate all the links in my database into an array with a key something like this: