Skip to content

Commit d8336e4

Browse files
committed
Fix PdfBuild
1 parent 7411d7c commit d8336e4

File tree

2 files changed

+25
-71
lines changed

2 files changed

+25
-71
lines changed

PdfBuildTest/PdfBuildTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<Reference Include="System" />
3737
<Reference Include="System.Core" />
3838
<Reference Include="System.Drawing" />
39+
<Reference Include="System.Security" />
3940
<Reference Include="System.Xml.Linq" />
4041
<Reference Include="System.Data.DataSetExtensions" />
4142
<Reference Include="Microsoft.CSharp" />

PdfBuildTest/Program.cs

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5-
using System.Threading.Tasks;
65
using System.Drawing.Imaging;
76
using System.Drawing;
87
using System.IO;
@@ -15,9 +14,6 @@ internal class Program
1514
{
1615
static void Main(string[] args)
1716
{
18-
//Console.WriteLine(Adler32CheckSum.CheckSum(File.ReadAllBytes("testchksum.dat")));
19-
//Console.ReadLine();
20-
//if (true) { return; }
2117
string teststr = "爷成功了"; ;
2218
Font f = new Font(SystemFonts.DefaultFont.FontFamily, 144);
2319
Brush black = new SolidBrush(Color.Black);
@@ -65,6 +61,7 @@ public void BuildWithImages(params string[] imgPaths)
6561
entryBuilder.BuildImagePages(this, img, width, height);
6662
}
6763
}
64+
//需要分别创建然后分别添加,创建这两个对象需要已经创建的对象的数量
6865
PdfEntry entryXref = entryBuilder.buildXrefEntry(this);
6966
PdfEntry entryEnd = entryBuilder.buildTailerEntry(this);
7067
AddEntry(entryXref);
@@ -74,6 +71,10 @@ public void BuildWithImages(params string[] imgPaths)
7471

