@@ -85,7 +85,7 @@ private static void GrayNoteColours(string path, CancellationToken ct)
8585 }
8686 }
8787 if ( changed )
88- document . Save ( path ) ;
88+ SaveDated ( document , path ) ;
8989 }
9090
9191 /// <summary>Reports a step's progress as the part <paramref name="start"/> … start + share of the whole.</summary>
@@ -241,18 +241,25 @@ private static IReadOnlyList<string> ImageToPdf(ConversionStep step, IProgress<d
241241 }
242242
243243 /// <summary>
244- /// Saves <paramref name="document"/> with its dates written as the PDF format writes them. PDFsharp writes them in
245- /// the thread's calendar: under a regional format with another calendar (Umm al-Qura, Thai Buddhist, Persian) the
246- /// file got that calendar's year as a Gregorian one, and the next save of it threw where that year is outside the
247- /// calendar (1448 under Saudi Arabia's).
244+ /// Saves <paramref name="document"/> with its dates in the Gregorian calendar, as the PDF format wants them.
245+ /// PDFsharp writes them in the thread's calendar: under a regional format with another calendar (Umm al-Qura, Thai
246+ /// Buddhist, Persian) the file got that calendar's year as a Gregorian one (1448 in Saudi Arabia in 2026), and a
247+ /// save threw where a date is outside that calendar: such a year from an earlier save, or the year 1 PDFsharp
248+ /// writes for a PDF without a creation date, under Saudi Arabia's and Iran's calendars.
248249 /// </summary>
249- internal static void SaveDated ( PdfDocument document , string path )
250+ internal static void SaveDated ( PdfDocument document , string path ) => InvariantDates ( ( ) => document . Save ( path ) ) ;
251+
252+ /// <inheritdoc cref="SaveDated(PdfDocument, string)"/>
253+ internal static void SaveDated ( PdfDocument document , Stream stream , bool closeStream ) =>
254+ InvariantDates ( ( ) => document . Save ( stream , closeStream ) ) ;
255+
256+ private static void InvariantDates ( Action save )
250257 {
251258 var culture = CultureInfo . CurrentCulture ;
252259 try
253260 {
254261 CultureInfo . CurrentCulture = CultureInfo . InvariantCulture ;
255- document . Save ( path ) ;
262+ save ( ) ;
256263 }
257264 finally
258265 {
@@ -283,13 +290,14 @@ private static IReadOnlyList<string> WithoutMetadata(ConversionStep step, IProgr
283290 var output = step . Output . Allocate ( "pdf" ) ;
284291 if ( output is null )
285292 return [ ] ;
286- document . Save ( output ) ;
293+ SaveDated ( document , output ) ;
287294 return [ output ] ;
288295 }
289296
290297 /// <summary>
291298 /// Drops the document information and every XMP packet (document, pages, pictures, forms) and the private data of
292- /// the apps that made the file. PDFsharp writes a new XMP packet with only its own name and the save date.
299+ /// the apps that made the file. PDFsharp writes a new XMP packet with only its own name and, as there is no
300+ /// creation date left, the year 1 for its dates.
293301 /// </summary>
294302 internal static void StripMetadata ( PdfDocument document )
295303 {
@@ -328,7 +336,7 @@ private static IReadOnlyList<string> SplitOrExtract(ConversionStep step, IProgre
328336 var path = step . Output . Allocate ( "pdf" , PageGroups . Suffix ( groups [ i ] ) ) ;
329337 if ( path is null )
330338 continue ;
331- part . Save ( path ) ;
339+ SaveDated ( part , path ) ;
332340 written . Add ( path ) ;
333341 progress ? . Report ( ( i + 1.0 ) / groups . Count ) ;
334342 }
@@ -356,7 +364,7 @@ private static IReadOnlyList<string> SplitOrExtract(ConversionStep step, IProgre
356364 var output = step . Output . Allocate ( "pdf" ) ;
357365 if ( output is null )
358366 return [ ] ;
359- extract . Save ( output ) ;
367+ SaveDated ( extract , output ) ;
360368 return [ output ] ;
361369 }
362370
@@ -403,7 +411,7 @@ private static async Task<IReadOnlyList<string>> CompressAsync(ConversionStep st
403411 foreach ( var index in groups [ i ] )
404412 Rotate ( part . AddPage ( source . Pages [ index ] ) , options . Rotation ) ;
405413 var path = Path . Combine ( scratch , $ "part-{ i + 1 } .pdf") ;
406- part . Save ( path ) ;
414+ SaveDated ( part , path ) ;
407415 parts . Add ( ( path , PageGroups . Suffix ( groups [ i ] ) ) ) ;
408416 }
409417 }
@@ -426,7 +434,7 @@ private static async Task<IReadOnlyList<string>> CompressAsync(ConversionStep st
426434 foreach ( var index in pages )
427435 Rotate ( extract . AddPage ( source . Pages [ index ] ) , options . Rotation ) ;
428436 var path = Path . Combine ( scratch , "pages.pdf" ) ;
429- extract . Save ( path ) ;
437+ SaveDated ( extract , path ) ;
430438 parts . Add ( ( path , null ) ) ;
431439 }
432440 }
@@ -461,7 +469,7 @@ private static async Task<bool> CompressFileAsync(string plain, string output, P
461469 {
462470 using var document = OpenForChanges ( plain , ct ) ;
463471 PdfCompressor . Compress ( document , level , preset . Image . KeepMetadata , ct ) ;
464- document . Save ( builtIn ) ;
472+ SaveDated ( document , builtIn ) ;
465473 candidates . Add ( builtIn ) ;
466474 }
467475 catch ( Exception ex ) when ( gs is not null && ex is not OperationCanceledException )
0 commit comments