Skip to content

Commit cb2899f

Browse files
committed
Made explicit call for writing the ending
1 parent 694a863 commit cb2899f

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/Synercoding.FileFormats.Pdf/PdfWriter.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public sealed class PdfWriter : IDisposable
2020
private readonly PageTree _pageTree;
2121
private readonly Catalog _catalog;
2222

23+
private bool _endingWritten = false;
24+
2325
/// <summary>
2426
/// Constructor for <see cref="PdfWriter"/>
2527
/// </summary>
@@ -68,6 +70,8 @@ public int PageCount
6870
/// <returns>Returns this <see cref="PdfWriter"/> to chain calls</returns>
6971
public PdfWriter SetDocumentInfo(Action<DocumentInformation> infoAction)
7072
{
73+
_throwWhenEndingWritten()';'
74+
7175
infoAction(DocumentInformation);
7276

7377
return this;
@@ -89,6 +93,8 @@ public PdfWriter AddPage(Action<PdfPage> pageAction)
8993
/// <returns>Returns this <see cref="PdfWriter"/> to chain calls</returns>
9094
public PdfWriter AddPage<T>(T data, Action<T, PdfPage> pageAction)
9195
{
96+
_throwWhenEndingWritten();
97+
9298
using (var page = new PdfPage(_tableBuilder, _pageTree))
9399
{
94100
pageAction(data, page);
@@ -115,6 +121,8 @@ public async Task<PdfWriter> AddPageAsync(Func<PdfPage, Task> pageAction)
115121
/// <returns>Returns an awaitable task that resolves into this <see cref="PdfWriter"/> to chain calls</returns>
116122
public async Task<PdfWriter> AddPageAsync<T>(T data, Func<T, PdfPage, Task> pageAction)
117123
{
124+
_throwWhenEndingWritten();
125+
118126
using (var page = new PdfPage(_tableBuilder, _pageTree))
119127
{
120128
await pageAction(data, page);
@@ -132,6 +140,8 @@ public async Task<PdfWriter> AddPageAsync<T>(T data, Func<T, PdfPage, Task> page
132140
/// <returns>The image reference that can be used in pages</returns>
133141
public Image AddImage(SixLabors.ImageSharp.Image image)
134142
{
143+
_throwWhenEndingWritten();
144+
135145
var id = _tableBuilder.ReserveId();
136146

137147
var pdfImage = new Image(id, image);
@@ -156,6 +166,8 @@ public Image AddImage(SixLabors.ImageSharp.Image image)
156166
/// <returns>The image reference that can be used in pages</returns>
157167
public Image AddJpgImageUnsafe(Stream jpgStream, int originalWidth, int originalHeight)
158168
{
169+
_throwWhenEndingWritten();
170+
159171
var id = _tableBuilder.ReserveId();
160172

161173
var pdfImage = new Image(id, jpgStream, originalWidth, originalHeight);
@@ -168,9 +180,17 @@ public Image AddJpgImageUnsafe(Stream jpgStream, int originalWidth, int original
168180
return pdfImage;
169181
}
170182

171-
/// <inheritdoc />
172-
public void Dispose()
183+
/// <summary>
184+
/// Write the PDF trailer; indicates that the PDF is done.
185+
/// </summary>
186+
/// <remarks>
187+
/// Other calls to this <see cref="PdfWriter"/> will throw or have no effect after call this.
188+
/// </remarks>
189+
public void WriteTrailer()
173190
{
191+
if (_endingWritten)
192+
return;
193+
174194
_writePageTree();
175195

176196
_writeCatalog();
@@ -181,12 +201,27 @@ public void Dispose()
181201

182202
_stream.Flush();
183203

204+
_endingWritten = true;
205+
}
206+
207+
/// <inheritdoc />
208+
public void Dispose()
209+
{
210+
WriteTrailer();
211+
212+
_stream.Flush();
213+
184214
if (_ownsStream)
185215
{
186216
_stream.Dispose();
187217
}
188218
}
189219

220+
private void _throwWhenEndingWritten()
221+
{
222+
if (_endingWritten) throw new InvalidOperationException("Can't change document information when PDF trailer is written to the stream.");
223+
}
224+
190225
private static void _writeHeader(PdfStream stream)
191226
{
192227
stream.WriteByte(0x25); // %

0 commit comments

Comments
 (0)