用C#压缩PDF

时间:2016-05-16 15:30:31

标签: c# visual-studio itextsharp

我加入了两个PDF,每个都是100kb,但在将它们放在一起后,PDF大小已超过500kb。如何压缩PDF的大小?

public static string PDFFromLabelData(LabelData label) 
{
    string base64String = "";
    try
    {
        using (var ms = new MemoryStream())
        {
            iTextSharp.text.Document document = new iTextSharp.text.Document();
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms);

            if (label.base64PDFString.Length > 0)
            {
                document.Open();
                iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
                iTextSharp.text.pdf.PdfImportedPage page;

                iTextSharp.text.pdf.PdfReader reader;
                byte[] byteArray = Convert.FromBase64String(label.base64PDFString);

                try
                {
                    reader = new iTextSharp.text.pdf.PdfReader(byteArray);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                int pages = reader.NumberOfPages;

                // loop over document pages
                document.SetPageSize(iTextSharp.text.PageSize.A4);
                document.NewPage();

                page = writer.GetImportedPage(reader, 1);
                cb.AddTemplate(page, 0, 0);
            }

            foreach (Bitmap bmp in label.LabelImages)
            {
                iTextSharp.text.Rectangle pageSize = null;
                pageSize = new iTextSharp.text.Rectangle(0, 0, (bmp.Width / bmp.VerticalResolution) * 71, (bmp.Height / bmp.VerticalResolution) * 71);
                document.SetPageSize(pageSize);
                document.SetMargins(2, 2, 4, 4);
                document.NewPage();

                if (!document.IsOpen())
                {
                    document.Open();
                }

                using (var mm = new MemoryStream())
                {
                    bmp.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);

                    iTextSharp.text.Jpeg jpg = new iTextSharp.text.Jpeg(mm.ToArray());

                    jpg.ScaleToFit(pageSize);
                    document.Add(jpg);
                }
            }

            document.Close();
            base64String = Convert.ToBase64String(ms.ToArray());
        }
    }
    catch (Exception e)
    {
        throw e;
    }

    return base64String;
}

1 个答案:

答案 0 :(得分:2)

您可以使用Syncfusion Essential PDF压缩现有PDF文档。在这里,我附上了代码段和示例供您参考,请尝试这一点并告诉我们结果。

//Load a existing PDF document
PdfLoadedDocument ldoc = new PdfLoadedDocument(inputFile);

//Create a new PDF compression options
PdfCompressionOptions options = new PdfCompressionOptions();

//Compress image.
options.CompressImages = true;

//Set the image quality.
options.ImageQuality = 50;

//Compress the font data
options.OptimizeFont = true;

//Compress the page contents
options.OptimizePageContents = true;

//Remove the metadata information.
options.RemoveMetadata = true;

//Set the options to loaded PDF document
ldoc.CompressionOptions = options;

//Save the document
ldoc.Save("Output.pdf");

//Close the document
ldoc.Close(true);

示例链接:Compress Existing PDF document

请参阅以下UG文档以获取更多详细信息:

Compress existing PDF documentation