• 将数据写入Word模版,生成PDF并加水印


    需要引用,DLL请自行下载

    using Aspose.Pdf;
    using Aspose.Words;
    using System.Data;
    using System.IO;

    首选创建Word数据模版;使用Office或者WPS创建一个DOC模版,并增加域;如何增加域名可自行搜索

    WPS增加 使邮件合并

    然后制做出以下类似模版

    关于输出列表;需要使用 域 

    将你要循环输出的字段包住

    //这里获取Table数据源,表的列名需要与模版中的域名对应
                DataTable dt = new DataTable();
                //这里获取列表数据的数据源;表的列名也需要与域名对应
                DataTable dtlist = new DataTable();
    
                //建议列表数据列最好不要与单一数据的表存在相同的列名称
                string _templatePath = "template.doc"; //模板路径
                _templatePath = Server.MapPath(_templatePath);
                string _savePath = "/out/"; //保存生成文档路径
                string sp = Server.MapPath(_savePath);
                string f = Guid.NewGuid().ToString("N") + ".doc";
                string copyto = sp + f;
    
                File.Copy(_templatePath.ToString(), copyto, true); //将文件复制到结果文档中
                Aspose.Words.Document doc = new Aspose.Words.Document(copyto);
                Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
                //先进行列表数据的替换,注意DataTable.TableName属性要与 «TableStart:s2198» 域名保持一致
                doc.MailMerge.ExecuteWithRegions(dtlist);
    
    
                foreach (DataRow r in dt.Rows)
                {
                    //这里获取列名,请注意更改为自己获取列名的逻辑
                    string field = string.Format("s{0}", r["itemid"].ToString());//
                    while (builder.MoveToMergeField(field))
                    {
                        builder.Write(r["value"].ToString());//写入值
                    }
                }
    
                //到此,往WORD模版里写入数据的逻辑就已经完成,接下来将数据保存为PDF
    
                MemoryStream docStream = new MemoryStream();
                doc.Save(docStream, Aspose.Words.SaveFormat.Pdf);
                Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(docStream);
                //增加水印图片,将图片放在根目录下
                ImageStamp imageStamp = new ImageStamp(Server.MapPath("waterMark3_3.gif"));
                imageStamp.Background = true;
                //设置了图片宽高;不设置则按照实际图片宽高
                //imageStamp.Height = 350;
                //imageStamp.Width = 350;
                imageStamp.Opacity = 0.5;
                //水印位置
                imageStamp.HorizontalAlignment = HorizontalAlignment.Center;
                imageStamp.VerticalAlignment = VerticalAlignment.Center;
                //为PDF的每个页面都加上水印
                for (int j = 1; j <= pdfDocument.Pages.Count; j++)
                {
                    pdfDocument.Pages[j].AddStamp(imageStamp);
                }
                //保存PDF
                pdfDocument.Save(docStream);
                //如果是页面的话,可以下载PDF
                Response.ContentType = "application/msword";
                Response.AddHeader("content-disposition", "attachment;  filename=Print" + child.Rows.Count + ".pdf");
                Response.BinaryWrite(docStream.ToArray());
                Response.End();
  • 相关阅读:
    Spring创建对象的方法
    Spring学习笔记1
    WIN7系统TortoiseSVN右键没有菜单解决办法
    TotoiseSVN的基本使用方法
    sql语句中where 1=1和 0=1 的作用
    windows批处理命令教程
    Mysql之B树索引
    Mysql与索引有关的树的概念
    Mysql索引简介
    Mysql之explain详解
  • 原文地址:https://www.cnblogs.com/gxivwshjj/p/12453699.html
Copyright © 2020-2023  润新知