最近做了一个系统需要把文件转换为pdf然后把转换后的pdf合成一个pdf文件,网上搜索了半天,最终决定使用itestsharp.dll配合Aspose.words和Aspose.cells来做,废话少说,上代码……

 #region 文件转换pdf
public void ConvertImageToPdf(string inputFileName, string outputFileName)
{
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
using (System.Drawing.Image image = System.Drawing.Image.FromFile(inputFileName))
{
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int framesCount = image.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);
image.SelectActiveFrame(dimension, frameIdx);
Aspose.Words.PageSetup ps = builder.PageSetup;
if (image.Width > 2000)
{
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width / 2, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height / 2, image.VerticalResolution);
}
else
{
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width-300, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height-100, image.VerticalResolution);
}
builder.InsertImage(image, RelativeHorizontalPosition.Page,100, RelativeVerticalPosition.Page, 0, ps.PageWidth, ps.PageHeight, WrapType.Inline);
}
}
doc.Save(outputFileName);
} //Word转换成pdf
/// <summary>
/// 把Word文件转换成为PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool DOCConvertToPDF(string sourcePath, string targetPath)
{
try
{
Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath); doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
} /// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool XLSConvertToPDF(string sourcePath, string targetPath)
{
try
{
Workbook workbook = new Workbook(sourcePath);
workbook.CreateStyle();
workbook.Save(targetPath, Aspose.Cells.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// txt转pdf
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public bool TxtConvertToPDF(string sourcePath, string targetPath)
{
try
{
StreamReader reader = new StreamReader(sourcePath, Encoding.Default); //使用默认编码,否则转换后乱码
string text = reader.ReadToEnd();
Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write(text);
doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
reader.Close(); return true;
}
catch (Exception ex)
{
return false;
}
}
//Word转换成pdf
/// <summary>
/// 把Word文件转换成为PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool ConvertCebToPdf(string sourcePath, string targetPath)
{
try
{
Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath); doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
<p>/// <summary>
        /// 合并pdf
        /// </summary>
        /// <param name="fileList">pdf路径集合</param>
        /// <param name="outMergeFile"></param>
        public void mergePDFFiles(List<string> fileList, string outMergeFile, string headerText)
        {
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25); 
           
            try
            {
                PdfWriter instance = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
                document.Open();
                PdfContentByte directContent = instance.DirectContent;
                BaseFont baseFont = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                #region 目录
                {
                    document.Add(new iTextSharp.text.Paragraph("\r\r\r"));</p><p>                    iTextSharp.text.Paragraph p1 = new iTextSharp.text.Paragraph(new Phrase(headerText, new iTextSharp.text.Font(baseFont)));
                    p1.Alignment = iTextSharp.text.Rectangle.ALIGN_CENTER;  //居中
                    document.Add(p1);
                    document.Add(new iTextSharp.text.Paragraph("\r\r"));
                    int num = 2;
                    for (int i = 0; i < fileList.Count; i++)
                    {
                        PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);
                        int numberOfPages = pdfReader.NumberOfPages;
                        string text = string.Format("{0}. {1}{2}\n\n", (i + 1), fileList[i].Split('$')[2].PadRight((80 - fileList[i].Split('$')[2].Length), '.'), num);
                        iTextSharp.text.Anchor anchor = new iTextSharp.text.Anchor(text, new iTextSharp.text.Font(baseFont));
                        anchor.Reference = "#link" + i;
                        document.Add(anchor);
                        num += numberOfPages;
                    }
 
                    /*for (int i = 0; i < fileList.Count; i++)
                    {
                        PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);
                        string text = string.Format("{0}. {1}\n\n", (i + 1), fileList[i].Split('$')[2]);
                        iTextSharp.text.Anchor anchor = new iTextSharp.text.Anchor(text, new iTextSharp.text.Font(baseFont));
                        anchor.Reference = "#link" + i;
                        document.Add(anchor);
                    }*/</p><p>                }
                #endregion</p><p>                #region 页码
                {
                    iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new iTextSharp.text.Phrase("Page:"), true);
                    footer.Border = iTextSharp.text.Rectangle.TITLE;
                    document.Footer = footer;
                }
                #endregion</p><p>                #region 文件合并
                int nn = 0;
                for (int i = 0; i < fileList.Count; i++)
                {
                    document.NewPage();
                    PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);
                    //添加锚点
                    Anchor anchor = new Anchor((i + 1) + "、 " + fileList[i].Split('$')[2] + "\n\n", new iTextSharp.text.Font(baseFont));
                    anchor.Name = "link" + i;
                    document.Add(anchor);</p><p>                    int numberOfPages = pdfReader.NumberOfPages;
                    for (int j = 1; j <= numberOfPages; j++)
                    {
                        if (nn == numberOfPages)
                            document.NewPage();
                        PdfImportedPage importedPage = instance.GetImportedPage(pdfReader, j);
                        directContent.AddTemplate(importedPage, 0f, 0f);
                        nn = numberOfPages;
                    }</p><p>                }
                #endregion
                document.Close();
            }
            catch (Exception ex)
            {
                document.Close();
            }
        }</p>

itextsharp、Aspose.Words、Aspose.Cells联合使用的更多相关文章

  1. EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类

    大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...

  2. 关于Aspose对于Word操作的一些扩展及思考

    Aspose.word Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务.Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocu ...

  3. Aspose系列实现docx转PDF,PPT转PDF,EXCEL转PDF

    没有什么营养,就是调用一下这个组件.其实一开始用的是Microsoft.Office.Interop.Excel;Microsoft.Office.Interop.Word 但是在服务器要注意,服务器 ...

  4. Java使用Aspose组件进行多文档间的转换操作

    首先,祝大家新年快乐,2019诸事顺利,很久没有更新博客,今天要给大家说的是 ”Aspose“ 组件,作为2019年第一篇博客,希望大家能够多多支持,2019年要继续加油. 什么是Aspose? As ...

  5. aspose words 介绍

    Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务.Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和 ...

  6. Aspose最新版文档格式转换使用破解

    Aspose简介 Aspose.Total是Aspose公司旗下全套文件格式处理解决方案,提供最完整.最高效的文档处理解决方案集,无需任何其他软件安装和依赖.主要提供.net.java.C++d三个开 ...

  7. net 预览文件 转换文件

    预览SWF文件 swfobject.js  (google浏览器 会阻止 需设置) @{ ViewBag.Title = "PdfPreview"; Layout = " ...

  8. 【狼窝乀野狼】Excel那些事儿

    在工作中我们常常遇到Excel表格,不管是数据的导入导出,还是财务统计什么都,都离不开Excel,Excel是我见过的最牛逼的一个软件(可能我的见识少)没有之一:如果你只停留在Excel处理数据,统计 ...

  9. [Asp.net] C# 操作Excel的几种方式 优缺点比较

    在项目中我们常常需要将数据库中的数据导出成Excel文件 有一次工作中我的目的就是读取Excel到内存中,整理成指定格式 整理后再导出到Excel. 因为我要处理的每个Excel表格文件很大.一个表格 ...

随机推荐

  1. (DFS)hdoj1175:连连看

    题目链接 这道题被稍微改编当作过去年的期末上机题,也被直接放到了这次这一届的第二次练习赛.当初刚看到这道题时DFS并没有系统的学过,做起来极其费劲.现在学过之后开始实践练习,发现这道题真的是很水. 我 ...

  2. C语言中动态分配数组

    如何动态的定义及使用数组呢?记得一般用数组的时候都是先指定大小的.当时问老师,老师说是不可以的.后来又问了一位教C++的老师,他告诉我在C++里用new可以做到,一直不用C++,所以也不明白.今天在逛 ...

  3. ODI中通过配置表和自定义逆向工程获取数据库信息

    自定义逆向工程RKM 从配置表meta_db, meta_table, meta_column, meta_key中获取生产库的元数据信息.

  4. rpm软件包管理/yum软件管理

    绝大数开源软件都是公布源代码的,源代码一般被打包为tar.gz归档压缩文件,然后手工编译为二进制可执行文件 ./configure 检查编译环境/相关库文件/配置参数,生成makefile make ...

  5. IOS UTF8中文字母数字 组合时长度截取

    //计算总共字数和限制字数的Index位置 -(NSMutableArray *) unicodeLengthOfString: (NSString *) text { NSMutableArray ...

  6. UITableViewCell 自适应高度 ios8特性

    这篇文章介绍了在一个动态数据的 table view 中,cell 根据 text view 内容的输入实时改变 cell 和 table view 的高度.自动计算 cell 高度的功能使用 iOS ...

  7. 一维条形码攻击技术(Badbarcode)

    0x00 前言 在日常生活中,条形码随处可见,特别在超市,便利店,物流业,但你们扫的条形码真的安全吗?之前TK教主 在PacSec介绍的条形码攻击和twitter上的demo视频太炫酷,所以就自己买了 ...

  8. iOS事件:触摸事件.运动事件.远程控制事件

    iOS中,提供了事件处理:触摸事件,运动事件,远程控制事件.这很大得方便程序猿的工作. 这里先简单做个介绍: // // ViewController.m // demo // // Created ...

  9. Unity3D ShaderLab立方体图的法线渲染

    Unity3D ShaderLab立方体图的法线渲染 某些情况下,我们希望立方体图的材质球上产生法线效果,来更多的表现细节,比如菱形花纹的玻璃,冰块的表面. 在帧数的协调下,我们可以通过input结构 ...

  10. min-height最小高度的实现(兼容IE6、IE7、FF)(解决IE6不兼容min-height)

    <!doctype html><html> <head> <meta charset="UTF-8"> <meta name= ...