简介

功能:PDF文档按每页转换成一张图片,一张图片转换成一张PDF 并将多张PDF合成一个多页的PDF文档。

经历:在各个网站上搜索始终出现各种问题,尤其是遇到引用的版本问题尤其头疼,不是不能适用当前的方法就是出现水印标签,最终在各位大佬的帮助下终于完成一个相对完整的代码(主要的是能满足需求)。

背景

之前在项目中遇到一个需求:将多页的PDF上传并转成图片展示要求一页一页的排列开看(例如图1),并要求在传送到服务中心平台看到的时候是PDF文档(服务中心平台只提供下载)。

点击下载需要使用到的dll文件

O2S.Components.PDFRender4NET.dll 版本:4.7.4.0

itextsharp.dll 版本:5.5.10.0

主要的代码如下

        /// <summary>
/// 将PDF文档转换为图片的方法
/// </summary>
/// <param name="pdfInputPath"></param>
/// <param name="desPath">输出相对路径</param>
/// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
/// <returns></returns>
public static List<string> ConvertPDF2Image(string pdfInputPath, string desPath, Definition definition, string title)
{
List<string> imgList = new List<string>();
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
int startPageNum = , endPageNum = pdfFile.PageCount;
int number = ;
for (int i = startPageNum; i <= endPageNum; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - , * (int)definition);
string filePath = desPath + title + "-" + number + ".jpg";
imgList.Add(filePath);
pageImage.Save(System.Web.HttpContext.Current.Server.MapPath(filePath));
number++;
}
pdfFile.Dispose();
return imgList;
} public enum Definition
{
One = , Two = , Three = , Four = , Five = , Six = , Seven = , Eight = , Nine = , Ten =
} /// <summary>
/// 合并PDF
/// </summary>
/// <param name="fileList">绝对路径集合</param>
/// <param name="outMergeFile">合并后的文件存在地址绝对路径</param>
public static void mergePdfFiles(List<string> fileList, string outMergeFile)
{
PdfReader reader;
//此处将内容从文本提取至文件流中的目的是避免文件被占用,无法删除
FileStream fs1 = new FileStream(fileList[], FileMode.Open);
byte[] bytes1 = new byte[(int)fs1.Length];
fs1.Read(bytes1, , bytes1.Length);
fs1.Close();
reader = new PdfReader(bytes1);
reader.GetPageSize();
// iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(1000,800);//设置样式
iTextSharp.text.Rectangle rec = reader.GetPageSize();
float width = rec.Width;
float height = rec.Height;
//创建一个文档变量
iTextSharp.text.Document document = new iTextSharp.text.Document(rec, , , , );
//创建该文档
PdfWriter pdfWrite = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
//打开文档
document.Open();
//添加内容
PdfContentByte contentByte = pdfWrite.DirectContent;
PdfImportedPage newPage;
for (int i = ; i < fileList.Count; i++)
{ FileStream fs = new FileStream(fileList[i], FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, , bytes.Length);
fs.Close();
reader = new PdfReader(bytes);
int pageNum = reader.NumberOfPages;//获取文档页数
for (int j = ; j <= pageNum; j++)
{
document.NewPage();
newPage = pdfWrite.GetImportedPage(reader, j);
contentByte.AddTemplate(newPage, , );
} }
document.Close();
}
        /// <summary>
/// 图片转PDF
/// </summary>
/// <param name="imagepath">图片位置(绝地路径)</param>
/// <param name="pdfpath">存放PDF地址(绝地路径)</param>
public static void iTextSharpCreatPDF(string imagepath, string pdfpath)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
float percentage = ;
//这里都是图片最原始的宽度与高度
float resizedWidht = image.Width;
float resizedHeight = image.Height;
Document doc = new Document(new iTextSharp.text.Rectangle(resizedWidht, resizedHeight), , , , ); //new Rectangle(1000,1000) //指定文件预设开档时的缩放为100%
//PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create));
doc.Open();
#region 下面对图片进行操作
////这时判断图片宽度是否大于页面宽度减去也边距,如果是,那么缩小,如果还大,继续缩小,
////这样这个缩小的百分比percentage会越来越小
while (resizedWidht > (doc.PageSize.Width - doc.LeftMargin - doc.RightMargin))
{
percentage = percentage * 0.9f;
resizedHeight = image.Height * percentage;
resizedWidht = image.Width * percentage;
}
#region 注释
////There is a 0.8 here. If the height of the image is too close to the page size height,
////the image will seem so big
//while (resizedHeight > (doc.PageSize.Height - doc.TopMargin - doc.BottomMargin) * 0.8)
//{
// percentage = percentage * 0.9f;
// resizedHeight = image.Height * percentage;
// resizedWidht = image.Width * percentage;
//}
#endregion
////这里用计算出来的百分比来缩小图片
image.ScalePercent(percentage * );
//让图片的中心点与页面的中心点进行重合
image.SetAbsolutePosition(doc.PageSize.Width / - resizedWidht / , doc.PageSize.Height / - resizedHeight / );
doc.Add(image);
#endregion
}
catch (DocumentException dex)
{
System.Web.HttpContext.Current.Response.Write(dex.Message);
}
catch (IOException ioex)
{
System.Web.HttpContext.Current.Response.Write(ioex.Message);
} catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
}
finally
{
doc.Close();
}
}

PDF文档转换为图片、图片转成PDF 及PDF合并的更多相关文章

  1. 将Word,PDF文档转化为图片

    #region 将Word文档转化为图片 /// <summary> /// 将Word文档转化为图片 /// </summary> /// <param name=&q ...

  2. 第一节:python提取PDF文档中的图片

    由于项目需要将PDF文档当中的图片转换成图片,所以参考了这篇文章https://blog.csdn.net/qq_15969343/article/details/81673302后项目得以解决. 1 ...

  3. PDF文档转PNG图片 c++(转载)

    PDF文档转PNG图片 c++,例子是转载的,忘记出处了.被我收集起来了. 链接:https://pan.baidu.com/s/1iuxDHibQnvx0UYJ5m25NAg 密码:5o0c

  4. 源码分享!!!world文档转换为JPG图片

    http://bbs.csdn.net/topics/390055515 —————————————————————————————————————————————————— 基本思路是:先将worl ...

  5. 如何使用免费PDF控件从PDF文档中提取文本和图片

             如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...

  6. 使用itextsharp创建PDF文档——图片集合

    文档管理系统中 ,扫描模块将文档或证件扫描后.为了便于保存多个图片,拟将多个图片生成一个PDF文档进行保存. 这里我们就需要PDF生成工具了.你可以在这里下载.PDFCreator 主要使用了开源工具 ...

  7. 转:在 C# 中使用 P/Invoke 调用 Mupdf 函数库显示 PDF 文档

    在 C# 中使用 P/Invoke 调用 Mupdf 函数库显示 PDF 文档 一直以来,我都想为 PDF 补丁丁添加一个 PDF 渲染引擎.可是,目前并没有可以在 .NET 框架上运行的免费 PDF ...

  8. 下载网页中的 pdf 各种姿势,教你如何 carry 各种网页上的 pdf 文档。

    关联词: PDF 下载 FLASH 网页 HTML 报告 内嵌 浏览器 文档 FlexPaperViewer swfobject. 这个需求是最近帮一个妹子处理一下各大高校网站里的 PDF 文档下载, ...

  9. C# 复制PDF页面到另一个PDF文档

    C# 复制PDF页面到另一个PDF文档 有时候我们可能有这样一个需求,那就是把PDF页面从一个PDF文档复制到另一个PDF文档中.由于PDF文档并不像word文档那样好编辑,因此复制也相对没有那么容易 ...

随机推荐

  1. 消息队列kafka

    消息队列kafka   为什么用消息队列 举例 比如在一个企业里,技术老大接到boss的任务,技术老大把这个任务拆分成多个小任务,完成所有的小任务就算搞定整个任务了. 那么在执行这些小任务的时候,可能 ...

  2. 我的第一个C程序

    // // main.c // one // // Created by Shuang Gai on 2019/1/19. // Copyright © 2019 Shuang Gai. All ri ...

  3. springmvc怎么在启动时自己执行一个线程

    之前使用springmvc的时候,都是写好controller和对应的数据库操作. 外界发请求的时候,controller进行一堆操作后返回相应的json数据. 似乎springmvc就是外界驱动的一 ...

  4. fireDAC oracle

    copy 4 files to D:\oracleapp\Administrator\product\11.2.0\client_1\BIN setup win 64 bit client .down ...

  5. Oracle安装过程出现问题---------安装Oracle11gR2先决条件检查失败

    一.错误信息当安装到“先决条件检查” 时,提示如下图所示的错误: 二.错误原因一般情况下,由于操作系统未开启默认共享,导致Oracle无法检查环境的可用性. 三.解决方法1.在运行中(或键盘按 Win ...

  6. Inception

    http://baijiahao.baidu.com/s?id=1601882944953788623&wfr=spider&for=pc

  7. 「这样玩Hexo」修改主题自定义实现界面和功能的自定义

    首发于个人博客 想获得更好的阅读体验,烦请移步⬆️ 前言 作为一个颜党,在换了许多Hexo的主题后,选择了现在使用的fexo主题.但是相比于大多数博主使用的NEXT,fexo还是不够powerful, ...

  8. Java 设计模式系列(四)生成器模式

    Java 设计模式系列(四)生成器模式 生成器模式也称之为建造者模式.将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.demo 1. 生成器模式原理 1.1 生成器模式结构 ...

  9. vue项目 调用百度地图 BMap is not defined

    这次老板新接了一个四点半官网页面,使用vue来写.emm……我感觉整个人都不好了,两天半解决了20个静态页面.还好vue写页面简直飞快,遇到一个vue的新坑,使用百度地图. 研究了好一会,总是报错BM ...

  10. openssl生成ssl证书(转)

    原文:http://blog.sina.com.cn/s/blog_4fd50c390101891c.html x509证书一般会用到三类文,key,csr,crt. Key 是私用密钥openssl ...