• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DomCR / ACadSharp.Pdf / 14305897173

07 Apr 2025 09:38AM UTC coverage: 2.035% (-73.9%) from 75.959%
14305897173

push

github

DomCR
version 0.1.0-beta

125 of 8279 branches covered (1.51%)

Branch coverage included in aggregate %.

648 of 29704 relevant lines covered (2.18%)

4.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ACadSharp.Pdf/Core/PdfWriter.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Globalization;
4
using System.IO;
5
using System.Text;
6

7
#if NETFRAMEWORK
8
using CSUtilities.Extensions;
9
#endif
10

11
namespace ACadSharp.Pdf.Core
12
{
13
        internal class PdfWriter : IDisposable
14
        {
15
                private int _currNumber = 1;
×
16
                private Encoding _encoding = Encoding.ASCII;
×
17
                private Stream _stream;
18
                private PdfDocument _document;
19
                private PdfConfiguration _configuration;
20

21
                private List<long> _xrefs = new();
×
22

23
                public PdfWriter(Stream stream, PdfDocument document, PdfConfiguration configuration)
×
24
                {
×
25
                        _stream = stream;
×
26
                        _document = document;
×
27
                        _configuration = configuration;
×
28
                }
×
29

30
                public void Write()
31
                {
×
32
                        this.assignIds();
×
33

34
                        this.writeLine("%PDF-1.5");
×
35
                        this.write(new byte[6] { 0x25, 0xA1, 0xD3, 0xF4, 0xCC, 0xA });
×
36

37
                        //Information
38
                        this.writePdfDictionary(_document.Information);
×
39

40
                        //Catalog
41
                        this.writePdfDictionary(_document.Catalog);
×
42

43
                        //Outlines (optional)
44

45
                        //Pages
46
                        this.writePdfDictionary(_document.Pages);
×
47
                        //Page
48
                        foreach (PdfPage item in this._document.Pages)
×
49
                        {
×
50
                                this.writePdfDictionary(item);
×
51
                                this.writePdfDictionary(item.Contents);
×
52
                        }
×
53

54
                        long xrefPos = this.writeXRef();
×
55

56
                        this.writeTrailer();
×
57

58
                        this.writeLine(PdfKey.StartXref);
×
59
                        this.writeLine(xrefPos.ToString(CultureInfo.InvariantCulture));
×
60
                        this.write("%%EOF");
×
61
                }
×
62

63
                /// <inheritdoc/>
64
                public void Dispose()
65
                {
×
66
                        this._stream.Dispose();
×
67
                }
×
68

69
                private void assignIds()
70
                {
×
71
                        this.assignId(this._document.Information);
×
72
                        this.assignId(this._document.Catalog);
×
73
                        this.assignId(this._document.Pages);
×
74
                }
×
75

76
                private void assignId(PdfItem item)
77
                {
×
78
                        item.SetId(ref this._currNumber);
×
79
                }
×
80

81
                private void writePdfDictionary(PdfDictionary dict)
82
                {
×
83
                        this._xrefs.Add(this._stream.Position);
×
84
                        this.write(dict.GetPdfForm(this._configuration));
×
85
                }
×
86

87
                private long writeXRef()
88
                {
×
89
                        long position = this._stream.Position;
×
90

91
                        this.writeLine("xref");
×
92
                        this.writeLine($"0 {this._currNumber}");
×
93
                        this.writeLine($"0000000000 65535 f");
×
94

95
                        foreach (var item in this._xrefs)
×
96
                        {
×
97
                                this.writeLine($"{item.ToString("0000000000", CultureInfo.InvariantCulture)} 00000 n");
×
98
                        }
×
99

100
                        return position;
×
101
                }
×
102

103
                private void writeTrailer()
104
                {
×
105
                        this.writeLine("trailer");
×
106
                        this.writeLine("<<");
×
107
                        this.writeLine($"/Size {this._currNumber}");
×
108
                        this.writeLine($"/Info {this._document.Information.Id} 0 R");
×
109
                        this.writeLine($"/Root {this._document.Catalog.Id} 0 R");
×
110
                        this.writeLine(">>");
×
111
                }
×
112

113
                private void write(byte[] bytes)
114
                {
×
115
                        _stream.Write(bytes);
×
116
                }
×
117

118
                private void write(string str)
119
                {
×
120
                        this.write(this._encoding.GetBytes(str));
×
121
                }
×
122

123
                private void writeLine(string str)
124
                {
×
125
                        this.write(str + "\n");
×
126
                }
×
127
        }
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc