Skip to content

Commit

Permalink
Allows passing in read options to QPdf constructor (MINOR)
Browse files Browse the repository at this point in the history
  • Loading branch information
svengeance committed Jan 1, 2024
1 parent 4e79c52 commit 21934c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/QPdfSharp/Options/QPdfReadOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright © Stephen (Sven) Vernyi and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

namespace QPdfSharp.Options;

public sealed class QPdfReadOptions
{
public bool AttemptRecovery { get; set; }
public bool IgnoreXrefStreams { get; set; }
}
21 changes: 19 additions & 2 deletions src/QPdfSharp/QPdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using QPdfSharp.Extensions;
using QPdfSharp.Interop;
using QPdfSharp.Options;

namespace QPdfSharp;

Expand All @@ -15,18 +16,22 @@ public unsafe partial class QPdf: IDisposable

~QPdf() => ReleaseUnmanagedResources();

public QPdf(string filePath, string password = "")
public QPdf(string filePath, string password = "", QPdfReadOptions? readOptions = null)
{
ApplyReadOptions(readOptions);

fixed (sbyte* filePathBytes = filePath.ToSByte())
fixed (sbyte* passwordBytes = password.ToSByte())
CheckError(QPdfInterop.qpdf_read(_qPdfData, filePathBytes, passwordBytes));
}

public QPdf(ReadOnlyMemory<byte> bytes, string name = "in-memory pdf", string password = "")
public QPdf(ReadOnlyMemory<byte> bytes, string name = "in-memory pdf", string password = "", QPdfReadOptions? readOptions = null)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("Must give a non-null non-empty name for an in-memory PDF.", nameof(name));

ApplyReadOptions(readOptions);

using var fileBytesHandle = bytes.Pin();

fixed (sbyte* fileNameBytes = name.ToSByte())
Expand Down Expand Up @@ -61,4 +66,16 @@ private void ReleaseUnmanagedResources()
fixed (QPdfData** pdf = &_qPdfData)
QPdfInterop.qpdf_cleanup(pdf);
}

private void ApplyReadOptions(QPdfReadOptions? options)
{
if (options is null)
return;

if (options.AttemptRecovery is { } attemptRecoveryValue)
QPdfInterop.qpdf_set_attempt_recovery(_qPdfData, attemptRecoveryValue.ToQPdfBool());

if (options.IgnoreXrefStreams is { } ignoreXrefStreamsValue)
QPdfInterop.qpdf_set_ignore_xref_streams(_qPdfData, ignoreXrefStreamsValue.ToQPdfBool());
}
}

0 comments on commit 21934c1

Please sign in to comment.