C# 使用C压缩PDF文件#

C# 使用C压缩PDF文件#,c#,pdf,reduce,C#,Pdf,Reduce,我有一个在代码级别减少PDF文件大小的请求,如果你有一个解决方案,请与我分享 但是图像大小应该是最小的 谢谢。下面的代码示例演示如何通过降低/压缩文档中图像的质量来压缩PDF文档。你可以试试看 //Loads the PDF document PdfDocument doc = new PdfDocument("Image.pdf"); //Disables the incremental update doc.FileInfo.IncrementalUpdate = false; //Tra

我有一个在代码级别减少PDF文件大小的请求,如果你有一个解决方案,请与我分享

但是图像大小应该是最小的


谢谢。

下面的代码示例演示如何通过降低/压缩文档中图像的质量来压缩PDF文档。你可以试试看

//Loads the PDF document
PdfDocument doc = new PdfDocument("Image.pdf");
//Disables the incremental update
doc.FileInfo.IncrementalUpdate = false;

//Traverses all pages
foreach (PdfPageBase page in doc.Pages)
{
    //Extracts images from page
    Image[] images = page.ExtractImages();
    if (images != null && images.Length > 0)
    {
        //Traverses all images
        for (int j = 0; j < images.Length; j++)
        {
            Image image = images[j];
            PdfBitmap bp = new PdfBitmap(image);
            //Reduces the quality of the image
            bp.Quality = 20;
            //Replaces the old image in the document with the compressed image
            page.ReplaceImage(j, bp);
        }
    }                
}
//Saves and closes the resultant document
doc.SaveToFile("Output.pdf");
doc.Close();
//加载PDF文档
PdfDocument doc=新的PdfDocument(“Image.pdf”);
//禁用增量更新
doc.FileInfo.IncrementalUpdate=false;
//遍历所有页面
foreach(文档页中的PdfPageBase页)
{
//从页面中提取图像
Image[]images=page.ExtractImages();
if(images!=null&&images.Length>0)
{
//遍历所有图像
对于(int j=0;j

注意:这个例子由Spire.PDF提供,我是Spire的员工。您可以查看文章以了解详细信息。

您尝试了什么,在哪里遇到了问题?我想比较一下PDF文件的大小。像转换图像文件一样。