Create PDFs from byte arrays in C#
To create a PDF document from a byte array, follow the steps below:
Download sample- Create a
GdPicturePDFobject and aGdPictureImagingobject. - Load the source byte array by passing the byte array to the
CreateGdPictureImageFromByteArraymethod of theGdPictureImagingobject. - Create the output PDF document with the
NewPDFmethod of theGdPicturePDFobject. - Add the image to the output PDF document with the
AddImageFromGdPictureImagemethod of theGdPicturePDFobject. - Save the output PDF document with the
SaveToFilemethod of theGdPicturePDFobject. - Release unnecessary resources.
The following code creates a PDF document from a byte array based on an image source:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");using GdPicturePDF gdpicturePDF = new GdPicturePDF();using GdPictureImaging gdpictureImaging = new GdPictureImaging();// Create a byte array from an image file.byte[] byteArray = File.ReadAllBytes(@"input.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(@"output.pdf");// Release unnecessary resources.gdpicturePDF.CloseDocument();gdpictureImaging.ReleaseGdPictureImage(imageId);Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports System.IOImports GdPicture14
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Create a byte array from an image file. Dim byteArray = File.ReadAllBytes("input.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("output.pdf") ' Release unnecessary resources. gdpicturePDF.CloseDocument() gdpictureImaging.ReleaseGdPictureImage(imageId)End UsingEnd UsingUsed methods
Related topics