2016-05-16 151 views
0

我已將兩個PDF加在一起,每個都是100kb,但將它們放在一起後,PDF大小已超過500kb。我怎樣才能壓縮PDF的大小?在C#中壓縮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; 
} 
+0

標識假設有在課堂上的壓縮屬性。您需要仔細觀察並查看。 – CathalMF

+2

據我所知,您發佈的代碼是用於創建一個全新的空白文檔,從另一個PDF導入第一頁,然後導入一堆圖像。但是,這與您的問題描述不匹配。 –

+0

正如@CathalMF所說,你可以嘗試使用'writer.SetFullCompression()'和'writer.CompressionLevel = PdfStream.BEST_COMPRESSION'來改進壓縮。正如@Chris所說,你的代碼似乎與你的問題無關。 – mkl

回答

1

您可以使用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

+0

問題是關於iTextSharp,而不是Syncfusion。 – mkl