首页 新闻 会员 周边 捐助

运用 Aspose将pdf转成图片时,图片内容空白或者有的字变成方框了

0
悬赏园豆:200 [已关闭问题] 关闭于 2017-02-27 16:53

/// <summary>
/// 将pdf文档转换为图片的方法
/// </summary>
/// <param name="originFilePath">pdf文件路径</param>
/// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为pdf所在路径</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为pdf总页数</param>
/// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
private void ConvertToImage(string originFilePath, string imageOutputDirPath, int startPageNum, int endPageNum, int resolution)
{
try
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(originFilePath);

if (doc == null)
{
throw new Exception("pdf文件无效或者pdf文件被加密!");
}

if (imageOutputDirPath.Trim().Length == 0)
{
imageOutputDirPath = Path.GetDirectoryName(originFilePath);
}

if (!Directory.Exists(imageOutputDirPath))
{
Directory.CreateDirectory(imageOutputDirPath);
}

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 (resolution <= 0)
{
resolution = 128;
}

string imageNamePrefix = Path.GetFileNameWithoutExtension(originFilePath);
for (int i = startPageNum; i <= endPageNum; i++)
{
MemoryStream stream = new MemoryStream();
string imgPath = Path.Combine(imageOutputDirPath, imageNamePrefix) + "_" + i.ToString("000") + ".jpg";
Aspose.Pdf.Devices.Resolution reso = new Aspose.Pdf.Devices.Resolution(resolution);
Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(reso, 100);
jpegDevice.Process(doc.Pages[i], stream);

Image img = Image.FromStream(stream);
Bitmap bm = ESBasic.Helpers.ImageHelper.Zoom(img, 0.6f);
bm.Save(imgPath, ImageFormat.Jpeg);
img.Dispose();
stream.Dispose();
bm.Dispose();

System.Threading.Thread.Sleep(200);
}

}
catch (Exception ex)
{
throw ex;
}
}
}

Original_Y的主页 Original_Y | 菜鸟二级 | 园豆:202
提问于:2017-02-27 16:39
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册