Click here to Skip to main content
15,068,929 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm working on a project in which requirement is to store byte array of any type [i.e. doc, jpg, pdf, xps etc] to pdf format only.

I'll receive byte array only.

I'll not have any knowledge of from which type of file this byte array is generated.

How can i save this byte array to PDF format only?

What I have tried:

File.WriteAllBytes(path+"/file.pdf", bytes);

above code works fine if byte array is created from pdf file only.
Updated 16-May-17 1:20am
Tomas Takac 16-May-17 7:14am
What you are doing there is just changing the file extension and that won't work. The word you are looking for is "conversion". Try to google that in conjunction with pdf, doc...
Sachin Makwana 16-May-17 7:22am
Thanks Sir for the reply. Using the keyword you suggest me, I found about iTextSharp.
Now i need to learn about iTextSharp.

1 solution

You have to know the type of data being passed. Without that information you can't do anything useful.

If you know that the data represents some kind of file type you can try to detect the type. With Linux there is the file command that uses the magic(5): file command's magic pattern file - Linux man page[^] to detect file types. With Windows you have to implement something similar (or search the web for existing implementations).

Once you identified the type you can save the data as file with the appropriate extension and use a type specific converter to create a PDF file.

PDF files for example begin with the string "%PDF-". If your data begins with these characters you can assume that it is a PDF file and can be stored as such.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900