Skip to content

Commit 694a863

Browse files
committed
Refactored write trailer into single method
1 parent 0a2dd09 commit 694a863

File tree

1 file changed

+19
-38
lines changed

1 file changed

+19
-38
lines changed

src/Synercoding.FileFormats.Pdf/PdfWriter.cs

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Reflection;
7+
using System.Threading.Tasks;
78

89
namespace Synercoding.FileFormats.Pdf
910
{
@@ -236,47 +237,27 @@ private void _writePdfEnding()
236237
var xRefTable = _tableBuilder.GetXRefTable();
237238
uint xRefPosition = xRefTable.WriteToStream(_stream);
238239

239-
var trailer = new Trailer(xRefPosition, xRefTable.Section.ObjectCount, _catalog, DocumentInformation);
240-
trailer.WriteToStream(_stream);
240+
_writeTrailer(_stream, xRefPosition, xRefTable.Section.ObjectCount, _catalog.Reference, DocumentInformation.Reference);
241241
}
242242

243-
private readonly struct Trailer
243+
private void _writeTrailer(PdfStream stream, uint startXRef, int size, PdfReference root, PdfReference documentInfo)
244244
{
245-
public Trailer(uint startXRef, int size, Catalog root, DocumentInformation documentInfo)
246-
{
247-
StartXRef = startXRef;
248-
Size = size;
249-
Root = root.Reference;
250-
DocumentInfo = documentInfo.Reference;
251-
}
252-
253-
public uint StartXRef { get; }
254-
public int Size { get; }
255-
public PdfReference Root { get; }
256-
public PdfReference DocumentInfo { get; }
257-
258-
internal uint WriteToStream(PdfStream stream)
259-
{
260-
var position = (uint)stream.Position;
261-
262-
stream
263-
.Write("trailer")
264-
.NewLine()
265-
.Dictionary(this, static (trailer, dictionary) =>
266-
{
267-
dictionary
268-
.Write(PdfName.Get("Size"), trailer.Size)
269-
.Write(PdfName.Get("Root"), trailer.Root)
270-
.Write(PdfName.Get("Info"), trailer.DocumentInfo);
271-
})
272-
.Write("startxref")
273-
.NewLine()
274-
.Write(StartXRef)
275-
.NewLine()
276-
.Write("%%EOF");
277-
278-
return position;
279-
}
245+
stream
246+
.Write("trailer")
247+
.NewLine()
248+
.Dictionary((size, root, documentInfo), static (triple, dictionary) =>
249+
{
250+
var (size, root, documentInfo) = triple;
251+
dictionary
252+
.Write(PdfName.Get("Size"), size)
253+
.Write(PdfName.Get("Root"), root)
254+
.Write(PdfName.Get("Info"), documentInfo);
255+
})
256+
.Write("startxref")
257+
.NewLine()
258+
.Write(startXRef)
259+
.NewLine()
260+
.Write("%%EOF");
280261
}
281262
}
282263
}

0 commit comments

Comments
 (0)