pdf是一種在我們日常工作學習中最常用到的文檔格式之一,但常常也會因為文檔的不易編輯的特點,在遇到需要編輯pdf文檔內容或者轉換文件格式的情況時讓人苦惱。通常對于開發者而言,可選擇通過使用組件的方式來實現pdf文檔的編輯或者格式轉換,因此本文將介紹如何通過使用免費版的組件free spire.pdf for .net來轉換pdf文檔。這里介紹將pdf轉換多種不同格式的圖像文件格式,如png,bmp,emf,tiff等,同時,轉換文檔也分為轉換全部文檔和轉換部分文檔為圖片兩種情況,本文也將作進一步介紹。下面是實現轉換功能的詳述,供參考。
提示:在下載安裝該組件后,在項目中注意添加引用spire.pdf.dll文件,如下圖:
一、轉換整個pdf文檔為圖片
(一)pdf轉png
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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.compressionlzw); 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.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++; } } } } |
運行結果:
二、 轉換pdf指定頁為圖片( pdf轉png、bmp、emf)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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://www.cnblogs.com/Yesi/archive/2018/02/06/8423008.html