C # compressing PDF images,

Source: Internet
Author: User

C # compressing PDF images,

If the document contains images, the entire document will be relatively large, occupying storage space and making it difficult to transmit files quickly and efficiently. For some PDF documents that contain a large number of high-quality images, compression can effectively reduce the space occupied by the documents. In addition, the transfer time can also be reduced during file transmission to improve efficiency. This article introduces two methods for compressing PDF images and documents through C. To use this method, you must use the latest version of Spire. PDF for. NET3.9.462. The following describes how to compress an image using C # code to compress the entire PDF file.

Tip:Before editing the code, install the component, add the referenced dll file to the project, and add commands.

Method 1

1 using Spire. pdf; 2 using System. drawing; 3 using Spire. pdf. graphics; 4 using Spire. pdf. exporting; 5 6 namespace CompressImage_PDF 7 {8 class Program 9 {10 static void Main (string [] args) 11 {12 // initialize a document class instance and load a pdf Document 13 documents doc = new document (@ "C: \ Users \ Administrator \ Desktop \ Input.pdf "); 14 // disable incremental update 15 doc. fileInfo. incrementalUpdate = false; 16 // traverses all PDF pages and diagnoses whether the page contains 17 foreach (Pdf PageBase page in doc. Pages) 18 {19 if (page! = Null) 20 {21 if (page. ImagesInfo! = Null) 22 {23 foreach (invalid imageinfo info in page. imagesInfo) 24 {25 // call the TryCompressImage () method to compress the image 26 page. tryCompressImage (info. index); 27} 28} 29} 30} 31 // Save the document 32 doc. saveToFile ("outputpipeline"); 33} 34} 35}

 

Method 2

1 using Spire. pdf; 2 using System. drawing; 3 using Spire. pdf. graphics; 4 5 namespace CompressImage_PDF 6 {7 class Program 8 {9 static void Main (string [] args) 10 {11 12 // initialize a document class instance and load a pdf Document 13 document doc = new document (@ "C: \ Users \ Administrator \ Desktop \ Input.pdf "); 14 15 // disable incremental update 16 doc. fileInfo. incrementalUpdate = false; 17 18 // traverse all PDF pages and extract picture 19 foreach (PdfPageBase page in doc. pages) 20 {21 Image [] images = page. ExtractImages (); 22 // traverse all images 23 if (images! = Null & images. length> 0) 24 {25 for (int j = 0; j <images. length; j ++) 26 {27 Image image = images [j]; 28 PdfBitmap bp = new PdfBitmap (image); 29 // set bp. quality value, which compresses the image by 30 bp. quality = 20; 31 // Replace the compressed image with the original 32 page. replaceImage (j, bp); 33} 34} 35} 36 // save document 37 doc. saveToFile ("Output2.pdf"); 38 39} 40} 41}

For PDF documents containing a large number of high-quality images, the above methods can effectively compress documents, and there will be no advertisement watermarks or damages to the source documents when the compression software on the market compresses documents. The method is introduced here. If it is useful to you, you are welcome to repost it (please indicate the source for reprinting ).

(This article is complete)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.