I'm trying to write PDF files with c# and I also want to compress pdf streams. It is important not to use any 3rd parties libraries (DotNetZip etc..). The only way for me to compress pdf streams is System.IO.DeflateStream but it seems that it doesn't work: when I compress a simple stream
BT
/F9 30 Tf
10 730 Td
(Hello World!) Tj
ET
PDF cannot decompress it and displays no text. I have read similar topic Is it possible to use the .NET DeflateStream for pdf creation? but the answer contains broken link on MS bug report, and I'm not able to use any 3rd parties libraries in my project.
Is there any possibility to force DeflateStream work properly with pdf?
EDIT: source code
Here is how I write PDF object:
var resultLine = new StringBuilder();
resultLine.AppendFormatLine("{0} 0 obj", objectId);
resultLine.AppendFormatLine("<< /Length {0} /Filter /FlateDecode >>");
resultLine.AppendLine("stream");
WriteRaw(resultLine.ToString());
WriteRaw(DeflateCompress(content));
var footer = new StringBuilder();
footer.AppendLine();
footer.AppendLine("endstream");
footer.AppendLine("endobj");
It works perfect without deflate compression.
here is my Deflate method
public static byte[] DeflateCompress(string source)
{
using (var output = new MemoryStream())
{
using (var compress = new DeflateStream(output, CompressionMode.Compress))
{
var inBuffer = Encoding.UTF8.GetBytes(source);
compress.Write(inBuffer, 0, inBuffer.Length);
}
return output.ToArray();
}
}
input source variable is
"q\r\nBT\r\n/F9 30 Tf\r\n0 0 0 rg\r\n10 730 Td\r\n(Hello World!) Tj\r\nET\r\nQ\r\n"
out byte array is
{byte[61]}
[0]: 43
[1]: 228
[2]: 229
[3]: 114
[4]: 10
[5]: 225
[6]: 229
[7]: 210
[8]: 119
[9]: 179
[10]: 84
[11]: 48
[12]: 54
[13]: 80
[14]: 8
[15]: 73
[16]: 227
[17]: 229
[18]: 50
[19]: 80
[20]: 0
[21]: 193
[22]: 162
[23]: 116
[24]: 94
[25]: 46
[26]: 67
[27]: 3
[28]: 5
[29]: 115
[30]: 144
[31]: 96
[32]: 10
[33]: 47
[34]: 151
[35]: 134
[36]: 71
[37]: 106
[38]: 78
[39]: 78
[40]: 190
[41]: 66
[42]: 120
[43]: 126
[44]: 81
[45]: 78
[46]: 138
[47]: 162
[48]: 166
[49]: 66
[50]: 72
[51]: 22
[52]: 47
[53]: 151
[54]: 43
[55]: 80
[56]: 91
[57]: 32
[58]: 47
[59]: 23
[60]: 0
EDIT 2: incorrectly compressed PDF
%PDF-1.6
1 0 obj
<<
/Type /Catalog
/Version /1.6
/Pages 5 0 R
/Outlines 3 0 R
>>
endobj
2 0 obj
<<
/Title (my( title..)
/Subject ()
/Keywords ()
/Author (me..\)
/CreationDate (D:20150724042147)
/ModDate (D:20150724042147)
/Creator ()
/Producer ()
>>
endobj
3 0 obj
<<
/Type /Outlines
/Count 0
>>
endobj
4 0 obj
<<
/Type /Font
/Subtype /Type1
/Name /F6
/BaseFont /Courier-Bold
>>
endobj
5 0 obj
<<
/Type /Pages
/Count 1
/Kids [6 0 R ]
>>
endobj
6 0 obj
<<
/Type /Page
/UserUnit 1
/Parent 5 0 R
/Resources <</Font <</F6 4 0 R >>
>>
/MediaBox [0 0 612 792]
/CropBox [0 0 612 792]
/Rotate 0
/ProcSet [/PDF /Text /ImageC]
/Contents [7 0 R ]
>>
endobj
7 0 obj
<< /Length 61 /Filter /FlateDecode >>
stream
+деr
беТwіT06PIге2P Бўt^.Csђ`
/—†GjNNѕBx~QNЉў¦BH/—+P[ /
endstream
endobj
xref
0 8
0000000000 65535 f
0000000010 00000 n
0000000097 00000 n
0000000278 00000 n
0000000330 00000 n
0000000421 00000 n
0000000486 00000 n
0000000702 00000 n
trailer
<<
/Size 8
/Root 1 0 R
/Info 2 0 R
>>
startxref
840
%%EOF