PDF合并

摘要:
此虚线是Excel生成PDF后显示的最大宽度。2、 然后合并四个PDF,注意合并方法:///////PDF merge//////要合并的PDF名称集//合并的PDFpublicstatic void合并PDF文件{PdfReaderreader;Documentdocument=newDocument();PdfWriter writer=PdfWriter.GetInstance;document.SetPageSize;//如果模板是A4,则必须是A4文档。Open() ; PdfContentBytecb=写入程序。DirectContent;PdfImportedPagenewPage;foreach{reader=newPdfReader;intiPageNum=reader.NumberOfPages;for{document.NewPage();NewPage=writer.GetImportedPage;cb.AddTemplate;}}文件Close();}调用:List<string>arrFileList=newList<string˃();arrFileList。添加合并PDF文件;表根据模板生成excel工作簿excel=newWorkbook();stringstrFilePath=ExcelTemplatePath+strTableName+“.xls”;//创建excel并打开模板文件excel。打开工作表=excel。工作表[“Sheet1”];对照表1表单元格。删除列;删除指定的列excel。保存///转换为PDFConvertXlsToPdf;Excel到PDF使用此引用:Aspose.Cells。dll,Aspose.Pdf.Kit。dll,Interop.Excel。dll,itemshare最好使用最新的dll。

要求:将多个table导出到一个PDF里,然后打印。 

问题分析:要求将四个table放一个PDF打印,四个table的列各不相同,第一个是表头,其他三个是列表,列比表头多很多,如果直接生成一个excel,然后再直接导出会发现有些列在PDF中换行了。 

原因:因为excle可打印的区域是有限制的,可打印的地方如下方法可见:文件-打印-设置-打印选择区域(如果打印要宽一点,选择A4,横向打印,)这时你点开始(2010版)回到sheet里,发现里面有一条虚线。这个虚线就是excel生成PDF后(相应纸张格式)最多显示的宽。

解决方法:

一,为了解决上面所说情况,可以设好四个table对应的excle模板,然后生成四个pdf。

二,再将四个PDF全并,注意(打印的方向,纸张大小一定要一样)

合并方法:

/// <summary>
        /// PDF合并
        /// </summary>
        /// <param name="arrFileList">要合并的PDF名称集</param>
        /// <param name="outMergeFile">合并后的PDF</param>
        public static void MergePDFFiles(List<string> arrFileList, string strOutMergeFile)
        {
            PdfReader reader;
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strOutMergeFile, FileMode.Create));  
document.SetPageSize(PageSize.A4.Rotate()); //如果你的模板是A4的,这里一定要是A4 document.Open(); PdfContentByte cb
= writer.DirectContent; PdfImportedPage newPage; foreach (string strFileName in arrFileList) { reader = new PdfReader(strFileName); int iPageNum = reader.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { document.NewPage(); newPage = writer.GetImportedPage(reader, j); cb.AddTemplate(newPage, 0, 0); } } document.Close(); }

调用:

List<string> arrFileList= new List<string>();
arrFileList.Add(strFileName);
MergePDFFiles(arrFileList, strFileName);


table根据模板生成excel

 Workbook excel = new Workbook();
string strFilePath = ExcelTemplatePath + strTableName + ".xls";
 //建立excel并打开模板文件
excel.Open(strFilePath);
Worksheet sheet = excel.Worksheets["Sheet1"];    控制sheet1
sheet.Cells.DeleteColumn((int.Parse(c.OrderNo.ToString()) - intDeleteCount));删除指定列
excel.Save(strFileName, FileFormatType.Default);
 ///转成PDF
 ConvertXlsToPdf(strFileName, strFileNamePdf);

excel转PDF

使用这个要引用:Aspose.Cells.dll、Aspose.Pdf.Kit.dll、Interop.Excel.dll、itextsharp.dll 最好用最新的。

/// 将Xls文件转换为PDF文件
/// </summary>
/// <param name="strSourceFile">源文件</param>
/// <param name="strTargetFile">目标文件</param>
/// <returns>是否成功</returns>
public static bool ConvertXlsToPdf(string strSourceFile, string strTargetFile)
{
    if (File.Exists(strTargetFile))
    {
        File.Delete(strTargetFile);
    }

    FileInfo fiPdf = new FileInfo(strTargetFile);
    if (!fiPdf.Directory.Exists)
    {
        fiPdf.Directory.Create();
    }

    bool blnResult = false;

    #region 微软式导出
    Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
    object missing = Type.Missing;
    Excel.ApplicationClass application = null;
    Excel.Workbook workBook = null;
    object paramFromPage = Type.Missing;
    object paramToPage = Type.Missing;
    try
    {
        application = new Excel.ApplicationClass();

        object target = strTargetFile;
        //object type = targetType;
        System.IO.FileInfo fi = new System.IO.FileInfo(strSourceFile);

        workBook = application.Workbooks.Open(fi.FullName, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing, missing);

        workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
        blnResult = true;
    }
    catch (Exception ex)
    {
        blnResult = false;
        throw ex;
    }
    finally
    {
        if (workBook != null)
        {
            workBook.Close(false, missing, missing);
            workBook = null;
        }
        if (application != null)
        {
            application.Quit();
            application = null;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    #endregion
    return blnResult;
}

 注意:如果报:无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。右击属性,有个东西改一下,具体有点忘了。

免责声明:文章转载自《PDF合并》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇image创建栅格地图《蜻蜓少年》有感下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#中NPOI操作excel之读取和写入excel数据

一、下载引用 下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2007版需要此dll)。 二、excel转datatable类 [csharp]view plaincopy usingSystem; usingSy...

使用ado访问excel中的数据

【1】链接数据源(excel文件) (1)配置链接字符串: string connString = @"Provider = Microsoft.Jet.OLEDB.4.0; Extended Properties = Excel 8.0; Data Source = C:UsersBoBoDesktopstudent.xlsx"; (2)创建链...

使用POI导出excel基础篇

最近搞了下POI导出Excel,听说很多次,却是第一次搞。 在pom.xml中引入依赖 <dependency>   <groupId>org.apache.poi</groupId>   <artifactId>poi</artifactId>   <version>3.10-FI...

asp.net 调用 excel 组件

Asp.net 如何调用 Excel ? 1.引用 Microsoft.Office.Interop.Excel.dll,自动包装成Interop.Microsoft.Office.Interop.Excel.dll 2.代码: ///<summary> ///生成 excel 报表 ///</summary> privatevoi...

python 将文本txt 转换 excel(xls)

现在有很多工具可以将mysql数据库中数据直接dump为excel表格模式,例如,navicat,sqlyog等,但是个人认为最好用是navicat,但是navicat需要收费。 通过select into outfile 可将数据导出为.txt格式,然后再通过python脚本将.txt格式转换成xls格式。 MySQL导出文本语句: SELECT * F...

java生成pdf字体的坑

java生成pdf字体的坑 做过java生成pdf的应该都知道有很多坑,特别是字体。 这里就记录一下字体的问题。 系统必须安装字体,项目的resources目录放置了要用到的中文字体,宋体和黑体。并在代码中进行了引用。但是实际发现系统仍然需要安装字体。 安装字体的坑 这里主要记录一下安装字体的坑 网上找到的教程 cd /usr/share/fonts/...