首页 技术 正文
技术 2022年11月15日
0 收藏 995 点赞 4,676 浏览 3095 个字

概述

  PDF是常用的文件格式之一,通常情况下,我们可以使用itextsharp生产PDF文件;可是如何将PDF文件转换成图片那?目前常用的:

  思路1、根据PDF绘画轨迹重新绘制图片;

  思路2、是将PDF文件解析成二进制,直接将二级制转换成图片;借助这2种思路,我在网上和同事的帮助下找到了2个DLL文件(第三方);

思路1:

  使用第三方DLL:O2S.Components.PDFRender4NET         DLL下载

  编写代码部分:

public enum Definition
{
One = , Two = , Three = , Four = , Five = , Six = , Seven = , Eight = , Nine = , Ten =
}
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, int endPageNum, ImageFormat imageFormat, Definition definition)
{
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// validate pageNum
if (startPageNum <= )
{
startPageNum = ;
}
if (endPageNum > pdfFile.PageCount)
{
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 - , * (int)definition);
pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);
pageImage.Dispose();
}
pdfFile.Dispose();
}
}

调用部分:

PDFTranImgHelp.ConvertPDF2Image("F:\\204834.pdf", "F:\\", "NImage", , , ImageFormat.Png, Definition.Five);

不足:

  如果预解析的原PDF文件中,含有png透明的图片,使用该方式解析失败!

思路二:

  使用的第三方类库是:Magick.NET-Q16-AnyCPU.dll    DLL下载(包括Lib下的文件)

  编写部分代码:  

public class PDFTranImg
{
public static byte[] ConvertPDF2Image(byte[] PDFbytes,string ImgPath)
{ try
{
//设置dll文件的目录
string DLLLibPath = AppDomain.CurrentDomain.BaseDirectory;
string dlllib = DLLLibPath + "lib";
MagickNET.SetGhostscriptDirectory(dlllib);
MagickReadSettings setting = new MagickReadSettings();
// Settings the density to 300 dpi will create an image with a better quality
setting.Density = new Density();
using (MagickImageCollection images = new MagickImageCollection())
{
// 读取二进制数组中的文件
images.Read(PDFbytes, setting);
using (MagickImage vertical = images.AppendVertically())
{
vertical.Write(ImgPath);
byte[] ReusltByte = File.ReadAllBytes(ImgPath);
return ReusltByte;
}
}
}
catch (Exception ex)
{
return null;
}
finally {
File.Delete(ImgPath);
} }
}

调用部分代码:

public FileContentResult EPDFCodePic(string InvoiceCodeNumber)
{ string[] InvoiceCodeNumber1 = PDFUnEncode(InvoiceCodeNumber);
string ResultPDF64 = LoadPDFImportTemplate(InvoiceCodeNumber1[], InvoiceCodeNumber1[], InvoiceCodeNumber1[], InvoiceCodeNumber1[]);
byte[] PDFBytes = Convert.FromBase64String(ResultPDF64);
string PDFTempFilePath = System.Web.HttpContext.Current.Server.MapPath("Temp");
string sPath = PDFTempFilePath + "\\" + DateTime.Now.ToString("yyyyMM");
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);
}
string SaveAsFileImagePath = sPath + "\\" + InvoiceCodeNumber1[]+InvoiceCodeNumber1[] + ".gif";
byte[] ResutlBytes= PDFTranImg.ConvertPDF2Image(PDFBytes,SaveAsFileImagePath);
return File(ResutlBytes, @"image/gif");
}

不足:

  使用该方式只能将PDF解析成gif格式的图片;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848