PDF processing control Aspose.PDF function demonstration: using C# to convert PDF files and byte arrays to and from each other

Byte array helps in storing or transmitting data. Likewise, PDF file format is popular due to its features and compatibility. You can use C# language to convert PDF files to byte arrays and you can also convert byte arrays to PDF files. This can help store and archive PDF files in a database more efficiently, and also by using byte arrays to serialize data. Let's explore the interconvertibility of these formats.

  • Convert PDF file to byte array using C#
  • Convert byte array to PDF file using C#

Click to download the latest version of Aspose.PDF icon-default.png?t=N7T8https://www.evget.com/product/565/download

Convert PDF file to byte array using C#

A PDF can be converted to a byte array in order to transfer it or store it for further processing. For example, you might need to serialize a PDF document, then converting it to a byte array would help. You need to follow these steps to convert PDF to byte array:

  • Load input PDF file
  • Initialize byte array
  • Initialize the FileStream object
  • Load file contents into byte array

The following code shows how to convert a PDF file to a byte array using C#, where the resulting ByteArray is passed to a method that converts the input file to an image:

dataDir = @"D:\Test\";

// Load input PDF file
string inputFile = dataDir + @"testpdf.pdf";

// Initialize a byte array
byte[] buff = null;

// Initialize FileStream object
FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(inputFile).Length;

// Load the file contents in the byte array
buff = br.ReadBytes((int) numBytes);
fs.Close();

// Work with the PDF file in byte array
ConvertPDFToJPEG(buff, 300, dataDir);


public static void ConvertPDFToJPEG(Byte[] PDFBlob, int resolution, string dataDir)
{
    // Open document
    using (MemoryStream InputStream = new MemoryStream(PDFBlob))
    {
        Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(InputStream);

        for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++) { using (FileStream imageStream = new FileStream(dataDir + "image" + pageCount + "_out" + ".jpg", FileMode.Create)) { // Create JPEG device with specified attributes // Width, Height, Resolution, Quality // Quality [0-100], 100 is Maximum // Create Resolution object Aspose.Pdf.Devices.Resolution res = new Aspose.Pdf.Devices.Resolution(resolution); // JpegDevice jpegDevice = new JpegDevice(500, 700, resolution, 100); // added the following to determine if landscape or not Int32 height, width = 0; PdfFileInfo info = new PdfFileInfo(pdfDocument); width = Convert.ToInt32(info.GetPageWidth(pdfDocument.Pages[pageCount].Number)); height = Convert.ToInt32(info.GetPageHeight(pdfDocument.Pages[pageCount].Number)); Aspose.Pdf.Devices.JpegDevice jpegDevice = //new Aspose.Pdf.Devices.JpegDevice(Aspose.Pdf.PageSize.A4, res, 100); new Aspose.Pdf.Devices.JpegDevice(width, height, res, 100); // Convert a particular page and save the image to stream //Aspose.Pdf.PageSize.A4.IsLandscape = true; jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream); // Close stream imageStream.Close(); } } } }

Convert byte array to PDF file using C#

Let's take it one step further and we can convert the byte array into a PDF file. Let us learn this with an example of converting an image as a byte array to a PDF file. You need to follow the steps below to convert byte array to PDF file.

  • Load input file
  • Initialize byte array
  • Load input image into byte array
  • Initialize an instance of the Document class
  • Add images on PDF pages
  • Save output PDF file

The following code illustrates how to programmatically convert a byte array to a PDF file using C#:

// Load input file
string inputFile = dataDir + @"Test.PNG";

// Initialize byte array
byte[] buff = null;
FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(inputFile).Length;

// Load input image into Byte Array
buff = br.ReadBytes((int)numBytes);


Document doc = new Document();
// Add a page to pages collection of document
Page page = doc.Pages.Add();
// Load the source image file to Stream object
MemoryStream outstream = new MemoryStream();
MemoryStream mystream = new MemoryStream(buff);
// Instantiate BitMap object with loaded image stream
Bitmap b = new Bitmap(mystream);

// Set margins so image will fit, etc.
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;

page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);
// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// Add the image into paragraphs collection of the section
page.Paragraphs.Add(image1);
// Set the image file stream
image1.ImageStream = mystream;

// Save resultant PDF file
doc.Save(outstream, SaveFormat.Pdf);
//doc.Save(dataDir + "outstream.pdf", SaveFormat.Pdf);

// Close memoryStream object
mystream.Close();

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/133159943