Articles in this section
Category / Section

How to convert the PDF stored in a PdfDocument object to byte array?

1 min read

In .NET PDF, you can convert the PDF stored in a PdfDocument object to byte array by creating a new Memory stream class and saving the PdfDocument as stream. This stream contains information about the PdfDocument object and can be converted to byte array.

C#

// Creates a new instance of PdfDocument class.
PdfDocument document = new PdfDocument();
// Adds a page to the document.
PdfPage page = document.Pages.Add();
// Creates Pdf graphics for the page.
PdfGraphics g = page.Graphics;
// Creates a solid brush
PdfBrush brush = new PdfSolidBrush(Color.Black);
// Sets the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f);
// Draws the text.
g.DrawString("Hello world!", font, brush, new PointF(20, 20));
//Creates a new Memory stream
MemoryStream stream = new MemoryStream();
// Saves the document as stream
document.Save(stream);
document.Close(true);
// Converts the PdfDocument object to byte form.
byte[] docBytes = stream.ToArray();
//Loads the byte array in PdfLoadedDocument
PdfLoadedDocument ldoc = new PdfLoadedDocument(docBytes);
//Saves the document and dispose it.
ldoc.Save("sample.pdf");
ldoc.Close();

Sample Link



Conclusion

I hope you enjoyed learning about how to convert the PDF stored in a PdfDocument object to byte array.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied