使用.Net中的iTetstSharp减小PDF大小

时间:2017-02-15 11:08:38

标签: c# .net pdf itext

我正在使用iTextSharp dll将Jpeg或Png图像的数量转换为PDF。我可以转换,但PDF的大小引起很多担心。如果我将9个jpeg图像(总大小为4.5 MB)转换为单个pdf,则会创建12.3 MB大小的PDF。以下是转换部分。

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;
    }

1 个答案:

答案 0 :(得分:2)

OP会像这样创建iTextSharp Image对象:

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

这实际意味着原始图像文件被解码为位图,然后要求iTextSharp使用位图,就像它是PNG图像一样。

如果是JPG图像,这通常意味着存储图像所需的数据量会爆炸。

为了防止这种尺寸爆炸,应该允许iTextSharp直接使用原始图像文件数据,在手头的上下文中:

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