/// <summary>
/// 将pdf文档转换为图片的方法(该方法基于第三方DLL),你可以像这样调用该方法:
/// ConvertPDF2Image("F:\\PdfFile.doc", "F:\\", "ImageFile", 1, 20, ImageFormat.Png, 256);
/// </summary>
/// <param name="pdfInputPath">Word文件路径</param>
/// <param name="imageOutputPath">图片输出路径,如果为空,默认值为Word所在路径</param>
/// <param name="imageName">图片的名字,不需要带扩展名,如果为空,默认值为Word的名称</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数</param>
/// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param>
/// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
public static void ConvertPdfToImage(string wordInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, int resolution)
{
try
{
// open word file
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(wordInputPath);
// validate parameter
if (doc == null) { throw new Exception("Pdf文件无效或者Pdf文件被加密!"); }
if (imageOutputPath.Trim().Length == 0) { imageOutputPath = Path.GetDirectoryName(wordInputPath); }
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
if (imageName.Trim().Length == 0) { imageName = Path.GetFileNameWithoutExtension(wordInputPath); }
if (startPageNum <= 0) { startPageNum = 1; }
if (endPageNum > doc.Pages.Count || endPageNum <= 0) { endPageNum = doc.Pages.Count; }
if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
if (imageFormat == null) { imageFormat = ImageFormat.Png; }
if (resolution <= 0) { resolution = 128; }
//PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//pdfSaveOptions.SaveFormat= GetSaveFormat(imageFormat);
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(GetSaveFormat(imageFormat));
imageSaveOptions.Resolution = resolution;
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
imageSaveOptions.PageIndex = i - 1;
MemoryStream stream = new MemoryStream();
Aspose.Pdf.SaveOptions pdfSaveOptions = new Aspose.Pdf.PdfSaveOptions();
//Aspose.Pdf.Devices.Resolution reso = new Aspose.Pdf.Devices.Resolution(resolution);
Aspose.Pdf.Devices.PngDevice pngDevice = new Aspose.Pdf.Devices.PngDevice(990, 1400);
string strNumber;
if (i < 10)
{
strNumber = "0" + i.ToString();
}
else
{
strNumber = i.ToString();
}
pngDevice.Process(doc.Pages[i], Path.Combine(imageOutputPath, imageName) + "_" + strNumber + "." + imageFormat.ToString());
stream.Dispose();
//doc.Save(Path.Combine(imageOutputPath, imageName) + "_" + strNumber + "." + imageFormat.ToString(), pdfSaveOptions);
}
}
catch (Exception ex)
{
throw ex;
}
}
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(wordInputPath);调试的时候,发现刚进来到这里,doc.page.count是pdf的页数没毛病,但是doc.pages就只有4页。想不通。。其他代码都还没执行,第一行就这样,这是因为我的aspose是免费版的缘故吗?如果不是那可能是什么原因呢?刚刚接触Aspose和.net,所以来问问,大神help~~~
https://zhidao.baidu.com/question/498851954668960444.html
得到PDF的路径之后,根据PDF转换为图片时只能转换4页的内容
貌似是aspose.pdf.dll的问题,因为我的是在官网下的,然后我又下了个破解版的,就没有只显示4页的问题了
这是aspose.pdf.dll问题,有许可或者是破解版就不会出现这样的情况了
解决了吗?