Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/PdfHelper/Converters/PdfMetadataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions src/PdfHelper/PdfHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Sitl.PdfHelper</AssemblyName>
<RootNamespace>Sitl.Pdf</RootNamespace>
<Version>1.5.1</Version>
<Version>1.6.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DocumentationFile>bin\Sitl.PdfHelper.xml</DocumentationFile>
Expand All @@ -22,13 +22,16 @@

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.12.4" />
<PackageReference Include="itext7" Version="7.1.19" />
<PackageReference Include="itext7.pdfhtml" Version="3.0.4" />
<PackageReference Include="MailKit" Version="4.14.1" />
<PackageReference Include="MsgReader" Version="6.0.5" />
<PackageReference Include="itext" Version="9.6.0" />
<PackageReference Include="itext.bouncy-castle-adapter" Version="9.6.0" />
<PackageReference Include="itext.pdfhtml" Version="6.3.2" />
<PackageReference Include="MailKit" Version="4.16.0" />
<PackageReference Include="MimeKit" Version="4.16.0" />
<PackageReference Include="MsgReader" Version="6.0.11" />
<PackageReference Include="OpenMcdf" Version="3.1.4" />
<PackageReference Include="ZXing.Net" Version="0.16.11" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.14" />
<PackageReference Include="PDFtoImage" Version="4.1.1" />
<PackageReference Include="ZXing.Net" Version="0.16.9" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" />

<!-- Newtonsoft.Json 9.0.3 referenced by itext7 is vulnerable, force to 13.0.4 -->
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
Expand Down
24 changes: 24 additions & 0 deletions src/PdfHelper/PdfHelper.sln
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/PdfHelper/PdfHelper_ConvertTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public IEnumerator<BitmapPage> 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);
Expand Down
22 changes: 18 additions & 4 deletions src/PdfHelper/PdfHelper_Metadata.cs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -18,12 +19,13 @@ public Dictionary<string, object> GetMetadata(bool useStringConverter = false, b
var res = new Dictionary<string, object>();
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);
}
}
}
Expand All @@ -45,13 +47,14 @@ public void SetMetadata(Dictionary<string, object> 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();
}
}
}
Expand All @@ -64,5 +67,16 @@ public void SetMetadata(Dictionary<string, object> metadata, bool useStringConve
}
}
}

/// <summary>
/// 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.
/// </summary>
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);
}
}
}
6 changes: 3 additions & 3 deletions src/PdfHelper/PdfHelper_Scale.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void Scale(float scale) {
}
}

class ScaleEventHandler : IEventHandler {
class ScaleEventHandler : AbstractPdfDocumentEventHandler {
protected float scale = 1;
protected PdfDictionary pageDict;

Expand All @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions src/PdfHelper/PdfHelper_Thumbnail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
27 changes: 17 additions & 10 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net10.0-windows7.0</TargetFramework>
<AssemblyName>Sitl.PdfHelper.Tests</AssemblyName>
<RootNamespace>Sitl.Pdf.Tests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -19,15 +19,22 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageReference Include="coverlet.collector" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="10.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
<PackageReference Include="MimeKit" Version="4.16.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading