Create PDFs from Byte Arrays in C#
To create a PDF document from a byte array, follow these steps:
-
Create a
GdPicturePDF
object and aGdPictureImaging
object. -
Load the source byte array by passing its path to the
CreateGdPictureImageFromByteArray
method of theGdPictureImaging
object. -
Create the output PDF document with the
NewPDF
method of theGdPicturePDF
object. -
Add the image to the output PDF document with the
AddImageFromGdPictureImage
method of theGdPicturePDF
object. -
Save the output PDF document with the
SaveToFile
method of theGdPicturePDF
object. -
Release unnecessary resources.
The code below creates a PDF document from a byte array based on an image source:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); using GdPictureImaging gdpictureImaging = new GdPictureImaging(); // Create a byte array from an image file. byte[] byteArray = File.ReadAllBytes(@"C:\temp\source.png"); // Load the source byte array. int imageId = gdpictureImaging.CreateGdPictureImageFromByteArray(byteArray); // Create the output PDF document. gdpicturePDF.NewPDF(); // Add the image to the output PDF document. gdpicturePDF.AddImageFromGdPictureImage(imageId, false, true); // Save the output PDF document. gdpicturePDF.SaveToFile(@"C:\temp\output.pdf"); // Release unnecessary resources. gdpicturePDF.CloseDocument(); gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Create a byte array from an image file. Dim byteArray = File.ReadAllBytes("C:\temp\source.png") ' Load the source byte array. Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromByteArray(byteArray) ' Create the output PDF document. gdpicturePDF.NewPDF() ' Add the image to the output PDF document. gdpicturePDF.AddImageFromGdPictureImage(imageId, False, True) ' Save the output PDF document. gdpicturePDF.SaveToFile("C:\temp\output.pdf") ' Release unnecessary resources. gdpicturePDF.CloseDocument() gdpictureImaging.ReleaseGdPictureImage(imageId) End Using End Using
This example is for illustrative purposes only. GdPicture.NET offers simpler ways to convert between different file types. For more information, see conversion.