diff --git a/src/PdfHelper/Converters/PdfMetadataConverter.cs b/src/PdfHelper/Converters/PdfMetadataConverter.cs index 5d62d9f..8b37e28 100644 --- a/src/PdfHelper/Converters/PdfMetadataConverter.cs +++ b/src/PdfHelper/Converters/PdfMetadataConverter.cs @@ -37,7 +37,9 @@ public object ConvertFrom(PdfObject value, CultureInfo culture) { } else if (value.GetObjectType() == PdfObject.STRING) { var s = (value as PdfString).GetValue(); if (PdfDateConverter.TryParse(s, out var d)) return d; - else return s; + if (double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out var n)) return n; + if (bool.TryParse(s, out var b)) return b; + return s; } else { return null; diff --git a/src/PdfHelper/PdfHelper.csproj b/src/PdfHelper/PdfHelper.csproj index 73de451..5896542 100644 --- a/src/PdfHelper/PdfHelper.csproj +++ b/src/PdfHelper/PdfHelper.csproj @@ -4,7 +4,7 @@ netstandard2.0 Sitl.PdfHelper Sitl.Pdf - 1.5.1 + 1.6.0 true true bin\Sitl.PdfHelper.xml @@ -22,13 +22,16 @@ - - - - + + + + + + + + + - - diff --git a/src/PdfHelper/PdfHelper.sln b/src/PdfHelper/PdfHelper.sln new file mode 100644 index 0000000..5df9146 --- /dev/null +++ b/src/PdfHelper/PdfHelper.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdfHelper", "PdfHelper.csproj", "{B20AB966-7E16-A7FA-DC98-DC11E8DE66C7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B20AB966-7E16-A7FA-DC98-DC11E8DE66C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B20AB966-7E16-A7FA-DC98-DC11E8DE66C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B20AB966-7E16-A7FA-DC98-DC11E8DE66C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B20AB966-7E16-A7FA-DC98-DC11E8DE66C7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B00A8E2C-A196-4B96-98CA-860469E2B92B} + EndGlobalSection +EndGlobal diff --git a/src/PdfHelper/PdfHelper_ConvertTo.cs b/src/PdfHelper/PdfHelper_ConvertTo.cs index f5f4a93..1cb70c0 100644 --- a/src/PdfHelper/PdfHelper_ConvertTo.cs +++ b/src/PdfHelper/PdfHelper_ConvertTo.cs @@ -24,7 +24,7 @@ public IEnumerator ConvertToBitmapsEnumerator(int startPage = 1, int var docNbPages = GetNumberOfPages(); if (nbPages == null) nbPages = docNbPages - (startPage - 1); if (startPage > docNbPages) throw new ArgumentOutOfRangeException(nameof(startPage)); - if (startPage - 1 + nbPages.Value > docNbPages) new ArgumentOutOfRangeException(nameof(nbPages)); + if (startPage - 1 + nbPages.Value > docNbPages) throw new ArgumentOutOfRangeException(nameof(nbPages)); for (int p = startPage; p < startPage + nbPages.Value; p++) { var pageSize = GetPageSize(p); diff --git a/src/PdfHelper/PdfHelper_Metadata.cs b/src/PdfHelper/PdfHelper_Metadata.cs index 90612a3..c6e79e6 100644 --- a/src/PdfHelper/PdfHelper_Metadata.cs +++ b/src/PdfHelper/PdfHelper_Metadata.cs @@ -1,6 +1,7 @@ -using System; +using System; using System.Collections.Generic; using System.IO; +using System.Reflection; using iText.Kernel.Pdf; namespace Sitl.Pdf { @@ -18,12 +19,13 @@ public Dictionary GetMetadata(bool useStringConverter = false, b var res = new Dictionary(); using (PdfReader reader = new PdfReader(PdfStream)) { using (PdfDocument pdfDoc = new PdfDocument(reader)) { - PdfDictionary infoDictionary = pdfDoc.GetTrailer().GetAsDictionary(PdfName.Info); + var infoDictionary = GetInfoDictionary(pdfDoc); + if (infoDictionary == null) return res; foreach (PdfName key in infoDictionary.KeySet()) { var val = converter.ConvertFrom(infoDictionary.Get(key)); if (ignoreNullValues && val == null) continue; if (ignoreNullValues && val is string && string.IsNullOrEmpty((string)val)) continue; - res.Add(key.GetValue(), converter.ConvertFrom(infoDictionary.Get(key))); + res.Add(key.GetValue(), val); } } } @@ -45,13 +47,14 @@ public void SetMetadata(Dictionary metadata, bool useStringConve using (PdfWriter writer = new PdfWriter(newPdfStream)) { writer.SetCloseStream(false); using (PdfDocument pdfDoc = new PdfDocument(reader, writer)) { - PdfDictionary infoDictionary = pdfDoc.GetTrailer().GetAsDictionary(PdfName.Info); + var infoDictionary = GetInfoDictionary(pdfDoc); foreach (var entry in metadata) { if (removeNullValues && entry.Value == null) infoDictionary.Remove(new PdfName(entry.Key)); else infoDictionary.Put(new PdfName(entry.Key), converter.ConvertTo(entry.Value)); } + infoDictionary.SetModified(); } } } @@ -64,5 +67,16 @@ public void SetMetadata(Dictionary metadata, bool useStringConve } } } + + /// + /// Gets the PDF info dictionary from the document. + /// In iText 9, the info dictionary is managed internally by PdfDocumentInfo + /// and is not accessible via the trailer until the document is closed. + /// + private static PdfDictionary GetInfoDictionary(PdfDocument pdfDoc) { + var docInfo = pdfDoc.GetDocumentInfo(); + var field = docInfo.GetType().GetField("infoDictionary", BindingFlags.NonPublic | BindingFlags.Instance); + return (PdfDictionary)field?.GetValue(docInfo); + } } } diff --git a/src/PdfHelper/PdfHelper_Scale.cs b/src/PdfHelper/PdfHelper_Scale.cs index 45b7ccf..b7a6c24 100644 --- a/src/PdfHelper/PdfHelper_Scale.cs +++ b/src/PdfHelper/PdfHelper_Scale.cs @@ -1,5 +1,5 @@ using System.IO; -using iText.Kernel.Events; +using iText.Kernel.Pdf.Event; using iText.Kernel.Pdf; using iText.Kernel.Pdf.Canvas; using iText.Kernel.Pdf.Xobject; @@ -42,7 +42,7 @@ public void Scale(float scale) { } } - class ScaleEventHandler : IEventHandler { + class ScaleEventHandler : AbstractPdfDocumentEventHandler { protected float scale = 1; protected PdfDictionary pageDict; @@ -54,7 +54,7 @@ public void SetPageDict(PdfDictionary pageDict) { this.pageDict = pageDict; } - public void HandleEvent(Event currentEvent) { + protected override void OnAcceptedEvent(AbstractPdfDocumentEvent currentEvent) { PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent; PdfPage page = docEvent.GetPage(); diff --git a/src/PdfHelper/PdfHelper_Thumbnail.cs b/src/PdfHelper/PdfHelper_Thumbnail.cs index 89c5a20..227e3f5 100644 --- a/src/PdfHelper/PdfHelper_Thumbnail.cs +++ b/src/PdfHelper/PdfHelper_Thumbnail.cs @@ -111,9 +111,9 @@ public byte[] GenerateThumbnail(int page, int? width, int? height, bool whiteBac var renderOptions = new PDFtoImage.RenderOptions( Dpi: dpi, - Width: thumbnailWidth, - Height: thumbnailHeight, - WithAnnotations: true, + Width: thumbnailWidth, + Height: thumbnailHeight, + WithAnnotations: true, BackgroundColor: whiteBackground ? SkiaSharp.SKColors.White : SkiaSharp.SKColors.Transparent ); using (var ms = new MemoryStream()) { diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index 20ca621..dbd8cf8 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -1,7 +1,7 @@  - net8.0-windows + net10.0-windows7.0 Sitl.PdfHelper.Tests Sitl.Pdf.Tests enable @@ -19,15 +19,22 @@ - - - - - - - - - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +