使用Aspose编辑WORD文档,使用bookmark定位,并且插入附件

本文介绍如何使用Aspose.Words库在C#中自动化生成带有表格和OLE对象的Word文档,并将其保存为PDF格式。通过定义书签并利用这些书签的位置来插入动态生成的数据表,同时插入图片和其它文档作为OLE对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在word文档中定义bookmark名称,例如我定义了EmployeeTable 和 SICTable。使用
builder.MoveToBookmark("EmployeeTable");定位到EmployeeTable Bookmark
       public void GenerateDocument(string DocumentName, string TemplateID)
        {
            string AsposeLicense = Server.MapPath("~/AsposeLicense/");
            Aspose.Words.License license = new License();
            license.SetLicense(AsposeLicense + "Aspose.Words.lic");

            string FileFlorder = Server.MapPath("~/WorkFolder/Template/");
            string SaveFileFlorder = Server.MapPath("~/WorkFolder/GenerateDocument/");
            string FileFlorderRoot = Server.MapPath("~/");

            var doc = new Aspose.Words.Document(FileFlorder + DocumentName);
            //doc.Document.
            //doc.

            DocumentBuilder builder = new DocumentBuilder(doc);
            DataTable dtEmployee = DBHR.GetDocumentEmployee(TemplateID);

            if (dtEmployee.Rows.Count > 0)
            {
                // DataTable dt = JsonConvert.DeserializeObject(EmployeeTable, typeof(DataTable)) as DataTable;
                builder.MoveToBookmark("EmployeeTable");
                builder.StartTable();

                for (int z = 0; z < dtEmployee.Columns.Count; z++)
                {
                    builder.InsertCell();
                    builder.Write(dtEmployee.Columns[z].ColumnName);
                }
                builder.EndRow();

                for (int x = 0; x < dtEmployee.Rows.Count; x++)
                {
                    for (int y = 0; y < dtEmployee.Columns.Count; y++)
                    {
                        builder.InsertCell();
                        builder.Write(dtEmployee.Rows[x][y].ToString());
                    }
                    builder.EndRow();
                }
                builder.EndTable();
            }

            Image image = Image.FromFile(FileFlorderRoot + "WorkFolder/Image/word.png"); //在word中显示的图标
            builder.InsertOleObject(FileFlorderRoot + "WorkFolder/Image/abc.docx",false,true,image); 插入的附件

            DataTable dtSIC = DBHR.SIC.GetDocumentTemplateSIC(TemplateID);
            if (dtSIC.Rows.Count > 0)
            {
                //DataTable SICdt = JsonConvert.DeserializeObject(SICTable, typeof(DataTable)) as DataTable;
                builder.MoveToBookmark("SICTable");
                builder.StartTable();
                for (int z = 0; z < dtSIC.Columns.Count; z++)
                {
                    builder.InsertCell();
                    builder.Write(dtSIC.Columns[z].ColumnName);
                }
                builder.EndRow();

                for (int x = 0; x < dtSIC.Rows.Count; x++)
                {
                    for (int y = 0; y < dtSIC.Columns.Count; y++)
                    {
                        builder.InsertCell();
                        builder.Write(dtSIC.Rows[x][y].ToString());
                    }
                    builder.EndRow();
                }
                builder.EndTable();
            } 
 
            doc.Save(SaveFileFlorder + DocumentName+"output.PDF", Aspose.Words.SaveFormat.Pdf);
 
 
        }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值