Skip to content

Commit 051c73f

Browse files
authored
Merge pull request #215 from delta-emil/expose-PdfReadAccuracy-through-XPdfForm
expose PdfReadAccuracy through XPdfForm
2 parents 9b1e9bd + 64af4f4 commit 051c73f

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

PdfSharpCore/Drawing/XImage.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
using PdfSharpCore.Pdf.IO;
3434
using PdfSharpCore.Pdf.Advanced;
3535
using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes;
36+
using PdfSharpCore.Pdf.IO.enums;
3637
using static MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource;
3738
using PdfSharpCore.Utils;
3839
using SixLabors.ImageSharp.PixelFormats;
@@ -118,9 +119,21 @@ protected XImage()
118119
/// </summary>
119120
/// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
120121
public static XImage FromFile(string path)
122+
{
123+
return FromFile(path, PdfReadAccuracy.Strict);
124+
}
125+
126+
/// <summary>
127+
/// Creates an image from the specified file.
128+
/// For non-pdf files, this requires that an instance of an implementation of <see cref="T:MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource"/> be set on the `ImageSource.ImageSourceImpl` property.
129+
/// For .NetCore apps, if this property is null at this point, then <see cref="T:PdfSharpCore.Utils.ImageSharpImageSource"/> with <see cref="T:SixLabors.ImageSharp.PixelFormats.Rgba32"/> Pixel Type is used
130+
/// </summary>
131+
/// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
132+
/// <param name="accuracy">Moderate allows for broken references when using a PDF file.</param>
133+
public static XImage FromFile(string path, PdfReadAccuracy accuracy)
121134
{
122135
if (PdfReader.TestPdfFile(path) > 0)
123-
return new XPdfForm(path);
136+
return new XPdfForm(path, accuracy);
124137
return new XImage(path);
125138
}
126139

PdfSharpCore/Drawing/XPdfForm.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
using PdfSharpCore.Internal;
4141
using PdfSharpCore.Pdf;
4242
using PdfSharpCore.Pdf.IO;
43+
using PdfSharpCore.Pdf.IO.enums;
4344

