开发语言 c# 基于低版本 .net framework ,base64转 pdf打印解决方案 - writer
本文介绍,因种种原因无法通过itextsharp插件生成有效的pdf文件。从而选取以下方式
通过base64转pdf打印
1.通过base64转pdf文件
2.再根据pdf文件转为png格式
3.再由winform PictureBox控件显示 拼多多帮助力入口
4.(公司通用打印方法)打印方法暂时不展示。
private Listmap = new List();
private void CreatePdfPath(){
DateTime now = DateTime.Now; // 获取当前日期和时间
//时间戳方式定义pdf文件名 long timestamp = (long)(now - new DateTime(1970, 1, 1)).TotalSeconds;
byte[] by = Convert.FromBase64String(base64);
//生成pdf路径 string locationpdf = Application.StartupPath + "\\" + timestamp + ".pdf"; System.IO.FileStream stream = new System.IO.FileStream(locationpdf, System.IO.FileMode.CreateNew);
//通过流的方式写入 System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream); writer.Write(by, 0, by.Length); writer.Close();
//调用pdf转png方法
ConvertPDFImage(locationpdf, 1, 1, ImageFormat.Png, Definition.Five, ref map);
File.Delete(locationpdf); 最后删除生成的pdf
}
///
/// 将PDF文档转换为图片的方法 /// ///PDF文件路径 ///从PDF文档的第几页开始转换 ///从PDF文档的第几页开始停止转换 ///设置所需图片格式 ///设置图片的清晰度,数字越大越清晰 public static void ConvertPDF2Image(string pdfInputPath, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition, ref Listmap) { map = new List();//初始化集合 PDFFile pdfFile = PDFFile.Open(pdfInputPath); if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfFile.PageCount) { endPageNum = pdfFile.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } for (int i = startPageNum; i <= endPageNum; i++) { Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); map.Add(pageImage); } pdfFile.Dispose(); }
好文链接
评论可见,查看隐藏内容