empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
527 stars 130 forks source link

correct datetime format in XMP Metadata for CreateDate/ModifyDate #166

Open flensrocker opened 2 months ago

flensrocker commented 2 months ago

I'm looking into getting PDFsharp generate PDF/A compliant documents (my goal is PDF/A-3, but since PDF/A-1A is already included I start there).

I ran PdfATests.Simple_PDF_A_document and uploaded the file here: https://www.pdfforge.org/online/en/validate-pdfa

Result:

ISO 19005-1:2005, 6.7.3 If a document information dictionary does appear at a document, then all of its entries that have analogous properties in predefined XMP schemas, shall also be embedded in the file in XMP form with equivalent values.

Running the document through one of the many online converter tools and looking at the differences, one outstanding difference is the format of the CreateDate and ModifyDate entries in the XMP metadata.

Wrong:

<xmp:CreateDate>2024-09-04T10:34:16.0000000</xmp:CreateDate>

Right:

<xmp:CreateDate>2024-09-04T10:34:16+02:00</xmp:CreateDate>

So instead of using the format string "o" the following should be used: "yyyy-MM-ddTHH:mm:ssK"

But this doesn't work, if the provided DateTime object doesn't specify a DateTimeKind. So setting DateTimeKind.Local if DateTimeKind.Unspecified resolves this problem (it doesn't really matter if it's Local or Utc, because none knows the original kind - but when creating the document from scratch with the test it matches local time).

I will provide a PR with this minimal changes, after that the validator doesn't complain anymore.