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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ insert_final_newline = true
[*.cs]
dotnet_diagnostic.IDE0008.severity = none # use explicit type instead of var
dotnet_diagnostic.IDE0011.severity = none # add braces to 'if' statement
dotnet_diagnostic.IDE0005.severity = suggestion # using directive is unnecessary
dotnet_diagnostic.IDE0005.severity = error # using directive is unnecessary
dotnet_diagnostic.IDE0049.severity = suggestion # name can be simplified
dotnet_diagnostic.IDE0055.severity = suggestion # fix formatting
dotnet_diagnostic.IDE0130.severity = error # namespace match directory structure
Expand Down
11 changes: 11 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project>
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- Required for IDE0005 (remove unnecessary usings) to fire during build -->
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- CA1819: public byte[] property is intentional public API surface -->
<!-- CA1720: 'Object' property name is public API, renaming would be breaking -->
<!-- CS1591: XML doc comments not required; GenerateDocumentationFile only needed for IDE0005 -->
<NoWarn>CA1819;CA1720;CS1591</NoWarn>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Unless otherwise stated in the source file, the code is release under the MIT License

Copyright (c) 2011-2024 SafeRapidPdf contributors
Copyright (c) 2011-2026 SafeRapidPdf contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion PdfInfoTool/Command/Show.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ internal static int RunShowAndReturnExitCode(ShowOptions opts)
type = objectType.ToString();
}
}
catch
#pragma warning disable CA1031 // Intentional: swallow all parsing errors for this CLI diagnostic tool
catch (Exception)
{
type = $"Not found {o} {g}";
}
#pragma warning restore CA1031
}

Console.WriteLine($"{obj}: {entry} - {type}");
Expand Down
8 changes: 6 additions & 2 deletions PdfInfoTool/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ internal interface IOptions
}

[Verb("dump", HelpText = "Dump an object out.")]
internal class DumpOptions : IOptions
#pragma warning disable CA1812 // Instantiated by CommandLineParser via reflection
internal sealed class DumpOptions : IOptions
#pragma warning restore CA1812
{
public bool Verbose { get; set; }
public bool Quiet { get; set; }
public string FileName { get; set; }
}