7572
public void WriteToFile(string path)
7673
{
74+
if (File.Exists(path))
75+
{
76+
File.Delete(path);
77+
}
7778
using(FileStream fs = File.OpenWrite(path))
7879
{
7980
for (int i = 0; i < xrefTable.Count; i++)
@@ -97,6 +98,7 @@ public class PdfEntry
9798
/// 当页面较多的时候可以先写出文件,然后释放内存,仅保留必要的数据结构
9899
/// </summary>
99100
public void WriteAndRelease() {
101+
//Do it later
100102
throw new NotImplementedException();
101103
}
102104

@@ -107,6 +109,7 @@ public PdfEntry(byte[] data) {
107109
}
108110

109111
public class PdfEntryBuilder {
112+
//Object id increment
110113
public int PdfEntryCounter = 1;
111114

112115
public PdfEntryBuilder() {
@@ -127,8 +130,8 @@ public PdfEntry buildPdfHeader()
127130
public PdfEntry buildSimpleObject(byte[] followingData)
128131
{
129132
using (MemoryStream ms = new MemoryStream()) {
130-
byte[] beginobj = Encoding.ASCII.GetBytes(PdfEntryCounter + " 0 obj\n");
131-
byte[] endobj = Encoding.ASCII.GetBytes("\nendobj\n\n");
133+
byte[] beginobj = (PdfEntryCounter + " 0 obj\n").Ascii();
134+
byte[] endobj = "\nendobj\n\n".Ascii();
132135
ms.Write(beginobj,0,beginobj.Length);
133136
ms.Write(followingData,0,followingData.Length);
134137
ms.Write(endobj,0,endobj.Length);
@@ -152,14 +155,14 @@ public PdfEntry buildXrefEntry(PDFBuilder builder)
152155
offset += builder.xrefTable[i].Length;
153156
}
154157
sb.Append("\n");
155-
return new PdfEntry(Encoding.ASCII.GetBytes(sb.ToString()));
158+
return new PdfEntry(sb.ToString().Ascii());
156159
}
157160

158161
public PdfEntry buildTailerEntry(PDFBuilder builder)
159162
{
160163
int xrefCount = builder.xrefTable.Count;
161164
string strs = "trailer\n<</Size " + xrefCount + "/Root 1 0 R>>\nstartxref\n" + builder.xrefTable.Sum(x => x.Length) + "\n%%EOF\n";
162-
return new PdfEntry(Encoding.ASCII.GetBytes(strs));
165+
return new PdfEntry(strs.Ascii());
163166
}
164167

165168
public void buildPageCountEntry(PDFBuilder builder,int pageCount) {
@@ -176,8 +179,8 @@ public void buildPageCountEntry(PDFBuilder builder,int pageCount) {
176179
}
177180
pageObj.Append("]>>");
178181
PdfEntry objPageObj = buildSimpleObject(pageObj.ToString().Ascii());
179-
builder.xrefTable.Add(refPageObj);
180-
builder.xrefTable.Add(objPageObj);
182+
builder.AddEntry(refPageObj);
183+
builder.AddEntry(objPageObj);
181184
}
182185

183186
public PdfEntry BuildImageEntry(Image img,int width,int height) {
@@ -220,22 +223,17 @@ private PdfEntry buildStreamObj(byte[] data) {
220223
int imgCounter = 0;
221224
public void BuildImagePages(PDFBuilder builder,Image img,int width,int height)
222225
{
223-
builder.xrefTable.Add(BuildImageEntry(img, width, height));
226+
builder.AddEntry(BuildImageEntry(img, width, height));
224227
int imgRefId = PdfEntryCounter - 1;
225228
PdfEntry refImageObj = buildSimpleObject(("<</XObject<</Im"+imgCounter+" "+imgRefId+" 0 R>>>>").Ascii());
226-
builder.xrefTable.Add(refImageObj);
229+
builder.AddEntry(refImageObj);
227230
PdfEntry propImageObj = buildStreamObj(("q "+width+" 0 0 "+height+" 0 0 cm /Im"+imgCounter+" Do Q\n").Ascii());
228-
builder.xrefTable.Add(propImageObj);
231+
builder.AddEntry(propImageObj);
229232
PdfEntry pageObj = buildSimpleObject($"<</Type/Page/MediaBox[0 0 {width} {height}]/Rotate 0/Resources {PdfEntryCounter - 2} 0 R/Contents {PdfEntryCounter - 1} 0 R/Parent 2 0 R>>".Ascii());
230-
builder.xrefTable.Add(pageObj);
233+
builder.AddEntry(pageObj);
231234
imgCounter++;
232235
}
233-
234-
235-
236236
}
237-
238-
239237
static class StringExtension {
240238
public static byte[] Ascii(this string str) {
241239
return Encoding.ASCII.GetBytes(str);
@@ -256,71 +254,26 @@ public static byte[] BitmapToPdfImage(Image i, int x = 0, int y = 0)
256254
using (MemoryStream msbefore = new MemoryStream(memoryBitmap))
257255
using (MemoryStream msafter = new MemoryStream())
258256
{
259-
260-
msafter.WriteByte(0x78);
261-
msafter.WriteByte(0x9c);
257+
//Wrap RawDeflate with zlib header and checksum
258+
byte[] zlibHeader = {0x78,0x9c};
259+
msafter.Write(zlibHeader,0,zlibHeader.Length);
262260
using (DeflateStream deflate = new DeflateStream(msafter, CompressionMode.Compress, true))
263261
{
264262
msbefore.CopyTo(deflate);
265-
266263
}
267264
msafter.WriteByte((byte)(checksum >> 0 & 0xff));
268265
msafter.WriteByte((byte)(checksum >> 8 & 0xff));
269266
msafter.WriteByte((byte)(checksum >> 16 & 0xff));
270267
msafter.WriteByte((byte)(checksum >> 24 & 0xff));
271-
return msafter.GetBuffer();
268+
return msafter.ToArray();
272269
}
273270
}
274271
}
275272
}
276-
/*
277-
The following C code computes the Adler-32 checksum of a data buffer.
278-
It is written for clarity, not for speed. The sample code is in the
279-
ANSI C programming language. Non C users may find it easier to read
280-
with these hints:
281-
282-
& Bitwise AND operator.
283-
>> Bitwise right shift operator. When applied to an
284-
unsigned quantity, as here, right shift inserts zero bit(s)
285-
at the left.
286-
<< Bitwise left shift operator. Left shift inserts zero
287-
bit(s) at the right.
288-
++ "n++" increments the variable n.
289-
% modulo operator: a % b is the remainder of a divided by b.
290-
291-
#define BASE 65521 // largest prime smaller than 65536
292-
293273

294-
Update a running Adler-32 checksum with the bytes buf[0..len-1]
295-
and return the updated checksum. The Adler-32 checksum should be
296-
initialized to 1.
297-
298-
Usage example:
299-
300-
unsigned long adler = 1L;
301-
302-
while (read_buffer(buffer, length) != EOF) {
303-
adler = update_adler32(adler, buffer, length);
304-
}
305-
if (adler != original_adler) error();
306-
307-
unsigned long update_adler32(unsigned long adler,
308-
unsigned char* buf, int len)
309-
{
310-
unsigned long s1 = adler & 0xffff;
311-
unsigned long s2 = (adler >> 16) & 0xffff;
312-
int n;
313-
314-
for (n = 0; n < len; n++)
315-
{
316-
s1 = (s1 + buf[n]) % BASE;
317-
s2 = (s2 + s1) % BASE;
318-
}
319-
return (s2 << 16) + s1;
320-
}
321-
322-
Return the adler32 of the bytes buf[0..len-1]
323-
*/
274+
/// <summary>
275+
/// Alder32 Checksum Algorithm based on RFC-1955
276+
/// </summary>
324277
public class Adler32CheckSum {
325278

326279
const uint BASE = 65521;

0 commit comments

Comments
 (0)