2

I have made a program that encrypts and decrypts pdf files. I want them to be locked for anyone but me and to open them via my program only. After I encrypt the pdf I have it in a byte array. Is there anyway to display the decrypted byte array of the pdf file to the form without deploying the file to the drive?

2
  • So you want to avoid writing the byte[] of the pdf file to disk? And you just want to show the byte[] (rendered as text such as a hex editor would show) on the winform? Commented Dec 7, 2015 at 18:13
  • Yes I don't want to write the decrypted pdf to the disk, because then it would all have been redundant. I want to present the decrypted bytes stored in my memory Commented Dec 7, 2015 at 18:31

2 Answers 2

1

What you need is a WinForms PDF viewer component that can load a PDF from a byte array. With the PDF viewer from Gnostice PDFOne .NET, here is the code:

PDFViewer PDFViewer1;
byte[] baPDF; // load the decrypted PDF to this byte array
...
PDFViewer1.LoadDocument(baPDF);

http://www.gnostice.com/docs/pdfone_dot_net/Gnostice_PDFOne_Windows_PDFViewer_PDFViewer_LoadDocument@byte[].html

NOTE: I work for this Gnostice company. Any other PDF viewer component, if it can load from a byte array, will work.

There is no need to save the decrypted PDF to the disk.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot! only one little thing, When I tried to use LoadDocument to the decrypted pdf's byte array I get an exception. Then I tried to load the regular pdf's byte array(with File.ReadAllBytes, no manipulating) and still the PDFException.. you might know why?
In that case, you need to send the decrypted PDF to the support people of the library.
Is it an open source library?
0

Assuming you've got some function called GetDecrypedBytes() that has the signature:

public byte[] GetDecryptedBytes();

How you encrypt/decrypt things is outside the scope of the question at hand and I assume you know how to do this.

Then you could write a function such as:

public static string ByteArrayToString(byte[] bytes)
{
  StringBuilder hex = new StringBuilder(bytes.Length * 2);
  foreach (byte b in bytes)
    hex.AppendFormat("{0:x2}", b);
  return hex.ToString();
}

And that would turn the bytes into text that could be displayed somewhere.

And then you could do something on your UI such as:

myTextBox.Text = ByteArrayToString(GetDecryptedBytes());

Obviously, one could tweak the ByteArrayToString function to change how the hexadecimal representation of the bytes is displayed.

2 Comments

Thanks but its not that, it's just shows the hexadecimal representation of the PDF file, not presenting it
You'll need to either send the PDF to a browser with the adobe add-on or use a third party control. Alternatively, you might be able to convert it to XPS format and have an element host display a WPF control that can show XPS data. There is no built-in PDF viewer for WinFotms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.