[Verb("show", HelpText = "Show object contents in a human readable way.")]
internal class ShowOptions : IOptions
#pragma warning disable CA1812 // Instantiated by CommandLineParser via reflection
internal sealed class ShowOptions : IOptions
#pragma warning restore CA1812
{
public bool Verbose { get; set; }
public bool Quiet { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion SafeRapidPdf.UnitTests/File/PdfFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PdfFileTests
public void Parsing_TinyFile(string pdf)
{
var r = PdfFile.Parse(pdf.ToStream());
Assert.True(r.Items.Count == 3);
Assert.Equal(3, r.Items.Count);
}

[Theory]
Expand Down
5 changes: 2 additions & 3 deletions SafeRapidPdf.UnitTests/File/PdfXRefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public void Parsing_Uncompressed_XRef(string xref)
{
var r = PdfXRef.Parse(xref.ToLexer());
// 1 section
Assert.Equal(1, r.Items.Count);
var s = r.Items[0] as PdfXRefSection;
var s = Assert.Single(r.Items) as PdfXRefSection;
// 6 entries
Assert.Equal(6, s.Items.Count);
}
Expand Down Expand Up @@ -65,7 +64,7 @@ public void Parsing_CompressedXRef()
// W[1 3 1] (5 columns, larger indexes)

// needed to resolve the values for refs encoded with 2
var base64Object706 = @"NzA2IDAgb2JqDTw8L0ZpbHRlci9GbGF0ZURlY29kZS9GaXJzdCAzMC9MZW5ndGggMTkzL04gNC9U
_ = @"NzA2IDAgb2JqDTw8L0ZpbHRlci9GbGF0ZURlY29kZS9GaXJzdCAzMC9MZW5ndGggMTkzL04gNC9U
eXBlL09ialN0bT4+c3RyZWFtDQpo3kSOwQ6CMBBEf2W/wG0BARPSRFAJBwKxHkwIh1qrUcES6EH/
3gIaTzO78zKZgDpAIKAuUOpZ9YC6rtUlUD+EKMJEN7rnnZBqPAZ/YgnsrQ8n3nrGcPsyKTfCjFTK
6dQwJ2WvJVemwnKzw6wVVxXXeCxOdyWNhbN2hMkMM1ZhliSxGNQZAhKO37pCrjrRC3PTT4wbIR+/
Expand Down
5 changes: 5 additions & 0 deletions SafeRapidPdf.UnitTests/SafeRapidPdf.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.analyzers" Version="1.27.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions SafeRapidPdf.UnitTests/Util/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace SafeRapidPdf.UnitTests.Util;


public static class StringExtensions
internal static class StringExtensions
{
public static Stream ToStream(this string input)
{
Expand Down Expand Up @@ -36,7 +36,7 @@ public static Lexer Base64ToLexer(this string input)

public static string ToHexString(this byte[] ba)
{
if (ba == null) throw new ArgumentNullException(nameof(ba));
ArgumentNullException.ThrowIfNull(ba);

var hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
Expand Down
26 changes: 9 additions & 17 deletions SafeRapidPdf/Attributes/ParameterTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
namespace SafeRapidPdf.Attributes;

[AttributeUsage(AttributeTargets.Property)]
public sealed class ParameterTypeAttribute : Attribute
public sealed class ParameterTypeAttribute(
bool required,
bool inheritable = false,
string version = "",
bool obsolete = false) : Attribute
{
public ParameterTypeAttribute(
bool required,
bool inheritable = false,
string version = "",
bool obsolete = false)
{
Required = required;
Inheritable = inheritable;
Version = version;
Obsolete = obsolete;
}

/// <summary>
/// Required or Optional
/// </summary>
public bool Required { get; }
public bool Required { get; } = required;

/// <summary>
/// Inheritable attribute
/// </summary>
public bool Inheritable { get; }
public bool Inheritable { get; } = inheritable;

/// <summary>
/// PDF version from which this parameter is allowed
/// </summary>
public string Version { get; }
public string Version { get; } = version;

/// <summary>
/// Was this parameter obsoleted?
/// </summary>
public bool Obsolete { get; }
public bool Obsolete { get; } = obsolete;
}
6 changes: 1 addition & 5 deletions SafeRapidPdf/Document/PdfArtBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ namespace SafeRapidPdf.Document;
/// <summary>
/// Extent of the page’s meaningful content
/// </summary>
public sealed class PdfArtBox : PdfRectangle
public sealed class PdfArtBox(PdfArray box) : PdfRectangle(PdfObjectType.ArtBox, box)
{
public PdfArtBox(PdfArray box)
: base(PdfObjectType.ArtBox, box)
{
}
}
11 changes: 3 additions & 8 deletions SafeRapidPdf/Document/PdfBaseObject.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
namespace SafeRapidPdf.Document;

public abstract class PdfBaseObject : IPdfObject
public abstract class PdfBaseObject(PdfObjectType type) : IPdfObject
{
protected PdfBaseObject(PdfObjectType type)
{
ObjectType = type;
}

public PdfObjectType ObjectType { get; }
public PdfObjectType ObjectType { get; } = type;

public bool IsContainer { get; protected set; }

Expand All @@ -16,5 +11,5 @@ protected PdfBaseObject(PdfObjectType type)
public virtual IReadOnlyList<IPdfObject> Items
=> !IsContainer
? null
: throw new NotImplementedException();
: throw new InvalidOperationException("Subclass must override Items when IsContainer is true.");
}
6 changes: 1 addition & 5 deletions SafeRapidPdf/Document/PdfBleedBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ namespace SafeRapidPdf.Document;
/// region to which the contents of the page should be clipped
/// when output in a production environment
/// </summary>
public sealed class PdfBleedBox : PdfRectangle
public sealed class PdfBleedBox(PdfArray box) : PdfRectangle(PdfObjectType.BleedBox, box)
{
public PdfBleedBox(PdfArray box)
: base(PdfObjectType.BleedBox, box)
{
}
}
2 changes: 1 addition & 1 deletion SafeRapidPdf/Document/PdfCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SafeRapidPdf.Document;

public sealed class PdfCatalog : PdfBaseObject
{
private readonly List<IPdfObject> _items = new();
private readonly List<IPdfObject> _items = [];

public PdfCatalog(PdfDictionary catalog)
: base(PdfObjectType.Catalog)
Expand Down
6 changes: 1 addition & 5 deletions SafeRapidPdf/Document/PdfCropBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ namespace SafeRapidPdf.Document;
/// <summary>
/// visible region of default user space
/// </summary>
public sealed class PdfCropBox : PdfRectangle
public sealed class PdfCropBox(PdfArray box) : PdfRectangle(PdfObjectType.CropBox, box)
{
public PdfCropBox(PdfArray box)
: base(PdfObjectType.CropBox, box)
{
}
}
6 changes: 1 addition & 5 deletions SafeRapidPdf/Document/PdfMediaBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ namespace SafeRapidPdf.Document;
/// boundaries of the physical medium on which the page is
/// intended to be displayed or printed
/// </summary>
public sealed class PdfMediaBox : PdfRectangle
public sealed class PdfMediaBox(PdfArray box) : PdfRectangle(PdfObjectType.MediaBox, box)
{
public PdfMediaBox(PdfArray box)
: base(PdfObjectType.MediaBox, box)
{
}
}
5 changes: 3 additions & 2 deletions SafeRapidPdf/Document/PdfPage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;

using SafeRapidPdf.Attributes;
using SafeRapidPdf.Objects;
Expand All @@ -7,7 +8,7 @@ namespace SafeRapidPdf.Document;

public class PdfPage : PdfBaseObject
{
private readonly List<IPdfObject> _items = new();
private readonly List<IPdfObject> _items = [];

public PdfPage(PdfIndirectReference pages, PdfPageTree parent)
: this(pages, parent, PdfObjectType.Page)
Expand All @@ -20,7 +21,7 @@ public PdfPage(PdfIndirectReference pages, PdfPageTree parent)

page.ExpectsType("Page");

foreach (PdfKeyValuePair pair in page.Items)
foreach (PdfKeyValuePair pair in page.Items.Cast<PdfKeyValuePair>())
{
HandleKeyValuePair(pair);
}
Expand Down
7 changes: 4 additions & 3 deletions SafeRapidPdf/Document/PdfPageTree.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;

using SafeRapidPdf.Attributes;
using SafeRapidPdf.Objects;
Expand All @@ -21,16 +22,16 @@ public PdfPageTree(PdfIndirectReference pages, PdfPageTree parent)
var pageTree = pages.Dereference<PdfDictionary>();
pageTree.ExpectsType("Pages");

foreach (PdfKeyValuePair pair in pageTree.Items)
foreach (PdfKeyValuePair pair in pageTree.Items.Cast<PdfKeyValuePair>())
{
switch (pair.Key.Text)
{
case "Type": // skip Type Pages
break;
case "Kids":
var kids = (PdfArray)pair.Value;
Kids = new List<IPdfObject>();
foreach (PdfIndirectReference item in kids.Items)
Kids = [];
foreach (PdfIndirectReference item in kids.Items.Cast<PdfIndirectReference>())
{
var dic = item.Dereference<PdfDictionary>();
string type = dic["Type"].Text;
Expand Down
6 changes: 1 addition & 5 deletions SafeRapidPdf/Document/PdfTrimBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ namespace SafeRapidPdf.Document;
/// <summary>
/// intended dimensions of the finished page after trimming
/// </summary>
public sealed class PdfTrimBox : PdfRectangle
public sealed class PdfTrimBox(PdfArray box) : PdfRectangle(PdfObjectType.TrimBox, box)
{
public PdfTrimBox(PdfArray box)
: base(PdfObjectType.TrimBox, box)
{
}
}
3 changes: 1 addition & 2 deletions SafeRapidPdf/IPdfObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;


namespace SafeRapidPdf;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SafeRapidPdf/Objects/PdfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private PdfFile(IReadOnlyList<IPdfObject> objects)
Items = objects;

// build up the fast object lookup dictionary
_indirectObjects = new Dictionary<string, PdfIndirectObject>();
_indirectObjects = [];

foreach (var obj in Items.OfType<PdfIndirectObject>())
{
Expand Down
3 changes: 1 addition & 2 deletions SafeRapidPdf/Objects/PdfIndirectObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Globalization;
using SafeRapidPdf.Parsing;

Expand All @@ -22,7 +21,7 @@ private PdfIndirectObject(int objectNumber, int generationNumber, IPdfObject obj

public IPdfObject Object { get; }

public override IReadOnlyList<IPdfObject> Items => new[] { Object };
public override IReadOnlyList<IPdfObject> Items => [Object];

internal static PdfIndirectObject Parse(Lexer lexer)
{
Expand Down
5 changes: 2 additions & 3 deletions SafeRapidPdf/Objects/PdfKeyValuePair.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;


namespace SafeRapidPdf.Objects;

/// <summary>
Expand All @@ -20,7 +19,7 @@ public PdfKeyValuePair(PdfName key, PdfObject value)

public PdfObject Value { get; }

public override IReadOnlyList<IPdfObject> Items => new[] { Value };
public override IReadOnlyList<IPdfObject> Items => [Value];

public override string ToString()
{
Expand Down
13 changes: 8 additions & 5 deletions SafeRapidPdf/Objects/PdfName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

namespace SafeRapidPdf.Objects;

public sealed class PdfName : PdfObject
public sealed partial class PdfName : PdfObject
{
private readonly string _rawName;

private PdfName(string name)
: base(PdfObjectType.Name)
{
_rawName = name;
RawName = name;
}

private string RawName { get; }

[GeneratedRegex(@"#(\d\d)")]
private static partial Regex HexEncodedCharRegex();

public string Name
// process the # encoded chars
=> Regex.Replace(_rawName, @"#(\d\d)", x =>
=> HexEncodedCharRegex().Replace(RawName, x =>
{
byte val = Convert.ToByte(x.Groups[1].Value, 16);
return ((char)val).ToString();
Expand Down
Loading
Loading