In an XML WebResponse
I get a URL tag which has a link to a PDF file. Example of the URL value is: https://www.member-data.com/files/hb/c8955fc4d6160ec0fd87f4879c6496d3.pdf
). I have to convert this PDF to a byte array, how do I do this in C#?
You can read data from a PDF file using the read() method of the FileInputStream class this method requires a byte array as a parameter.
A consecutive sequence of variables of the data type byte, in computer programming, is known as a byte array. An array is one of the most basic data structures, and a byte is the smallest standard scalar type in most programming languages.
Read the image using the read() method of the ImageIO class. Create a ByteArrayOutputStream object. Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class. Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.
Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.
You can use WebClient.DownloadData
, which has a default return value of a byte array. e.g.
byte[] bytes = myClient.DownloadData("https://www.member-data.com/files/hb/c8955fc4d6160ec0fd87f4879c6496d3.pdf");
Also, this assumes that you want the actual file in a byte array, not the content (text) of the PDF. That's a whole other ball of wax.
You can use WebClient.DownloadData()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With