Visual Studio 在C#中压缩PDF

epfja78i  于 2023-03-09  发布在  C#
关注(0)|答案(1)|浏览(258)

我已经加入了两个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;
}
tf7tbtn2

tf7tbtn21#

你可以使用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文档:
压缩现有PDF文档

相关问题