4445
namespace PdfSharpCore.Drawing
4546
{
@@ -59,7 +60,7 @@ public class XPdfForm : XForm
5960
/// document. Furthermore, because XPdfForm can occupy very much memory, it is recommended to
6061
/// dispose XPdfForm objects if not needed anymore.
6162
/// </summary>
62-
internal XPdfForm(string path)
63+
internal XPdfForm(string path, PdfReadAccuracy accuracy)
6364
{
6465
int pageNumber;
6566
path = ExtractPageNumber(path, out pageNumber);
@@ -74,6 +75,7 @@ internal XPdfForm(string path)
7475
throw new ArgumentException("The specified file has no valid PDF file header.", "path");
7576

7677
_path = path;
78+
_pathReadAccuracy = accuracy;
7779
if (pageNumber != 0)
7880
PageNumber = pageNumber;
7981
}
@@ -82,54 +84,79 @@ internal XPdfForm(string path)
8284
/// Initializes a new instance of the <see cref="XPdfForm"/> class from a stream.
8385
/// </summary>
8486
/// <param name="stream">The stream.</param>
85-
internal XPdfForm(Stream stream)
87+
/// <param name="accuracy">Moderate allows for broken references.</param>
88+
internal XPdfForm(Stream stream, PdfReadAccuracy accuracy)
8689
{
8790
// Create a dummy unique path
8891
_path = "*" + Guid.NewGuid().ToString("B");
8992

9093
if (PdfReader.TestPdfFile(stream) == 0)
9194
throw new ArgumentException("The specified stream has no valid PDF file header.", "stream");
9295

93-
_externalDocument = PdfReader.Open(stream);
96+
_externalDocument = PdfReader.Open(stream, accuracy);
9497
}
9598

9699
/// <summary>
97100
/// Initializes a new instance of the <see cref="XPdfForm"/> class from a stream and password.
98101
/// </summary>
99102
/// <param name="stream">The stream.</param>
100103
/// <param name="password">The password.</param>
101-
internal XPdfForm(Stream stream, string password) {
104+
/// <param name="accuracy">Moderate allows for broken references.</param>
105+
internal XPdfForm(Stream stream, string password, PdfReadAccuracy accuracy) {
102106
// Create a dummy unique path
103107
_path = "*" + Guid.NewGuid().ToString("B");
104108

105109
if (PdfReader.TestPdfFile(stream) == 0)
106110
throw new ArgumentException("The specified stream has no valid PDF file header.", "stream");
107111

108-
_externalDocument = PdfReader.Open(stream, password, PdfDocumentOpenMode.ReadOnly);
112+
_externalDocument = PdfReader.Open(stream, password, PdfDocumentOpenMode.ReadOnly, accuracy);
109113
}
110114

111115
/// <summary>
112116
/// Creates an XPdfForm from a file.
113117
/// </summary>
114118
public static new XPdfForm FromFile(string path)
119+
{
120+
return FromFile(path, PdfReadAccuracy.Strict);
121+
}
122+
123+
/// <summary>
124+
/// Creates an XPdfForm from a file.
125+
/// </summary>
126+
public static new XPdfForm FromFile(string path, PdfReadAccuracy accuracy)
115127
{
116128
// TODO: Same file should return same object (that's why the function is static).
117-
return new XPdfForm(path);
129+
return new XPdfForm(path, accuracy);
118130
}
119131

120132
/// <summary>
121133
/// Creates an XPdfForm from a stream.
122134
/// </summary>
123135
public static XPdfForm FromStream(Stream stream)
124136
{
125-
return new XPdfForm(stream);
137+
return FromStream(stream, PdfReadAccuracy.Strict);
138+
}
139+
140+
/// <summary>
141+
/// Creates an XPdfForm from a stream.
142+
/// </summary>
143+
public static XPdfForm FromStream(Stream stream, PdfReadAccuracy accuracy)
144+
{
145+
return new XPdfForm(stream, accuracy);
126146
}
127147

128148
/// <summary>
129149
/// Creates an XPdfForm from a stream and a password.
130150
/// </summary>
131151
public static XPdfForm FromStream(Stream stream, string password) {
132-
return new XPdfForm(stream, password);
152+
return FromStream(stream, password, PdfReadAccuracy.Strict);
153+
}
154+
155+
/// <summary>
156+
/// Creates an XPdfForm from a stream and a password.
157+
/// </summary>
158+
public static XPdfForm FromStream(Stream stream, string password, PdfReadAccuracy accuracy) {
159+
return new XPdfForm(stream, password, accuracy);
133160
}
134161

135162
/*
@@ -370,12 +397,14 @@ internal PdfDocument ExternalDocument
370397
throw new InvalidOperationException("This XPdfForm is a template and not an imported PDF page; therefore it has no external document.");
371398

372399
if (_externalDocument == null)
373-
_externalDocument = PdfDocument.Tls.GetDocument(_path);
400+
_externalDocument = PdfDocument.Tls.GetDocument(_path, _pathReadAccuracy);
374401
return _externalDocument;
375402
}
376403
}
377404
internal PdfDocument _externalDocument;
378405

406+
private PdfReadAccuracy _pathReadAccuracy;
407+
379408
/// <summary>
380409
/// Extracts the page number if the path has the form 'MyFile.pdf#123' and returns
381410
/// the actual path without the number sign and the following digits.

PdfSharpCore/Pdf.Internal/ThreadLocalStorage.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
using System.Diagnostics;
3333
using System.IO;
3434
using PdfSharpCore.Pdf.IO;
35+
using PdfSharpCore.Pdf.IO.enums;
3536

3637
namespace PdfSharpCore.Pdf.Internal
3738
{
@@ -55,7 +56,7 @@ public void RemoveDocument(string path)
5556
_importedDocuments.Remove(path);
5657
}
5758

58-
public PdfDocument GetDocument(string path)
59+
public PdfDocument GetDocument(string path, PdfReadAccuracy accuracy)
5960
{
6061
Debug.Assert(path.StartsWith("*") || Path.IsPathRooted(path), "Path must be full qualified.");
6162

@@ -69,7 +70,7 @@ public PdfDocument GetDocument(string path)
6970
}
7071
if (document == null)
7172
{
72-
document = PdfReader.Open(path, PdfDocumentOpenMode.Import);
73+
document = PdfReader.Open(path, PdfDocumentOpenMode.Import, accuracy);
7374
_importedDocuments.Add(path, document.Handle);
7475
}
7576
return document;

0 commit comments

Comments
 (0)