c# pdf 转图片,转换后 图片保存在 同目录下的 picture 文件夹了
public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
{
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// 开始的页
if (startPageNum <= 0)
{
startPageNum = 1;
}
if (endPageNum > pdfFile.PageCount)
{
endPageNum = pdfFile.PageCount;
}
if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}
// 转成图片,并保存
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();
}
private void button1_Click(object sender, EventArgs e)
{
// 打开文件
OpenFileDialog addFileDialog = new OpenFileDialog();
addFileDialog.Filter = "pdf|*.pdf";
//
if (addFileDialog.ShowDialog() == DialogResult.OK)
{
if (addFileDialog.FileName != null)
{
//得到地址信息
address_pdf = addFileDialog.FileName;
}
else
{
;
}
}
else
{
;
}
}
private void button2_Click(object sender, EventArgs e)
{
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase "\\picture\\";
ConvertPDF2Image(address_pdf, str, "A", 1, 1000, ImageFormat.Jpeg, Definition.Four);
MessageBox.Show("PDF转JPEG完成!");
}
评论