好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#将PDF转为多种图像文件格式的方法(Png/Bmp/Emf/Tiff)

pdf是一种在我们日常工作学习中最常用到的文档格式之一,但常常也会因为文档的不易编辑的特点,在遇到需要编辑pdf文档内容或者转换文件格式的情况时让人苦恼。通常对于开发者而言,可选择通过使用组件的方式来实现pdf文档的编辑或者格式转换,因此本文将介绍如何通过使用免费版的组件free spire.pdf for .net来转换pdf文档。这里介绍将pdf转换多种不同格式的图像文件格式,如png,bmp,emf,tiff等,同时,转换文档也分为转换全部文档和转换部分文档为图片两种情况,本文也将作进一步介绍。下面是实现转换功能的详述,供参考。

提示:在下载安装该组件后,在项目中注意添加引用spire.pdf.dll文件,如下图:

一、转换整个pdf文档为图片

(一)pdf转png

?

using spire.pdf;

using system.drawing;

namespace pdftoimage1

{

   class program

   {

     static void main( string [] args)

     {

       //初始化一个pdfdocument类实例,并加载pdf文档

       pdfdocument doc = new pdfdocument();

       doc.loadfromfile( @"c:\users\administrator\desktop\sample.pdf" );

       //遍历pdf每一页

       for ( int i = 0; i < doc.pages.count; i++)

       {

         //将pdf页转换成bitmap图形

         system.drawing.image bmp = doc.saveasimage(i);

         //将bitmap图形保存为png格式的图片

         string filename = string .format( "page-{0}.png" , i + 1);

         bmp.save(filename, system.drawing.imaging.imageformat.png);

       }

     }

   }

}

调试运行程序,生成文档。

运行结果:

spire.pdf支持将pdf文档转换为多种图像格式的文件,可根据需要选择相应的文件格式,这里以png为例。

(二) pdf转tiff

?

using system;

using system.drawing;

using system.drawing.imaging;

using spire.pdf;

namespace savepdfastiff

{

   class program

   {

     static void main( string [] args)

     {

       //创建一个pdfdocument类对象,并加载pdf文档

       pdfdocument document = new pdfdocument();

       document.loadfromfile( @"c:\users\administrator\desktop\sample.pdf" );

       //调用方法saveasimage()将pdf文档保存为tiff格式

       jointiffimages(saveasimage(document), "result.tiff" , encodervalue测试数据pressionlzw);

       system.diagnostics.process.start( "result.tiff" );

     }

     //自定义方法saveasimage()将pdf文档保存图像文件

     private static image[] saveasimage(pdfdocument 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" );

     }

     //自定义jointiffimages()方法,使用指定编码器和图像编码器参数将图像从pdf页面保存到tiff图像类型,。

     public static void jointiffimages(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测试数据pression, ( 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++;

       }

     }

   }

}

运行结果:

二、 转换pdf指定页为图片( pdf转png、bmp、emf)

?

using spire.pdf;

using system.drawing;

using system.drawing.imaging;

namespace pdftoimage

{

   class program

   {

     static void main( string [] args)

     {

       //实例化一个pdfdocument类对象,并加载pdf文档

       pdfdocument doc = new pdfdocument();

       doc.loadfromfile( @"c:\users\administrator\desktop\sample.pdf" );

       //调用方法saveasimage()将pdf第二页保存为bmp格式

       image bmp = doc.saveasimage(1);

       //调用另一个saveasimage()方法,并将指定页面保存保存为emf、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);

       }

       //命名保存的文件并打开

       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" );

     }

   }

}

运行结果:

总结

以上所述是小编给大家介绍的c#将pdf转为多种图像文件格式的方法(png/bmp/emf/tiff),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://HdhCmsTestcnblogs测试数据/Yesi/archive/2018/02/06/8423008.html

dy("nrwz");

查看更多关于C#将PDF转为多种图像文件格式的方法(Png/Bmp/Emf/Tiff)的详细内容...

  阅读:50次