I have a pdf file in a base64 string format. I need to convert it to an image (any type) to use further in my code.
I have searched SO as well as the web and I have not been successful with anything.
Does anyone have a tried and true method for this?
I can use a third party. I just need a working one!!
Thank you
I used the Apitron PDf Rasterizer
to convert my file. It is very simple to use and works great. No missing dlls and clear documentation. However, you do need to purchase it.
Thanks to all for assistance
base64 is the form of string web friendly representation of byte array. you may convert it to a byte array like this:
byte [] decodedBytes = Convert.FromBase64String (encodedText);
then you may simple save it to a file:
System.IO.File.WriteAllBytes("file.pdf", decodedBytes);
and then finally convert pdf to image
Related
I have MemoryStream data (HTML POST Data) which i need to parse it.
Converting it to string give result like below
key1=value+1&key2=val++2
Now the problem is that all this + are space in html. Am not sure why space is converting to +
This is how i am converting MemoryStream to string
Encoding.UTF8.GetString(request.PostData.ToArray())
If you are using Content-Type of application/x-www-form-urlencoded, your data needs to be url encoded.
Use System.Web.HttpUtility.UrlEncode():
using System.Web;
var data = HttpUtility.UrlEncode(request.PostData);
See more in MSDN.
You can also use JSON format for POST.
I suppose that the data you are retrieving are encoded with URL rules.
You can discover why data are encoded to this format reading this simple article from W3c school.
To encode/decode your post string you may use this couple of methods:
System.Web.HttpUtility.UrlEncode(yourString); // Encode
System.Web.HttpUtility.UrlDecode(yourString); // Decode
You can find more informations about URL manipulation functions here.
Note: If you need to encode/decode an array of string you need to enumerate your collection with a for or foreach statement. Remember that with this kind of cycles you cannot directly change the cycle variable value during the enumeration (so probably you need a temporary storage variable).
At least, to efficiently parse strings, I suggest you to use the System.Text.RegularExpression.Regex class and learn the regex "language".
You can find some example on how to use Regex here; Regex101 site has also a C# code generator that shows you how to translate your regex into code.
My goal is to convert a string to a ByteArray, write this ByteArray AS a ByteArray to a string so it's unreadable but still readable again upon "ByteArray to String" conversion in C#.
This is how my code is right now:
string json = "{\"database\":{\"tables\":{\"Users\":[\"column\":{\"id\":\"1\", \"name\":\"Test\"}]}}}";
var bytes = Encoding.ASCII.GetBytes(json);
File.WriteAllBytes("database.dat", bytes);
This works in theory, however the final output file has the same content of the string, and not the converted ByteArray. This is what the file contains:
database.dat
{"database":{"tables":{"Users":["column":{"id":"1", "name":"Test"}]}}}
But I expected something like
l4#ˆC}nC(YXX>AI0ve‚22úL«*“ÑÃYgPæaiäi
’Ê¢±·Ä¿|^Û×RÉ!×¹ÝYPZŠO•QÚÉèT“g‘Ѳ¬¡\g²Ô
What am I doing wrong? Is this not a ByteArray? Is there another way to convert data to an unreadable file, and then be able to convert it back into a string in my program?
What am I doing wrong? Is this not a ByteArray? Is there another way
to convert data to an unreadable file, and then be able to convert it
back into a string in my program?
Depends how much unreadable you want it to be? In the most extreme case you might need to use encryption.
In your case, you are storing ASCII representation of the string into a file, so of course a text editor can read it back to you.
One way could be try converting the byte array which you obtained to base64 encoded string - and store that string in file. That way it will not be easily readable, however, someone else can still decode it if he/she tries. So the security guarantees provided aren't that much. But again, depends on your needs.
I have an ImageButton in my website that has a dynamic source well it basically looks like this: "data:image/svg+xml;base64,...."
So I am trying to insert an image in to PDF using that. This is the code I use
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Uri(((ImageButton) FindControl(fieldKey)).ImageUrl));
I either get a "The URI is empty" error or a path not found.
Any ideas how to approach this?
I doubt anyone would google this but I figured it out so why not post an anwser.
To implement data img types in to PDF remove the prefix part and then convert it back from base 64 in to array byte.
string theSource = ((ImageButton)FindControl(fieldKey)).ImageUrl.Replace("data:image/png;base64,", "");
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(Convert.FromBase64String(theSource));
Looking for recommendations for a simple way to write a string to a file so that the resulting file would not be readable/editable in Notepad. I thought about using some serious encryption, but I think that is just over kill for what I need. Just looking for a easy unreadable text format to convert to and from.
You could Base64 encode it:
string text = "My quasi secret text.";
byte[] buffer = System.Text.UTF8Encoding.GetBytes(text);
string protectedText = Convert.ToBase64String(buffer);
File.WriteAllText(filename, protectedText)
EDIT: Updated to UTF8Encoding to handle unicode.
How about using a binary formatter to serialize it?
That would pretty well ensure that people opening the file in Notepad would just end up with gobbledegook. But (it had to be said) if you attach any importance to people not being able to see or mess with this data, it needs to be truly encrypted.
Google translate it into French then translate it back. This won't work if your users speak French.
If it doesn't need to be encrypted, then I would suggest Convert.ToBase64String/Convert.FromBase64String.
Simple and reliable.
encryption, or maybe encoding (e.g. Base64 encoding? - or even a simpler one?). This would make it non readable, but still editable.
Something super simple would be Base64 encoding:
Convert.ToBase64String();
Encryption: to put (a message) into code or to distort so that it cannot be understood without the appropriate decryption equipment
By definition, you are trying to encrypt the data. The only difference is you only need an encryption that's strong enough to not allow laymen from accessing it's contents.
Like others have mentioned, using Base64 would effectively encrypt the data against your average user.
So in the end here is what I did.
TO Write the data:
//Write the text in a way the user wont recognize if they open the file in notepad.
byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(data);
string protectedText = Convert.ToBase64String(buffer);
File.WriteAllText(UserToUserLevelDataFile, protectedText);
TO Read the data:
string longText = File.ReadAllText(UserToUserLevelDataFile);
data = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(longText));
Works like a charm, thanks everyone for your ideas!
I've looked everywhere for the answer to this question but cant find anything so hoping you guys can help me on here.
Basically I want to insert an image into an element in xml document that i have using c#
I understand i have to turn it into bytes but im unsure of how to do this and then insert it into the correct element...
please help as i am a newbie
Read all the bytes into memory using
File.ReadAllBytes().
Convert the bytes to a Base64 string
using Convert.ToBase64String().
Write the Base64 Encoded string to
your element content.
Doneski!
Here's an example in C# for writing and reading images to/from XML.
You can use a CDATA part or simply put all the bytes in their hexadecimal form as a string.
Another option is to use a base64 encoding
The element you use is up to you.
http://www.dreamincode.net/code/snippet1335.htm seems to do exactly what you want to do. It might be something you might want to try out. Note that it is in VB.NET which you can easily convert to C#.
XML can only contain characters, it can't contain an image. There are various ways you can represent an image using characters, for example by encoding the image in PNG and then encoding the PNG in base64; or you could generate an element that contains a link to a URI from where the image can be retrieved. All such conventions have to be agreed between sender and recipient. So before you rush into base64 encoding, check that this is what the recipient expects.