同樣的需要第三方的.dll,http://www.o2sol.com/pdfview4net/download.htm
using O2S.Components.PDFRender4NET;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace pdfConvert
{
public class Program
{
public enum ImageMergeOrientation
{
Horizontal,
Vertical
}
public static void Main(string[] args)
{ // pdf轉圖片
//PDFTranImgHelp.ConvertPDF2Image(@"D:\Csharpstudy\pdfTojpg\test.pdf", @"D:\Csharpstudy\pdfTojpg\", "test",1, ImageFormat.Png, Definition.Five);
//合並圖片
const string folderPath = @"D:\Csharpstudy\pdfTojpg";
var images = new DirectoryInfo(folderPath).GetFiles("*.png", SearchOption.TopDirectoryOnly);
CombineImages(images, @"D:\Csharpstudy\pdfTojpg\FinalImage_H.png");
CombineImages(images, @"D:\Csharpstudy\pdfTojpg\FinalImage_V.png", ImageMergeOrientation.Vertical);
}
public static void CombineImages(FileInfo[] files, string toPath, ImageMergeOrientation mergeType = ImageMergeOrientation.Vertical)
{
//change the location to store the final image.
var finalImage = toPath;
var imgs = files.Select(f => Image.FromFile(f.FullName));
var finalWidth = mergeType == ImageMergeOrientation.Horizontal ?
imgs.Sum(img => img.Width) :
imgs.Max(img => img.Width);
var finalHeight = mergeType == ImageMergeOrientation.Vertical ?
imgs.Sum(img => img.Height) :
imgs.Max(img => img.Height);
var finalImg = new Bitmap(finalWidth, finalHeight);
Graphics g = Graphics.FromImage(finalImg);
g.Clear(SystemColors.AppWorkspace);
var width = finalWidth;
var height = finalHeight;
var nIndex = 0;
foreach (FileInfo file in files)
{
Image img = Image.FromFile(file.FullName);
if (nIndex == 0)
{
g.DrawImage(img, new Point(0, 0));
nIndex++;
width = img.Width;
height = img.Height;
}
else
{
switch (mergeType)
{
case ImageMergeOrientation.Horizontal:
g.DrawImage(img, new Point(width, 0));
width += img.Width;
break;
case ImageMergeOrientation.Vertical:
g.DrawImage(img, new Point(0, height));
height += img.Height;
break;
default:
throw new ArgumentOutOfRangeException("mergeType");
}
}
img.Dispose();
}
g.Dispose();
finalImg.Save(finalImage,ImageFormat.Png);
finalImg.Dispose();
}
}
public enum Definition
{
One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
}
public class PDFTranImgHelp
{
/// <summary>
/// 將PDF文檔轉換為圖片的方法
/// </summary>
/// <param name="pdfInputPath">PDF文件路徑</param>
/// <param name="imageOutputPath">圖片輸出路徑</param>
/// <param name="imageName">生成圖片的名字</param>
/// <param name="startPageNum">從PDF文檔的第幾頁開始轉換</param>
/// <param name="endPageNum">從PDF文檔的第幾頁開始停止轉換</param>
/// <param name="imageFormat">設置所需圖片格式</param>
/// <param name="definition">設置圖片的清晰度,數字越大越清晰</param>
public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, ImageFormat imageFormat, Definition definition)
{
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// validate pageNum
if (startPageNum <= 0)
{
startPageNum = 1;
}
var endPageNum = pdfFile.PageCount;
if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);
pageImage.Dispose();
}
pdfFile.Dispose();
}
#region 合並圖片
#endregion
}
}