forked from florentbr/SeleniumBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfXRefs.cs
More file actions
85 lines (69 loc) · 2.31 KB
/
PdfXRefs.cs
File metadata and controls
85 lines (69 loc) · 2.31 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
using System;
using System.IO;
namespace Selenium.Pdf {
class PdfXRefs {
public const int ID_EMPTY = 0;
public const int ID_CATALOGUE = 1;
public const int ID_INFO = 2;
public const int ID_PAGES = 3;
public const int ID_FONTS = 4;
public const int ID_OUTLINES = 5;
public const int ID_START_GENERATE = 6;
private StreamWriter _pdfWriter;
private long[] _buffer = new long[100];
private int _count = ID_START_GENERATE;
public PdfXRefs(StreamWriter writer) {
_pdfWriter = writer;
}
public int Count {
get {
return _count;
}
}
public int CreateObject() {
return _count++;
}
public void RegisterObject(int id) {
_pdfWriter.Flush();
RegisterObject(id, _pdfWriter.BaseStream.Position);
}
public int CreateAndRegisterObject() {
int id = _count++;
RegisterObject(id);
return id;
}
/// <summary>
/// Adds a new position in the reference table.
/// </summary>
public void RegisterObject(int id, long position) {
if (id >= _buffer.Length)
IncreaseCapacity();
_buffer[id] = position;
}
/// <summary>
/// Writes the xref table to the pdf stream.
/// </summary>
/// <returns></returns>
public long Write() {
_pdfWriter.Flush();
long position = _pdfWriter.BaseStream.Position;
int count = _count;
_pdfWriter.WriteLine("xref");
_pdfWriter.WriteLine("0 " + count); //First and last object number
_pdfWriter.Write("0000000000 65535 f\r\n");
for (int i = 1; i < count; i++) {
_pdfWriter.Write(_buffer[i].ToString().PadLeft(10, '0'));
_pdfWriter.Write(" 00000 n\r\n");
}
return position;
}
/// <summary>
/// Increase the capacity of the buffer holding the positions.
/// </summary>
private void IncreaseCapacity() {
long[] newBuffer = new long[_buffer.Length * 2];
Array.Copy(_buffer, newBuffer, _count);
_buffer = newBuffer;
}
}
}