-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPdfWriter.cs
More file actions
345 lines (281 loc) · 12.2 KB
/
PdfWriter.cs
File metadata and controls
345 lines (281 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Synercoding.FileFormats.Pdf.LowLevel;
using Synercoding.FileFormats.Pdf.LowLevel.Colors.ColorSpaces;
using Synercoding.FileFormats.Pdf.LowLevel.Extensions;
using Synercoding.FileFormats.Pdf.LowLevel.Internal;
using Synercoding.FileFormats.Pdf.LowLevel.XRef;
using System.IO.Compression;
using System.Reflection;
namespace Synercoding.FileFormats.Pdf;
/// <summary>
/// This class is the start point for creating a pdf file
/// </summary>
public sealed class PdfWriter : IDisposable
{
private readonly bool _ownsStream;
private readonly ObjectStream _objectStream;
private readonly TableBuilder _tableBuilder = new TableBuilder();
private readonly PageTree _pageTree;
private readonly Catalog _catalog;
private bool _endingWritten = false;
/// <summary>
/// Constructor for <see cref="PdfWriter"/>
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to write the PDF file</param>
public PdfWriter(Stream stream)
: this(stream, true)
{ }
/// <summary>
/// Constructor for <see cref="PdfWriter"/>
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to write the PDF file</param>
/// <param name="ownsStream">If the stream is owned, then when this <see cref="PdfWriter"/> is disposed, the stream is also disposed.</param>
public PdfWriter(Stream stream, bool ownsStream)
{
var pdfStream = new PdfStream(stream);
_writeHeader(pdfStream);
_objectStream = new ObjectStream(pdfStream, _tableBuilder);
_pageTree = new PageTree(_tableBuilder.ReserveId());
_catalog = new Catalog(_tableBuilder.ReserveId(), _pageTree);
DocumentInformation = new DocumentInformation(_tableBuilder.ReserveId())
{
Producer = $"Synercoding.FileFormats.Pdf {typeof(PdfWriter).GetTypeInfo().Assembly.GetName().Version}",
CreationDate = DateTime.Now
};
_ownsStream = ownsStream;
}
/// <summary>
/// Document information, such as the author and title
/// </summary>
public DocumentInformation DocumentInformation { get; }
/// <summary>
/// Returns the number of pages already added to the writer
/// </summary>
public int PageCount
=> _pageTree.PageCount;
/// <summary>
/// Set meta information for this document
/// </summary>
/// <param name="infoAction">Action used to set meta data</param>
/// <returns>Returns this <see cref="PdfWriter"/> to chain calls</returns>
public PdfWriter SetDocumentInfo(Action<DocumentInformation> infoAction)
=> SetDocumentInfo(infoAction, static (action, did) => action(did));
/// <summary>
/// Set meta information for this document
/// </summary>
/// <typeparam name="T">Type of data to pass to the action</typeparam>
/// <param name="data">Data to be used in the action</param>
/// <param name="infoAction">Action used to set meta data</param>
/// <returns>Returns this <see cref="PdfWriter"/> to chain calls</returns>
public PdfWriter SetDocumentInfo<T>(T data, Action<T, DocumentInformation> infoAction)
{
_throwWhenEndingWritten();
infoAction(data, DocumentInformation);
return this;
}
/// <summary>
/// Add a page to the pdf file
/// </summary>
/// <param name="pageAction">Action used to setup the page</param>
/// <returns>Returns this <see cref="PdfWriter"/> to chain calls</returns>
public PdfWriter AddPage(Action<PdfPage> pageAction)
=> AddPage(pageAction, static (action, page) => action(page));
/// <summary>
/// Add a page to the pdf file
/// </summary>
/// <param name="data">Data passed into the action</param>
/// <param name="pageAction">Action used to setup the page</param>
/// <returns>Returns this <see cref="PdfWriter"/> to chain calls</returns>
public PdfWriter AddPage<T>(T data, Action<T, PdfPage> pageAction)
{
_throwWhenEndingWritten();
using (var page = new PdfPage(_tableBuilder, _pageTree))
{
pageAction(data, page);
_writePageAndResourcesToObjectStream(page);
}
return this;
}
/// <summary>
/// Add a page to the pdf file
/// </summary>
/// <param name="pageAction">Action used to setup the page</param>
/// <returns>Returns an awaitable task that resolves into this <see cref="PdfWriter"/> to chain calls</returns>
public async Task<PdfWriter> AddPageAsync(Func<PdfPage, Task> pageAction)
=> await AddPageAsync(pageAction, static async (action, page) => await action(page));
/// <summary>
/// Add a page to the pdf file
/// </summary>
/// <param name="data">Data passed into the action</param>
/// <param name="pageAction">Action used to setup the page</param>
/// <returns>Returns an awaitable task that resolves into this <see cref="PdfWriter"/> to chain calls</returns>
public async Task<PdfWriter> AddPageAsync<T>(T data, Func<T, PdfPage, Task> pageAction)
{
_throwWhenEndingWritten();
using (var page = new PdfPage(_tableBuilder, _pageTree))
{
await pageAction(data, page);
_writePageAndResourcesToObjectStream(page);
}
return this;
}
/// <summary>
/// Add an <see cref="SixLabors.ImageSharp.Image"/> to the pdf file and get the <see cref="Image"/> reference returned
/// </summary>
/// <param name="image">The image that needs to be added.</param>
/// <returns>The image reference that can be used in pages</returns>
public Image AddImage(Image<Rgba32> image)
{
_throwWhenEndingWritten();
var pdfImage = Image.Get(_tableBuilder, image);
_objectStream.Write(pdfImage);
return pdfImage;
}
/// <summary>
/// Add a separation image to the <see cref="PdfWriter"/>.
/// </summary>
/// <param name="separation">The <see cref="Separation"/> to use.</param>
/// <param name="image">The image to use.</param>
/// <param name="grayScaleMethod">The <see cref="GrayScaleMethod"/> to use.</param>
/// <param name="decodeArray">Optional decode array to use, default value is <c>[ 0.0 1.0 ]</c></param>
/// <returns>The SeparationImage reference that can be used in pages</returns>
public Image AddSeparationImage(Separation separation, Image<Rgba32> image, GrayScaleMethod grayScaleMethod, double[]? decodeArray = null)
{
_throwWhenEndingWritten();
decodeArray ??= new double[] { 0, 1 };
if(decodeArray.Length != 2)
throw new ArgumentOutOfRangeException(nameof(decodeArray), "Length of decode array for separation images should be 2.");
if (decodeArray.Any(v => v < 0 || v > 1))
throw new ArgumentOutOfRangeException(nameof(decodeArray), "All values of the decode array should be between 0 and 1.");
var id = _tableBuilder.ReserveId();
var mask = Image.GetMask(_tableBuilder, image);
var imageStream = Image.AsImageByteStream(image, grayScaleMethod);
var pdfImage = new Image(id, imageStream, image.Width, image.Height, separation, mask, decodeArray, StreamFilter.FlateDecode);
_objectStream.Write(pdfImage);
return pdfImage;
}
/// <summary>
/// Add an jpg <see cref="Stream"/> to the pdf file and get the <see cref="Image"/> reference returned
/// </summary>
/// <remarks>
/// The <paramref name="jpgStream"/> is not checked, and is used as is. Make sure only streams that represent a JPG are used.
/// </remarks>
/// <param name="jpgStream">The <see cref="Stream"/> of the jpg image that needs to be added.</param>
/// <param name="originalWidth">The width of the image in the <paramref name="jpgStream"/>.</param>
/// <param name="originalHeight">The height of the image in the <paramref name="jpgStream"/>.</param>
/// <param name="colorSpace">The color space of the jpg image.</param>
/// <returns>The image reference that can be used in pages</returns>
public Image AddJpgUnsafe(Stream jpgStream, int originalWidth, int originalHeight, ColorSpace colorSpace)
{
_throwWhenEndingWritten();
var id = _tableBuilder.ReserveId();
var pdfImage = new Image(id, jpgStream, originalWidth, originalHeight, colorSpace, null, null, StreamFilter.DCTDecode);
_objectStream.Write(pdfImage);
return pdfImage;
}
/// <summary>
/// Write the PDF trailer; indicates that the PDF is done.
/// </summary>
/// <remarks>
/// Other calls to this <see cref="PdfWriter"/> will throw or have no effect after call this.
/// </remarks>
public void WriteTrailer()
{
if (_endingWritten)
return;
_objectStream
.Write(_pageTree)
.Write(_catalog)
.Write(DocumentInformation);
_writePdfEnding();
_objectStream.InnerStream.Flush();
_endingWritten = true;
}
/// <inheritdoc />
public void Dispose()
{
WriteTrailer();
_objectStream.InnerStream.Flush();
if (_ownsStream)
{
_objectStream.InnerStream.Dispose();
}
}
private void _writePageAndResourcesToObjectStream(PdfPage page)
{
_objectStream.Write(page);
foreach (var kv in page.Resources.Images)
_objectStream.Write(kv.Value);
foreach (var (font, refId) in page.Resources.FontReferences)
_objectStream.Write(refId, font);
foreach (var (separation, _) in page.Resources.SeparationReferences)
_objectStream.Write(separation);
foreach (var (state, (_, refId)) in page.Resources.ExtendedGraphicsStates)
_objectStream.Write(refId, state);
_objectStream.Write(page.Content.RawContentStream);
}
internal static Stream FlateEncode(Stream inputStream)
{
var outputStream = new MemoryStream();
inputStream.Position = 0;
using (var zlibStream = new ZLibStream(outputStream, CompressionLevel.SmallestSize, true))
{
inputStream.CopyTo(zlibStream);
}
outputStream.Position = 0;
return outputStream;
}
private void _throwWhenEndingWritten()
{
if (_endingWritten) throw new InvalidOperationException("Can't change document information when PDF trailer is written to the stream.");
}
private static void _writeHeader(PdfStream stream)
{
stream.WriteByte(0x25); // %
stream.WriteByte(0x50); // P
stream.WriteByte(0x44); // D
stream.WriteByte(0x46); // F
stream.WriteByte(0x2D); // -
stream.WriteByte(0x31); // 1
stream.WriteByte(0x2E); // .
stream.WriteByte(0x37); // 7
stream.WriteByte(0x0D); // CR
stream.WriteByte(0x0A); // LF
stream.WriteByte(0x25); // %
stream.WriteByte(0x81); // binary indicator > 128
stream.WriteByte(0x82); // binary indicator > 128
stream.WriteByte(0x83); // binary indicator > 128
stream.WriteByte(0x84); // binary indicator > 128
stream.WriteByte(0x0D); // CR
stream.WriteByte(0x0A); // LF
}
private void _writePdfEnding()
{
if (!_tableBuilder.Validate())
throw new InvalidOperationException("XRef table is invalid.");
var xRefTable = _tableBuilder.GetXRefTable();
uint xRefPosition = xRefTable.WriteToStream(_objectStream.InnerStream);
_writeTrailer(_objectStream.InnerStream, xRefPosition, xRefTable.Section.ObjectCount, _catalog.Reference, DocumentInformation.Reference);
}
private static void _writeTrailer(PdfStream stream, uint startXRef, int size, PdfReference root, PdfReference documentInfo)
{
stream
.Write("trailer")
.NewLine()
.Dictionary((size, root, documentInfo), static (triple, dictionary) =>
{
var (size, root, documentInfo) = triple;
dictionary
.Write(PdfName.Get("Size"), size)
.Write(PdfName.Get("Root"), root)
.Write(PdfName.Get("Info"), documentInfo);
})
.NewLine()
.Write("startxref")
.NewLine()
.Write(startXRef)
.NewLine()
.Write("%%EOF");
}
}