C # how to convert a PDF file into multiple image file formats (Png/Bmp/Emf/Tiff ),
PDF is one of the most common document formats in our daily work and study, but it is often difficult to edit documents, it is annoying to edit the content of a PDF document or convert the file format. Generally, developers can choose to edit PDF documents or convert their formats by using components. Therefore, this article describes how to use the Free version of the component Free Spire. PDF. NET to Convert PDF documents. This section describes how to convert PDF files into different formats, such as PNG, BMP, EMF, And TIFF, the conversion document can be divided into converting all documents and converting some documents into pictures. This article will further introduce them. The following describes how to implement the Conversion Function for your reference.
Tip:InDownloadAfter installing this component, add the reference Spire. PDF. dll file in the project, for example:
1. Convert the entire PDF file to an image
(1) Convert PDF to Png
Using Spire. pdf; using System. drawing; namespace extends toimage1 {class Program {static void Main (string [] args) {// Initialize an example document class and load the PDF document doc = new document (); doc. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ sample.pdf"); // traverses every page of a PDF file for (int I = 0; I <doc. pages. count; I ++) {// converts a PDF page to a Bitmap image System. drawing. image bmp = doc. saveAsImage (I); // Save the Bitmap image as a Png Image string fileName = string. format ("page-000002.16.png", I + 1); bmp. save (fileName, System. drawing. imaging. imageFormat. png );}}}}
Debug and run the program to generate documents.
Running result:
Spire. PDF can be used to convert a PDF file to a variety of image formats. You can select the corresponding file format as needed. Here, Png is used as an example.
(2) Converting PDF to TIFF
Using System; using System. drawing; using System. drawing. imaging; using Spire. pdf; namespace SavePdfAsTiff {class Program {static void Main (string [] args) {// create an upload document class object and load the PDF document into document = new upload document (); document. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ sample.pdf"); // call the SaveAsImage () method to save the PDF file as the join1_images (SaveAsImage (document) in tiff format ), "result. tiff ", EncoderValue. compr EssionLZW); System. diagnostics. process. start ("result. tiff ");} // custom method SaveAsImage () Save the PDF document to the Image file private static Image [] SaveAsImage (document) {Image [] images = new Image [document. pages. count]; for (int I = 0; I <document. pages. count; I ++) {images [I] = document. saveAsImage (I);} return images;} private static ImageCodecInfo GetEncoderInfo (string mimeType) {ImageCodecInfo [] encoders = ImageCodecInfo. getImageEncoders (); for (int j = 0; j <encoders. length; j ++) {if (encoders [j]. mimeType = mimeType) return encoders [j];} throw new Exception (mimeType + "mime type not found in ImageCodecInfo");} // custom join1_images () method, use the specified encoder and image encoder parameters to save the image from the pdf page to the tiff image type ,. Public static void join1_images (Image [] images, string outFile, EncoderValue compressEncoder) {Encoder enc = Encoder. saveFlag; EncoderParameters ep = new EncoderParameters (2); ep. param [0] = new EncoderParameter (enc, (long) EncoderValue. multiFrame); ep. param [1] = new EncoderParameter (Encoder. compression, (long) compressEncoder); Image pages = images [0]; int frame = 0; ImageCodecInfo info = GetEncoderInfo ("image/tiff"); foreach (Image img in images) {if (frame = 0) {pages = img; pages. save (outFile, info, ep);} else {ep. param [0] = new EncoderParameter (enc, (long) EncoderValue. frameDimensionPage); pages. saveAdd (img, ep);} if (frame = images. length-1) {ep. param [0] = new EncoderParameter (enc, (long) EncoderValue. flush); pages. saveAdd (ep) ;}frame ++ ;}}}}
Running result:
2. Convert the specified PDF page to an image (PDF to Png, Bmp, and Emf)
Using Spire. pdf; using System. drawing; using System. drawing. imaging; namespace extends toimage {class Program {static void Main (string [] args) {// instantiate an upload document class object and load the PDF document doc = new upload document (); doc. loadFromFile (@ "C: \ Users \ Administrator \ Desktop \ sample.pdf"); // call the SaveAsImage () method to save the second page of PDF as Bmp Format Image bmp = doc. saveAsImage (1); // call another SaveAsImage () method and save the specified page as Emf and Png Image emf = doc. saveAsImage (0, Spire. pdf. graphics. pdfImageType. metafile); Image zoomImg = new Bitmap (int) (emf. size. width * 2), (int) (emf. size. height * 2); using (Graphics g = Graphics. fromImage (zoomImg) {g. scaleTransform (2.0f, 2.0f); g. drawImage (emf, new Rectangle (new Point (0, 0), emf. size), new Rectangle (new Point (0, 0), emf. size), GraphicsUnit. pixel);} // name the saved file and open bmp. save ("converttobmp .bmp", ImageFormat. bmp); System. diagnostics. process. start ("converttobmp .bmp"); emf. save ("convertToEmf. emf ", ImageFormat. emf); System. diagnostics. process. start ("convertToEmf. emf "); zoomImg. save ("convertToZoom.png", ImageFormat. png); System. diagnostics. process. start ("convertToZoom.png ");}}}
Running result:
PS: