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?
-
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?itsmatt– itsmatt2015-12-07 18:13:34 +00:00Commented 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 memoryErez Konforti– Erez Konforti2015-12-07 18:31:18 +00:00Commented Dec 7, 2015 at 18:31
2 Answers
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);
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.
3 Comments
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.