pdf⽂件转图⽚C#源码图⽚转pdf源码
public enum Definition
{
One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten =  10
}
/// <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 string[] PdfToPng(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
{
List<string> outFileList = new List<string>();
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// validate pageNum
if (startPageNum <= 0)
{
startPageNum = 1;
param name}
if (endPageNum > pdfFile.PageCount)
{
endPageNum = pdfFile.PageCount;
}
if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}
if (endPageNum == 1)
{
Bitmap pageImage = pdfFile.GetPageImage(1 - 1, 56 * (int)definition);
pageImage.Save(imageOutputPath + imageName + "." + imageFormat, imageFormat);
pageImage.Dispose();
outFileList.Add(imageOutputPath + imageName + "." + imageFormat);
}
else
{
for (int i = startPageNum; i <= endPageNum; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
pageImage.Save(imageOutputPath + imageName + i + "." + imageFormat, imageFormat);
pageImage.Dispose();
outFileList.Add(imageOutputPath + imageName + i + "." + imageFormat);
}
}
pdfFile.Dispose();
return outFileList.ToArray(); ;
}
void ConvertJPG2PDF(string jpgfile, string pdf, bool isRotation)
{
var document = PageSize.A4, 25, 25, 25, 25);
using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))            {
var image = Image.GetInstance(imageStream);
if (isRotation)
image.RotationDegrees = 90;
if (image.Height > PageSize.A4.Height - 25)
{
image.PageSize.A4.Width - 25, PageSize.A4.Height - 25);                }
else if (image.Width > PageSize.A4.Width - 25)
{
image.PageSize.A4.Width - 25, PageSize.A4.Height - 25);
                }
image.Alignment = Image.ALIGN_MIDDLE;
document.Add(image);
}
document.Close();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。