2

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
Community
  • 1
  • 1
Sergey
  • 161
  • 9
  • 1
    You should include in your question some sample code of how you are generating the PDF file, and a sample output PDF so that we can see what's wrong with it. – yms Jul 24 '15 at 14:26
  • Get the zlib files from bouncycastle, they will work. The license is super liberal and you can include them in your project commercial or not, it's essentially the same zlib license. – Paulo Soares Jul 24 '15 at 14:56
  • I added source code. Method that generates PDF object block works perfectly without compression. But when I use DeflateCompress it fails. I also tried to remove first two bytes from result byte array with no success. – Sergey Jul 24 '15 at 15:05
  • 1
    resultLine.AppendFormatLine("<< /Length {0} /Filter /FlateDecode >>"); there is no length in there... – yms Jul 24 '15 at 15:16
  • 1
    How are you writting the xref table? Again, you need to include a sample output PDF. – yms Jul 24 '15 at 15:24
  • I added incorrectly compressed pdf (encoded with ASCII). And I'm sorry aboyt length misprint. It is 61 in output pdf. – Sergey Jul 24 '15 at 16:28
  • You have to write your own deflate encoder if you cannot use external libraries. DeflateStream will not work. – Mihai Iancu Jul 24 '15 at 18:29
  • Mihai, are you sure that it will not work any way? May be it is possible to correct DeslateStream's output in some way to get a correct bytes? As I can see the problem is in absence of backward compatibility from RFC 1951 to RFC 1950 standart. – Sergey Jul 24 '15 at 19:54
  • I do not think it is possible to "correct" the bytes. We had the same problem with our library and we had to implement the compression. – Mihai Iancu Jul 25 '15 at 10:02
  • Thanks to everybody for answers. I think I'll had to implement deflate by myself. – Sergey Jul 25 '15 at 18:33

0 Answers0