PDF轉圖片流並jquery顯示到頁面
首先在显示pdf文件,则需先学习pdf.js的运用,先推荐http://blog.csdn.net/xiangcns/article/details/42089189博客,我这边的重点是怎样将pdf.js转换成图片流。为啥不直接转图片,大概是因为在服务器中不允许存图片,只能将pdf路径转图片流传输。直接上demo代码:我这边是引用O2S.ComponentModel.DataAnnotations.dll(下载地址http://pan.baidu.com/s/1nUecQ,密码hep9)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using O2S.Components.PDFRender4NET;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
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 static List<byte[]> ConvertPDF2Image(string pdfInputPath, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
{
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
// validate pageNum
if (startPageNum <= 0)
{
startPageNum = 1;
}
if (endPageNum > pdfFile.PageCount)
{
endPageNum = pdfFile.PageCount;
}
if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}
// 创建byte数组存放流数组
List<byte[]> byteList = new List<byte[]>();
//循环整个pdf文件去将pdf转换成图片流
for (int i = startPageNum; i <= endPageNum; i++)
{
//pdf生成的Bitmap转换图片流
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
MemoryStream ms = new MemoryStream();
pageImage.Save(ms, imageFormat);
pageImage.Dispose();
byte[] byteImg = ms.ToArray();
byteList.Add(byteImg);
}
pdfFile.Dispose();
return byteList;
}
//以上就是将pdf转图片流,并没有将图片存在本地,以下只是扩展
//将图片以二进制流
public byte[] SaveImage(String path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
return imgBytesIn;
}
//现实二进制流代表的图片
public void ShowImgByByte(byte[] imgBytesIn)
{
Response.ContentType = "image/jpg";//没有这个会出现乱码
Response.BinaryWrite(imgBytesIn);
}
}
}
有了图片流怎样显示到页面了,这里直接使用html图片控件,直接上代码
///
/// pdf路径转图片流
///
///
public string PdfPathToImage(string path)
{
//得到图片流数组
IList<byte[]> imgBytesIn = PdfToImgHelper.ConvertPDF2Image(path, 1, 100, ImageFormat.Jpeg, PdfToImgHelper.Definition.Three);
//拼json数据
StringBuilder strSb = new StringBuilder();
strSb.Append("[");
Int32 _index = 1;
//遍历将图片流转换成Base64
foreach (byte[] s in imgBytesIn)
{
if (imgBytesIn.Count == _index)
{
strSb.Append("{");
strSb.Append(""value":"" + Convert.ToBase64String(s) + """);
strSb.Append("}");
}
else
{
strSb.Append("{");
strSb.Append(""value":"" + Convert.ToBase64String(s) + """);
strSb.Append("},");
}
_index++;
}
strSb.Append("]");
String strValue = strSb.ToString();
return JsonConvert.SerializeObject(strValue);
}
//pdf路径转图片公共方法
function PdfPathToImage(path) {
$.ajax({
type: "post",
url: "/Evaluation/PdfPathToImage",
data: { "path": path },
async: true,
dataType:'json',
success: function (data) {
//得到base64jsons数据后转成对象
var p = eval(data);
//如果对象不为空则循环拼接html图片控件
if (p) {
var _html = "";
for (var item in p) {
_html += '';
}
$("#masterslider").append(_html);
}
}
});
}
继续学习,有哪里需要优化的地方,还请提出,逆水行舟不进则退!
轉載https://www.cnblogs.com/Sunnykeke/p/6654797.html