user5752271
user5752271

Reputation:

Reduce PDF size using iTetstSharp in .Net

I am converting number of Jpeg or Png image to PDF using iTextSharp dll. I can convert but the size of the PDF cause much worry. If I convert 9 jpeg images (total size is 4.5 MB) into single pdf , it creates 12.3 MB size of PDF. Below is conversion part.

private bool CreatePdf(string stFilePath_in, List<ImageData> lstImageData_in, string doctype, string stproCompid)
    {
        bool flag = false;
        StringBuilder builder = new StringBuilder();
        try
        {
            this.UtilityProgress(lstImageData_in.Count);
            builder.Append(stFilePath_in);
            builder.Append(@"\");
            builder.Append(lstImageData_in[0].Barcode);
            builder.Append(".pdf");
            Document document = new Document(PageSize.LETTER, 10f, 10f, 42f, 35f);
            PdfWriter.GetInstance(document, new FileStream(builder.ToString(), FileMode.OpenOrCreate));
            document.Open();
            IOrderedEnumerable<ImageData> enumerable = from files in lstImageData_in
                                                       orderby files.PageNo
                                                       select files;
            if (enumerable != null)
            {
                DbFileData data2;
                foreach (ImageData data in enumerable)
                {
                    Bitmap bitmap = new Bitmap(data.FilePath);
                    iTextSharp.text.Image instance = iTextSharp.text.Image.GetInstance(bitmap, ImageFormat.Png);
                    if (instance.Height > instance.Width)
                    {
                        float num = 0f;
                        num = 700f / instance.Height;
                        instance.ScalePercent(num * 100f);
                    }
                    else
                    {
                        float num2 = 0f;
                        num2 = 540f / instance.Width;
                        instance.ScalePercent(num2 * 100f);
                    }
                    instance.Border = 15;
                    instance.BorderColor = BaseColor.BLACK;
                    instance.BorderWidth = 3f;
                    document.Add(instance);
                    document.NewPage();
                    bitmap.Dispose();
                }
                document.Close();
                if (doctype == "AR")
                {
                    //data2.m_stInvoiceNo = lstImageData_in[0].Barcode.Substring(2);
                    data2.m_stInvoiceNo = lstImageData_in[0].Barcode.ToString();
                    data2.m_doctype = "AR";
                }
                else
                {
                    data2.m_stInvoiceNo = lstImageData_in[0].Barcode.ToString();
                    data2.m_doctype = "PO";
                }
                data2.m_stImgLocation = builder.ToString();
                string str = DateTime.Now.ToString("MM/dd/yy,hh:mm:ss");
                data2.m_dtDate = DateTime.Now.Date;
                data2.m_stTime = str.Substring(str.IndexOf(",") + 1);
                data2.m_stcompid = stproCompid;
                this.OnPdfFileCreationCompleted(data2);
                return true;
            }
            flag = false;
        }
        catch (Exception exception)
        {
            flag = false;
            StringBuilder builder2 = new StringBuilder();
            builder2.Append(builder.ToString());
            builder2.Append(": \t");
            builder2.Append(exception.Message);
            this.m_excepLogger.LogException(builder2.ToString());
        }
        return flag;
    }

Upvotes: 1

Views: 314

Answers (1)

mkl
mkl

Reputation: 95908

The OP creates the iTextSharp Image object like this:

Bitmap bitmap = new Bitmap(data.FilePath);
iTextSharp.text.Image instance = iTextSharp.text.Image.GetInstance(bitmap, ImageFormat.Png);

What this actually means is that the original image file is decoded into a bitmap, and then iTextSharp is asked to use the bitmap as if it was a PNG image.

In case of JPG images this usually means that the amount of data required to store the image explodes.

To prevent such size explosions one should allow iTextSharp to directly work with the original image file data, in the context at hand:

iTextSharp.text.Image instance = iTextSharp.text.Image.GetInstance(data.FilePath);

Upvotes: 2

Related Questions