利用Aspose.Words 把word 生成图片

本文介绍如何使用Aspose.Words库将Word文档转换为具有翻页效果的HTML格式,包括设置页面属性、图片渲染及生成过程。

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

 Aspose.Words.Document doc = new Aspose.Words.Document(HttpContext.Current.Server.MapPath(strFileName));
            DocumentBuilder oWordApplic = new DocumentBuilder(doc);
            StringBuilder sb = new StringBuilder();
            string headerStr = string.Format(@"<book pageheight='620' pagewidth='439' bgcolor='0xffffff' pagethickness='0.5' maxloaded='0' minscale='0.5' plugins='navigationbar, gallery, overlays, titles, anchors, swfaddress, links, keyboardnavigation, slideshow'>
            <chapter>
            <page stiff='true' anchor='cover'>
            <img  aa='true' src='{0}/0.png' />
            </page>", "lib");
            string itemStr = @" <spreadpage>
            <img src='lib/{0}.png' />
            </spreadpage>";
            string tailStr = @" <page stiff='true'>
            <img   src='lib/{0}.png' aa='true' />
            </page>
            </chapter>
            </book>";
            sb.Append(headerStr);

            float MyScale = 0.50f;
            float MyResolution = 150.0f;
            int count = doc.PageCount;
            for (int i = 0; i < count; i++)
            {
                if (i > 0 && i < count - 1)
                {
                    sb.Append(string.Format(itemStr, i));
                }
                PageInfo pageInfo = doc.GetPageInfo(i);
                
                Size pageSize = pageInfo.GetSizeInPixels(MyScale, MyResolution);
                using (Bitmap img = new Bitmap(pageSize.Width + 80, pageSize.Height))
                {
                    img.SetResolution(MyResolution, MyResolution);
                    using (Graphics gr = Graphics.FromImage(img))
                    {
                        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                        gr.FillRectangle(Brushes.White, 0, 0, pageSize.Width + 80, pageSize.Height);
                        doc.RenderToScale(i, gr, 0, 0, MyScale);
                    }
                    
                    img.Save(string.Format("{0}/{1}.png", Server.MapPath("lib"), i));
                }
            }

            sb.Append(string.Format(tailStr, count - 1));

           
            string content = sb.ToString();
            Response.Write(content);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值