您的位置:首页 > 编程语言 > ASP

利用Aspose.Words 把word 生成图片

2014-07-02 08:50 267 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: