C#Net Aspose.Word替换书签内容(图片、表格、文本)的简单封装

Aspose.Word 替换书签内容:

    public static class AsposeWordReplaceBookMarkContentApi
    {
        public static void BookMarkReplace(Document wordDoc, DocumentBuilder builder, string bookMark, string type, string value)
        {
            var bm = wordDoc.Range.Bookmarks[bookMark];
            if (bm == null)
            {
                return;
            }


            if (type == "IMG")
            {
                bm.Text = "";
                builder.MoveToBookmark(bookMark);
                var img = builder.InsertImage(@value);
                //img.Width = 300;
                //img.Height = 300;
                img.Width = ConvertUtil.PixelToPoint(124d); //单位转换</span>
                img.Height = ConvertUtil.PixelToPoint(134d);
                img.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
            }
            else if (type == "TABLE")
            {
                bm.Text = "";
                builder.MoveToBookmark(bookMark);

                DataTable dt = LitJson.JsonMapper.ToObject<DataTable>(value);//json转datatable

                int rowCount = dt.Rows.Count;
                int columnCount = dt.Columns.Count;

                builder.MoveToBookmark(bookMark);
                builder.StartTable();//开始画Table            
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; // RowAlignment.Center;                

                string str = string.Empty;

                builder.RowFormat.Height = 20;

                //添加列头
                for (int i = 0; i < columnCount; i++)
                {
                    builder.InsertCell();
                    //Table单元格边框线样式
                    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
                    //Table此单元格宽度
                    builder.CellFormat.Width = 600;
                    //此单元格中内容垂直对齐方式
                    builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
                    builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
                    builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
                    //字体大小
                    builder.Font.Size = 10;
                    //是否加粗
                    builder.Bold = true;
                    //向此单元格中添加内容
                    builder.Write(dt.Columns[i].ColumnName);
                }
                builder.EndRow();

                //添加每行数据
                for (int i = 0; i < rowCount; i++)
                {


                    for (int j = 0; j < columnCount; j++)
                    {
                        str = dt.Rows[i][j].ToString();

                        //插入Table单元格
                        builder.InsertCell();

                        //Table单元格边框线样式
                        builder.CellFormat.Borders.LineStyle = LineStyle.Single;

                        //Table此单元格宽度 跟随列头宽度
                        //builder.CellFormat.Width = 500;

                        //此单元格中内容垂直对齐方式
                        builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;

                        builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None;
                        builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;

                        //字体大小
                        builder.Font.Size = 10;

                        //是否加粗
                        builder.Bold = false;

                        //向此单元格中添加内容
                        builder.Write(str);

                    }

                    //Table行结束
                    builder.EndRow();

                }
                builder.EndTable();
            }
            else
            {
                //其他文字,日期等文字
                bm.Text = "";
                builder.RowFormat.HeightRule = HeightRule.Exactly;
                builder.MoveToBookmark(bookMark);
                bm.Text = @value;
            }

        }
    }

调用

    Document doc = new Document('模板路径');
    DocumentBuilder builder = new DocumentBuilder(doc);
    AsposeWordReplaceBookMarkContentApi.BookMarkReplace(doc, builder, '书签名', "bookMark", '对应书签值');
    doc.Save('替换完书签保存路径');




转载请说明出处:原文链接 http://www.nbsite.cn/hdjs/230
标签: C#

相关阅读:

继承IHttpModule拦截http请求

未能加载文件或程序集“Newtonsoft.Json,Version=4.5.0.0 问题

.NET 通过 Autofac 和 DynamicProxy 实现AOP

JS中URL编码与.net URL解码