Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created January 18, 2021 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/82f890d8ed5fbc4801695381a19dfc4d to your computer and use it in GitHub Desktop.
Save aspose-com-gists/82f890d8ed5fbc4801695381a19dfc4d to your computer and use it in GitHub Desktop.
Convert PDF File to Byte Array or Byte Array to PDF 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();
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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment