c#调用Aspose.Word组件一些常用操作

1、c#接口接受值、调用方法

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string option = context.Request["option"];
            //if(option!=null)
            if (!String.IsNullOrEmpty(op))
            {
                switch(option)
                {
                    case "Math":
                        Math(context);
                        break;
                    case "Math1":
                        Math1(context);
                        break;
                }
            }
        }

2、Aspose.word常用操作

引用:using Aspose.Words;

public void Math(HttpContext context)
        {
            //接受前端传进来的参数
            string title = context.Request["title"];
            string templetepath= context.Server.MapPath(@"~") + "\\a\\横版.docx";
            string codepath = context.Server.MapPath("~/a/Img/" + id + "/") + title + ".jpg";
            try
            {
                Aspose.Words.Document doc = new Document(templetepath);
                DocumentBuilder builder = new DocumentBuilder(doc);
                //插入表格
                Aspose.Words.Tables.Table table = builder.StartTable();
                builder.CellFormat.TopPadding = 2.5;
                builder.CellFormat.BottomPadding = 2.5;
                builder.InsertCell();
                builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
                builder.CellFormat.Width = 400;
                builder.Font.Name = "微软雅黑";
                builder.Font.Size = 12;
                builder.Font.Bold = false;
                builder.Write("信息");
                builder.EndRow();
                int width= Convert.ToInt32(100*0.05);
                int height=Convert.ToInt32(700*0.05);
                //word报告插入书签,移动到书签,写入内容
                builder.MoveToBookmark("title");
                Aspose.Words.Font font = builder.Font;
                font.Name = "微软雅黑";
                font.Size = 18;
                font.Bold = false;
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                builder.Write(title);  //不换行
                builder.EndRow();
                Aspose.Words.Tables.Row r2 = new Aspose.Words.Tables.Row(doc);
                NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); //拿到所有表格
                Aspose.Words.Tables.Table table = allTables[0] as Aspose.Words.Tables.Table; //拿到第二个表格
                //插入二维码 map_Title, false, false
                builder.MoveToMergeField("土地利用总体规划", false, false);  //
                builder.MoveToCell(0, 0, 0, 0);                              //移动到表格位置。
                builder.MoveToBookmark("code");                              //移动到“code”书签                       
                //插入图片方式
                builder.InsertImage(codepath);                   //直接插入
                builder.InsertImage(imgpath, width, height);     //设置高宽,下同 
                builder.InsertImage(codepath, ConvertUtil.PixelToPoint(70), ConvertUtil.PixelToPoint(70));
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;  //设置插入样式

                //scale书签替换map_Scale、map_Time
                doc.Range.Bookmarks["code"].Text = code;
                //替换表格中的&时间&(code的位置写成为“&时间&”,二维码直接替换“&时间&”),采用的是正则表达式
                doc.Range.Replace("&时间&", code, new Aspose.Words.Replacing.FindReplaceOptions());
                if (File.Exists(savepath))
                {
                    File.Delete(savepath);
                }
                doc.Save(savepath);
                Document docword = new Document(savepath);
                docword.Save(context.Server.MapPath("~") + "\\ashx\\tdly\\MapDKImg\\" + id + "\\" + title + ".pdf", Aspose.Words.SaveFormat.Pdf);
                context.Response.Write("/a/Img/" + id + "/" + title + ".pdf");
            }
            catch (Exception ex)
            {
                ex.ToString();
                context.Response.Write(ex.ToString());
            }
        }

3、生成二维码

引用:
using ThoughtWorks.QRCode.Codec;

public void Math1(HttpContext context)
        {
            //二维码保存的是字符串
            context.Response.ContentType = "text/plain";
            string pid = context.Request.QueryString["id"];
            try
            {
                string concent = "https://www.bilibili.com/?av=" + id;
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                qrCodeEncoder.QRCodeScale = 4;
                qrCodeEncoder.QRCodeVersion = 8;
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                System.Drawing.Image image = qrCodeEncoder.Encode(concent);
                if (!Directory.Exists(filepath))
                {
                    Directory.CreateDirectory(filepath);
                }
                filepath = filepath + filename + ".jpg";
                System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                fs.Close();
                image.Dispose();
                return filepath;
            }
            catch
            {
                //LocalPub.WriteSysLog(prjid.ToString(), "1.1", "2", "生成图片二维码失败", "2");
                return "";
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值