We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Load PDF from stream


I was wondering if it was possible to load an existing PDF document from a stream into an Aspose PDF object in order to change the compression settings of the PDF. Here is a code snippet of what I’m trying to accomplish:

var pdfStream = new MemoryStream( pdfFile.Contents );
var pdf = new Aspose.Pdf.Pdf( pdfStream );

Obviously I can not do it directly like this but I was wondering if there was any way to accomplish this using Aspose.PDF and/or Aspose.PDF,Kit.


Hi Robert,

You can’t use Aspose.Pdf to load an existing PDF file; in fact, Aspose.Pdf.Kit allows you to manipulate existing PDF files using various classes. However, can you please elaborate that which compression settings you’re interested to set for a PDF file? Kindly share your thoughts, so we would be able to help you out.

We’re sorry for the inconvenience.

Well it’s my understanding ( from searching these forums and your site ) that there are basically three options for compression settings. Text compression, picture quality, and the compression level property on the Aspose PDF object. I was just planning on attempting to use some combination of those to decrease the size of a database that contains many PDF’s.

Hi Robert,

I would like to share with you that there is only one way in Aspose.Pdf.Kit to compress existing PDF files; you can use CompressionLevel property to set the compression as shown in the following code snippet:

//set compression level

Settings.CompressionLevel = 3;

//open file using PdfFileStamp class

PdfFileStamp pdfStamp = new PdfFileStamp(“input.pdf”, “output.pdf”);

//close PDF file


You can use PdfFileStamp constructor to load the PDF file using a stream or file path. However, please note that there are certain limitations of CompressionLevel, so you need to read the details on this link.

Please try it at your end and see if it helps. If you find any other questions or concerns, please do let us know.

Thank you for your response. Although I ran into an issue when attempting to implement this.

I was using Pdf.Kit release 4.1 which did not have the Settings static object available so I updated to the latest release 4.6. When I added the reference to my website I got a Server 500 error when attempting to access the website. It did not matter if I was trying to access a page that was not using the newly added reference or not. After some testing I decided to try the Pdf.Kit 4.5 release and everything works perfectly again.

Do you have any insight on why this may be happening or is there any information that I can provide you to help solve this issue?

There were no exceptions and nothing appeared in my event log.

Hi Robert,

I have tested the version 4.6.0 in a web application, but I couldn’t notice any issue. Can you please try to remove any existing references and add a new reference to 4.6.0? If you still find the same issue then please share a small sample application which can reproduce the issue at our end. Also, please share the details regarding your working environment i.e. OS and .NET Framework etc.

We’re sorry for the inconvenience.

I’m using Windows 7 64bit, VS2010 and .NET 4 on my development machine. I was using your .NET 3.5 dll because our server does not have .NET 4 on it yet.

I definitely cleared the old reference and replaced it with the new one just fine.

I have tested the code snippet you provided and unfortunately no matter what compression level I use the PDF documents are getting larger. I believe this is due to the way the PDF documents are being created by the end user. The PDF’s are basically just scanned images so I am wondering if I would get better results by extracting the images (more than likely a single image that is the entire PDF ) and compressing the images manually?

Please let me know what your thoughts are on this subject and if your software supports something like this.


Hi Robert,

I have tested the .NET Framework 3.5 version of the Aspose.Pdf.Kit for .NET release 4.6.0 in a web application and faced the following error during build process:

"Could not load file or assembly ‘Aspose.Pdf.Kit, Version=, Culture=neutral, PublicKeyToken=ae9e610db67fc2c1’ or one of its dependencies. The system cannot find the file specified."

Whereas, I can’t see any such error with DLLs for 2.0 or 4.0. Did you receive the same error on building the application as well? Please share the situation with us, so we could be on the same page before moving forward.

As far as the compression of the PDF file is concerned, you might have noticed from the description of the CompressionLevel property that compression for binary data only works fine up to the level 3 or 4. Also, the compression results can be different depending upon the type of the data and structure of the PDF file. If you could share a sample PDF file with us, our team would be able to investigate and find out if it can be further compressed or not.

We’re sorry for the inconvenience.

I did not receive a build error but I will try again to be sure.

I did read the description of the compression level setting and I tried every setting 1-9 to no avail. The PDF’s I am working with are a single scanned in image with a resolution of 1690x2200. I ended up just extracting the image from the PDF using your software, scaling down the image and then I will just re-create the PDF also using your software.

I’m having an issue with my program hanging up on pdf.Save(file path) but I’m sure it’s something on my end I’m missing.

I will re-test the 4.6 dll and get back to you though.

Unfortunately I can not share a PDF with you because the content is specific to our customer but I can not get the PDF to compress to anything smaller than the size of the image when I am re-creating the PDF using your software. This must be possible though because I am starting with a PDF that is roughly ~28kb and extracting an image that is ~60kb. The size of the image is dependent on the file type you extract it as.

You can re-create this by taking any image and create a PDF containing that single image using your software. At best it will be the same size as the image.

This is the code I’m using to create the PDF:

//Instantiate a Pdf object by calling its empty constructor
var pdf1 = new Pdf();
pdf1.CompressionLevel = 3;
//Create a section in the Pdf object
Section sec1 = pdf1.Sections.Add();
//Create an image object in the section
var image1 = new Aspose.Pdf.Image( sec1 );
//Add image object into the Paragraphs collection of the section
sec1.Paragraphs.Add( image1 );
//Set the image stream
image1.ImageInfo.ImageStream = imageStream;
//Set the type of image using ImageFileType enumeration
image1.ImageInfo.ImageFileType = ImageFileType.Gif;
//Save the Pdf
pdf1.Save( @“C:\test.pdf” );

Hi Robert,

We’ll look into this issue at our end and you’ll be updated with the results accordingly.

We’re sorry for the inconvenience.

Hi Robert,

I would really appreciate if you could clarify one thing. Do you want to compress the PDF file while creating it from scratch or you want to compress an existing PDF file?


Really I would like either option to work. When I could not compress the original PDF I decided to try re-creating it to compress it.

I believe the issue in both cases is the main content of the PDF is a scanned image. Which is why I figured I could compress the image and re-create the PDF.

Hi Robert,

I have logged this issue as PDFKITNET-18494 in our issue tracking system. Our team will further investigate this issue and you’ll be updated via this forum thread once it is resolved.

We’re sorry for the inconvenience.

Alright, thank you and I look forward to hearing back from you.

Hi Robert,

Our team has looked into this issue and I would like to share with you that the Aspose.Pdf.Kit can’t compress the images; as the sample file contains only the image that’s why it is not being compressed any further. However, if you want to compress the file containing images, you need to extract the images, compress those images using some image compression tool and then insert the images back to the PDF file. Moreover, as Aspose.Pdf.Kit doesn’t support image compression at the moment, it might take some time to investigate this feature and I’m afraid, it can’t be supported in short time.

We’re sorry for the inconvenience.