6

I posted a question related to this a while back but got no responses. Since then, I've discovered that the PDF is encoded using FlateDecode, and I was wondering if there is a way to manually decode the PDF in C# (Windows Phone 8)? I'm getting output like the following:

%PDF-1.5
%????
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
5 0 obj
<<
/Filter /FlateDecode
/Length 9
>>
stream x^+

The PDF has been created using the SyncFusion PDF controls for Windows Phone 8. Unfortunately, they do not currently have a text extraction feature, and I couldn't find that feature in other WP PDF controls either.

Basically, all I want is to download the PDF from OneDrive and read the PDF contents. Curious if this is easily doable?

Community
  • 1
  • 1
greentea
  • 347
  • 1
  • 2
  • 12
  • *if there is a way to manually decode the PDF in C#* - of course there is; several existing .Net PDF libraries are written purely in C#... *if this is easily doable* - if you use such a library, text extraction from PDFs up to a certain PDF content complexity is easy. If you try not to use such a library, chances are you have to re-invent the wheel, i.e. to program code resembling major parts of such a library. This is not easy. – mkl Sep 12 '14 at 09:30
  • 1
    Thanks for your response mkl. Unfortunately, none of the .NET libraries I've found that relate to text extraction support Windows Phone 8 (they reference a library WP8 doesn't support). Seems like I may have to re-invent the wheel or wait for one of the companies to come out with an Extract Text feature. I've read Adobe's documentation (http://acroeng.adobe.com/wp/?page_id=321) on this but it isn't clear how to manually decode it. – greentea Sep 12 '14 at 11:43

2 Answers2

5
private static string decompress(byte[] input)
{
    byte[] cutinput = new byte[input.Length - 2];
    Array.Copy(input, 2, cutinput, 0, cutinput.Length);

    var stream = new MemoryStream();

    using (var compressStream = new MemoryStream(cutinput))
    using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
        decompressor.CopyTo(stream);

    return Encoding.Default.GetString(stream.ToArray());
}

According to below similar question the first 2 bytes of the stream has to be cut from the stream. This is done in above function. Just pass all bytes of the stream to input. Make sure the bytecount is the same as the length specified.

C# decode (decompress) Deflate data of PDF File

Pete
  • 1,191
  • 12
  • 19
  • 1
    +1 for mentioning that you have to trim the first 2 bytes... It works! How would we possibly know that??? – Nacht Dec 20 '19 at 03:46
  • Thanks, yes in the other thread user1011394 explains it has something to do with RFC1951 over RC1950. I did some more research and found out these 2 bytes are the RFC 1950 - ZLIB framing bytes. Maybe another way is to use the ZLIB libary. – Pete Dec 21 '19 at 22:22
1

The easiest solution is to use DeflateStream provided by .NET framework. Example can be found in similar thread. This approach might have some pitfalls.

If this doesn't work, there are libraries (like DotNetZip), capable of deflate stream decompression. Please check this link for performance comparison.

The last possible option I see, without reinventing wheel is to use other PDF parsing libraries and use them for stream decompression, or even for whole PDF processing.

Community
  • 1
  • 1
Gotcha
  • 392
  • 2
  • 10