Skip to content

Commit 4cfbaf0

Browse files
authored
Merge pull request #15 from synercoder/fix-pdfwriter-always-disposes-stream
Fix pdfwriter always disposes stream
2 parents 5ef2180 + 7ec0ac4 commit 4cfbaf0

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/Synercoding.FileFormats.Pdf/PdfWriter.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Synercoding.FileFormats.Pdf
1212
/// </summary>
1313
public class PdfWriter : IDisposable
1414
{
15+
private readonly bool _ownsStream;
1516
private readonly Stream _stream;
1617
private readonly TableBuilder _tableBuilder = new TableBuilder();
1718

@@ -25,12 +26,23 @@ public class PdfWriter : IDisposable
2526
/// </summary>
2627
/// <param name="stream">The <see cref="Stream"/> to write the PDF file</param>
2728
public PdfWriter(Stream stream)
29+
: this(stream, true)
30+
{ }
31+
32+
/// <summary>
33+
/// Constructor for <see cref="PdfWriter"/>
34+
/// </summary>
35+
/// <param name="stream">The <see cref="Stream"/> to write the PDF file</param>
36+
/// <param name="ownsStream">If the stream is owned, then when this <see cref="PdfWriter"/> is disposed, the stream is also disposed.</param>
37+
public PdfWriter(Stream stream, bool ownsStream)
2838
{
2939
_stream = stream;
3040
(new Header()).WriteToStream(stream);
3141

3242
_pageTreeNode = _tableBuilder.ReserveId();
3343
_catalog = _tableBuilder.ReserveId();
44+
45+
_ownsStream = ownsStream;
3446
}
3547

3648
/// <summary>
@@ -66,12 +78,15 @@ public void Dispose()
6678

6779
_stream.Flush();
6880

69-
_stream.Dispose();
81+
if (_ownsStream)
82+
{
83+
_stream.Dispose();
84+
}
7085
}
7186

7287
private void _writePageTree()
7388
{
74-
_tableBuilder.SetPosition(_pageTreeNode, (uint)_stream.Position);
89+
_tableBuilder.SetPosition(_pageTreeNode, (uint)_stream.Position);
7590

7691
var tree = new PageTree(_pageTreeNode, _pageReferences);
7792
tree.WriteToStream(_stream);
@@ -87,13 +102,13 @@ private void _writeCatalog()
87102

88103
private void _writePdfEnding()
89104
{
90-
if(!_tableBuilder.Validate())
105+
if (!_tableBuilder.Validate())
91106
{
92107
throw new InvalidOperationException("XRef table is invalid.");
93108
}
94109

95110
var xRefTable = _tableBuilder.GetXRefTable();
96-
var xRefPosition = xRefTable.WriteToStream(_stream);
111+
uint xRefPosition = xRefTable.WriteToStream(_stream);
97112

98113
var trailer = new Trailer(xRefPosition, xRefTable.Section.ObjectCount, _catalog.ObjectId);
99114
trailer.WriteToStream(_stream);

src/Synercoding.FileFormats.Pdf/Synercoding.FileFormats.Pdf.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
<TargetFramework>netstandard1.6</TargetFramework>
55
<LangVersion>7.3</LangVersion>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
7-
<Version>1.0.0-alpha001</Version>
7+
<Version>1.0.0-alpha002</Version>
88
<Authors>Synercoding</Authors>
99
<Description>Contains classes which makes it easy to quickly create a pdf file</Description>
1010
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
11+
<PackageReleaseNotes>Added the ability to indicate that the PdfWriter does not own the stream.</PackageReleaseNotes>
12+
<PackageProjectUrl>https://github.com/synercoder/FileFormats.Pdf</PackageProjectUrl>
13+
<PackageLicenseUrl>https://github.com/synercoder/FileFormats.Pdf/blob/master/LICENSE</PackageLicenseUrl>
14+
<RepositoryUrl>https://github.com/synercoder/FileFormats.Pdf</RepositoryUrl>
1115
</PropertyGroup>
1216

1317
<ItemGroup>

0 commit comments

Comments
 (0)