1

I can get the creation date of a PDF using iText7 (see below), but I don't know how to set it. iText5 used to have a PdfStamper that was replaced by PDFWriter. But I don't see any methods that allow me to update the document info.

var documentinfo = PDFDocument.GetDocumentInfo();
var date = documentinfo.GetMoreInfo("CreationDate");

any ideas?

mark gamache
  • 389
  • 3
  • 12

1 Answers1

1

The PDF creation date is set when one creates the PDF using iText7. However, if desired, one can change the creation date. The code below shows how to create a PDF using the default creation date, as well as, how to create a PDF specifying a creation date.

Download/install NuGet package: iText7

Add the following using directives:

  • using iText.Kernel.Pdf;
  • using iText.Layout;
  • using iText.Layout.Element;
  • using System.Diagnostics;

To create a PDF file and set the creation date:

private void CreatePdf(string filename)
{
    //use current Date/Time for PDF 'CreationDate' 

    //create new instance
    using (PdfWriter writer = new PdfWriter(filename))
    {
        //create new PDF document
        PdfDocument pdfDoc = new PdfDocument(writer);

        //create reference
        PdfDocumentInfo pdfDocInfo = pdfDoc.GetDocumentInfo();

        DateTime creationDateTime = PdfDate.Decode(pdfDocInfo.GetMoreInfo("CreationDate"));
        Debug.WriteLine($"creationDateTime: {creationDateTime.ToString()}");

        //create new instance
        Document doc = new Document(pdfDoc);

        //add paragraph
        doc.Add(new Paragraph("Hello World"));

        //close
        doc.Close();
        pdfDoc.Close();
    }
}


private void CreatePdf(string filename, DateTime pdfCreationDateTime)
{
    //specify PDF 'CreationDate'

    //create new instance
    using (PdfWriter writer = new PdfWriter(filename))
    {
        //create new PDF document
        PdfDocument pdfDoc = new PdfDocument(writer);

        //create reference
        PdfDocumentInfo pdfDocInfo = pdfDoc.GetDocumentInfo();
       
        //set PDF creation date
        pdfDocInfo.SetMoreInfo("CreationDate", GetPdfDateAsString(pdfCreationDateTime));

        DateTime decodedCreationDateTime = PdfDate.Decode(pdfDocInfo.GetMoreInfo("CreationDate"));
        Debug.WriteLine($"decodedCreationDateTime: {decodedCreationDateTime.ToString()}");

        //create new instance
        Document doc = new Document(pdfDoc);

        //add paragraph
        doc.Add(new Paragraph("Hello World"));

        //close
        doc.Close();
        pdfDoc.Close();
    }
}


private string GetPdfDateAsString(DateTime dt)
{
    DateTimeOffset dateTimeOffset = dt;
    
    //PDF defines a standard date format
    //D:YYYYMMDDHHmmSSOHH'mm'
    //where 'YYYY' = 4-digit year
    //      'MM' = 2-digit month
    //      'DD' = 2-digit day
    //      'HH' = 2-digit hour (24 hour)
    //      'mm' = 2-digit minute
    //      'SS' = 2-digit seconds
    //      'O' = offset [+|-]
    //      'HH' = offset hours
    //      'mm' = offset minutes
    //
    //Note: there is a single quote after both offset hours and offset minutes
    string offset = offset = $"{dateTimeOffset.Offset.Hours.ToString("D2")}'{dateTimeOffset.Offset.Minutes.ToString("D2")}'";

    return $"D:{dt.ToString("yyyy")}{dt.ToString("MM")}{dt.ToString("dd")}{dt.ToString("HH")}{dt.ToString("mm")}{dt.ToString("ss")}{offset}";
}

Usage:

private void btnCreatePDF_Click(object sender, EventArgs e)
{
    using (SaveFileDialog sfd = new SaveFileDialog())
    {
        sfd.Filter = "PDF File (*.pdf)|*.pdf";

        sfd.FileName = "Test.pdf";

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            //ToDo: change to desired DateTime value
            //CreatePdf(sfd.FileName);
            CreatePdf(sfd.FileName, DateTime.Now.AddDays(2));

            Debug.WriteLine($"Created '{sfd.FileName}'");
        }
    }
}

To get the creation date/time of a PDF:

private DateTime GetPdfCreationDate(string filename)
{
    //create new instance
    using (PdfReader reader = new PdfReader(filename))
    {
        //create new PDF document
        PdfDocument pdfDoc = new PdfDocument(reader);

        //create reference
        PdfDocumentInfo pdfDocInfo = pdfDoc.GetDocumentInfo();

        //get creation date
        DateTime creationDateTime = PdfDate.Decode(pdfDocInfo.GetMoreInfo("CreationDate"));

        pdfDoc.Close();

        return creationDateTime;
    }
}

Usage:

private void btnOpenPdf_Click(object sender, EventArgs e)
{
    using (OpenFileDialog sfd = new OpenFileDialog())
    {
        sfd.Filter = "PDF File (*.pdf)|*.pdf";

        sfd.FileName = "Test.pdf";

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            DateTime creationDateTime = GetPdfCreationDate(sfd.FileName);

            Debug.WriteLine($"'{sfd.FileName}' has a creation date of '{creationDateTime.ToString()}'");
        }
    }
}

Resources:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24