Click here to Skip to main content
14,970,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an application in .NET which creates simple text based PDF files from scratch. I have been successful in creating PDF files where I put content text as it is e.g. Td (Hello World) Tj.

But now I want to reduce the size of the PDf files by applying some form of compression to the content. I have so far implemented native .NET Gzip and zlib compression classes. I have managed to compress the content (text) and reduce the size of the PDF file considerably. But the catch is that when I open PDF file in Acrobet reader, the file/pages are blank with no content. Although when I open the PDF file in Notepad, I can see the content has been compressed.

What am I doing wrong? below is something that I am doing when writing PDF content. Am I missing some required PDF element tags? Below compression is zlib.
HTML
5 0 obj<</Filter /FlateDecode/Length 571>>stream
xœíVM›0½#ñæØî!;6ØàÜ€°›¨Xâ´? R«Vê¥=Tê¯ïÂGøPÚlw%F€åÁï=?Ð |¶-_À¶Bm[÷påþd[ú#Ðå']@ÿ †füeRß«E 0NS4ƒ#AHºõ*é;xóV}Wpe|ˆ:"ƒôÝ.}„(+ò¬ô.KG×3~OGÆ€¹k¦Ö§Ž0×EÅÈfb¡È‹,{ˆ7P‚dwЪÇ0,šŽ"à?UÏmâ‚(ÊŽ©®K•©}=‚Ãvi™-çÛ'x$ǸÎtŸ67ƒýij&éqoø‘F4dDóRË—™éÊY"è("Êi4ôÑ[    ìª*y/S®Z!ΗòŠè¼Xª•qŸ§$¯ªüQȼÒ÷u8^   ]ß
Îó ©b„£8!xŠ{õs¡×T#rÎ¥ÃÚzWxî9dªE·¤:ü>dD• nŒ8E•~I\€Ît¬§ÞÀ™è]#œp:LûZî·ð|å2AÑ"ãxYËÓ¢ÛÚ†Ká»R4Ÿ®ïVð«;ó´eÚ§ÚgAm/Tvœ?Ø%æìR«¨±
C"ÈUç!¡|WÈ—E†™S궛%¸Á%[ã³v³$ð—›5Ík˜¹n³fU~¤Ê›K…who`Ú¯s˜Ôã9ƒýhKK¹´"KK9ˆ¥¥<[´´"KKÙ«wKK9+––rKKùÿ¢ÛRÚV¬u…à
endstream
Posted

1 solution

you cannot ToArray the memory stream until you dispose the DeflateStream (as flush is acutally not implemented (and Deflate/GZip compress blocks of data); the final block is only written on close/dispose.

Try the below code:
C#
public static string Base64Compress(string data, Encoding enc) 
{
   using (var ms = new MemoryStream())
   {
     using (var ds = new DeflateStream(ms, CompressionMode.Compress))
     {
       byte[] b = enc.GetBytes(data);
       ds.Write(b, 0, b.Length);
     }
      return Convert.ToBase64String(ms.ToArray());
   }
} 
   
Comments
azeeth 28-Feb-12 21:17pm
   
Thanks mate, But it didn't really fix anything. PDF file pages are still blank.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900