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

Free Support Forum - aspose.com

Image to Pdf increases PDF size using Aspose.Pdf.Document

Hi Team,


I am using Aspose.Pdf.Document library to convert an image to PDF. However the size of Pdf increases (almost gets doubled). Can you please share some code to compress the size of generated Pdf file.
Below is the code used :

public static byte[] ConvertImageToPdf(MemoryStream stream, string fileType)
{
var pdf = new Aspose.Pdf.Document();
var pdfImageSection = pdf.Pages.Add();
var image = new Aspose.Pdf.Image { ImageStream = stream };

var bmp = new Bitmap(stream);
pdfImageSection.PageInfo.Width = bmp.Width;
pdfImageSection.PageInfo.Height = bmp.Height;

pdfImageSection.Paragraphs.Add(image);
// remove extra white space in four corners by setting page margin information
pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 5f;

using (var memoryStream = new MemoryStream())
{
pdf.Save(HttpContext.Current.Server.MapPath("~") + “/pdf1.pdf”);
pdf.Optimize();
pdf.OptimizeSize = true;
pdf.Save(memoryStream);
return memoryStream.ToArray();

}
}

Quick response is appreciable.
Thanks!

Hi There,


Thanks for contacting support.

The reason of PDF file size increases is when you convert an Image into PDF, the Aspose.Pdf API does not compromise the quality of the image. However in order to compress or reduce the file size you need to change the image quality. Please check the following code snippet to optimize the PDF document with reducing the image quality inside it. In below code I have set the image quality reduced to 10%.

pdf.OptimizeResources(new Document.OptimizationOptions()<o:p></o:p>

{

LinkDuplcateStreams = true,

RemoveUnusedObjects = true,

RemoveUnusedStreams = true,

CompressImages = true,

ImageQuality = 10

});

pdf.Save(dataDir + "pdf1.pdf");


You may visit "Optimize PDF Document" section in our API documentation for more details. In case of any further assistance please feel free to contact us.


Best Regards,

Hi Asad,


Thanks for the quick response.

I have tried suggested approach but still image size is not getting reduced.

public static byte[] ConvertImageToPdf(MemoryStream stream, string fileType)
{
var pdf = new Aspose.Pdf.Document();
var pdfImageSection = pdf.Pages.Add();
var image = new Aspose.Pdf.Image {ImageStream = stream};

var bmp = new Bitmap(stream);
pdfImageSection.PageInfo.Width = bmp.Width;
pdfImageSection.PageInfo.Height = bmp.Height;

pdfImageSection.Paragraphs.Add(image);
// remove extra white space in four corners by setting page margin information
pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 5f;
using (var memoryStream = new MemoryStream())
{
pdf.OptimizeSize = true;
pdf.OptimizeResources(new Aspose.Pdf.Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = true,
ImageQuality = 10
});
pdf.Save(AppDomain.CurrentDomain.BaseDirectory + “\pdf.pdf”);
pdf.Save(memoryStream);
return memoryStream.ToArray();
}
}

I have attached the file used for conversion.

Please let me know if there is any other way to compress PDF.

Thanks!

Hi There,


Thanks for sharing input image and code snippet. In order to optimize/reduce the size of the PDF document after JPEG to PDF Conversion, you need to save the PDF document first and then you can optimize it after it has been saved. Please check the following snippet I have used to optimize the PDF. In the below code please check that I have saved the document before optimizing it. I am also attaching the generated output for your reference.

var pdf = new
Aspose.Pdf.
Document();<o:p></o:p>

var pdfImageSection = pdf.Pages.Add();

FileStream stream = new FileStream(dataDir + "snowinatlanta.jpg", FileMode.Open);

var image = new Aspose.Pdf.Image { ImageStream = stream };

var bmp = new System.Drawing.Bitmap(stream);

pdfImageSection.PageInfo.Width = bmp.Width;

pdfImageSection.PageInfo.Height = bmp.Height;

pdfImageSection.Paragraphs.Add(image);

// remove extra white space in four corners by setting page margin information

pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 5f;

pdf.Save(dataDir + "Image2Pdf_out.pdf");

pdf.OptimizeResources(new Document.OptimizationOptions()

{

LinkDuplcateStreams = true,

RemoveUnusedObjects = true,

RemoveUnusedStreams = true,

CompressImages = true,

ImageQuality = 10

});

pdf.Save(dataDir + "Image2Pdf_out_optimized.pdf");


In case if you need any further assistance please feel free to contact us.


Best Regards,

Hi,


I have tried out the above solution, saving pdf before optimization does the trick.
I am facing another problem that is saving image that have large width get squished on save.
Can you please let me know the solution of saving image as it is without any modification.

public static byte[] ConvertImageToPdfAspose(MemoryStream stream, string fileType)
{
var pdf = new Document();
var pdfImageSection = pdf.Pages.Add();
FileStream fileStream = new FileStream(HttpContext.Current.Server.MapPath("~") + “/Image.jpg”, FileMode.Open);
var image = new Image {ImageStream = fileStream };
pdfImageSection.Paragraphs.Add(image);
pdf.Save(HttpContext.Current.Server.MapPath("~") + “/pdf1.pdf”);
pdf.Save(new MemoryStream());
using (var memoryStream = new MemoryStream())
{
pdf.OptimizeResources(new Document.OptimizationOptions
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = true,
ImageQuality = 75
});
pdf.Save(memoryStream);
return memoryStream.ToArray();
}
}

Attached the image.

Hi Nitin,


Thanks for your feedback.

nitinjain4:
I am facing another problem that is saving image that have large width get squished on save.

The image got squished at your end because you were not setting height and width of the page according to those of the image to be added. Though, please check following code snippet, in order to set height and width of the page according to image. This way, image will not get squished.


var pdf = new
Aspose.Pdf.
Document();<o:p></o:p>

var pdfImageSection = pdf.Pages.Add();

FileStream stream = new FileStream(dataDir + "WideImage.png", FileMode.Open);

var image = new Aspose.Pdf.Image { ImageStream = stream };

var bmp = new System.Drawing.Bitmap(stream);

pdfImageSection.PageInfo.Width = bmp.Width;

pdfImageSection.PageInfo.Height = bmp.Height;

pdfImageSection.Paragraphs.Add(image);

// remove extra white space in four corners by setting page margin information

pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 5f;

pdf.Save(dataDir + "Image2Pdf_out.pdf");

pdf.OptimizeResources(new Document.OptimizationOptions()

{

LinkDuplcateStreams = true,

RemoveUnusedObjects = true,

RemoveUnusedStreams = true,

CompressImages = true,

ImageQuality = 10

});

pdf.Save(dataDir + "Image2Pdf_out_optimized.pdf");



I have also attached an output, generated by above code, for your reference. For more information, you may visit "Convert an Image to PDF" article in our API documentation. In case of any further assistance, please feel free to contact us.


Best Regards,