Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET PDF
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.
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 28-Feb-12 15:25pm
azeeth338

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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());
   }
} 
  Permalink  
Comments
azeeth at 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)

  Print Answers RSS
0 Mathew Soji 274
1 BillWoodruff 270
2 Afzaal Ahmad Zeeshan 244
3 Sergey Alexandrovich Kryukov 240
4 Raul Iloc 160
0 OriginalGriff 6,219
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,103
3 Manas Bhardwaj 4,549
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 28 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100