Share via


create pdf from byte array in c#

Question

Monday, March 25, 2019 2:16 PM

Hi,
I need to convert the byte array to PDF using  C# 

can anyone help me in this regards

Thanks in advance,
Md. Altaf Hosssain

All replies (5)

Monday, March 25, 2019 3:05 PM

I need to convert the byte array to PDF using  C# 

I rather doubt that you will get much help without providing a lot more
details about what exactly is in this byte array. What does it contain?
How was it created?

Generally speaking, it's usually simpler to use an existing library to create
PDF files. Such as:

PDF File Writer C# Class Library (Version 1.22.0)
https://www.codeproject.com/Article.aspx?tag=198374989783137036&_z=9548260

Also see the recommendations in this thread:

create pdf file from C#.net
https://social.msdn.microsoft.com/Forums/vstudio/en-US/4a13a735-633c-445e-b633-268d96293170/create-pdf-file-from-cnet

Which include among others::

PDFsharp
http://pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=54&Itemid=64

  • Wayne

Tuesday, March 26, 2019 2:10 AM | 1 vote

You can add reference to spire.pdf.dll and then use code like below to convert byte array to pdf with c#:

PdfDocument doc = new PdfDocument();

doc.LoadFromBytes(byteArray);

doc.SaveToFile(.......)

Hope it helps.


Tuesday, March 26, 2019 4:55 PM

Is it possible to do with itextsharp, if you do, please let me know?
Thank you


Wednesday, March 27, 2019 6:01 AM

Hi

Thanks for the feedback.

You could refer to the following code to create pdf from a byte array by using itextsharp.

 Document doc = new Document(PageSize.A4);
            string path = "D:\\test.pdf";
            Document myDocument = new Document();
            PdfWriter.GetInstance(myDocument, new FileStream(path, FileMode.Create));
            myDocument.Open();
            myDocument.Add(new Paragraph(Encoding.UTF8.GetString(picarr)));
            myDocument.Close();

Best Regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, March 27, 2019 11:48 AM

HiThanks, for your answermy pdf text Unicode. So Using Encoding.utf8 than text is broken.  Encoding.UTF8.GetString(picarr) best Regards Altaf hossain