新聞中心
一、序言
在Office Word中,支持在Word文檔中插入類(lèi)型非常豐富的形狀,包括線條、矩形、基本形狀(諸如圓形、多邊形、星形、括號(hào)、笑臉等等圖形)、箭頭形狀、公式形狀、流程圖、旗幟圖形、標(biāo)注圖形等等,我們?cè)诰幊踢^(guò)程中,想要在Word中繪制不同類(lèi)型的圖形,可以通過(guò)類(lèi)庫(kù)來(lái)操作??丶pire.Doc for .NET 6.0及以上版本開(kāi)始支持Office Word中的所有圖形,可以通過(guò)代碼操作某個(gè)單一的形狀,也可以通過(guò)將單一形狀進(jìn)行組合來(lái)獲得想要的圖形或形狀效果,當(dāng)然,也支持自己自定義圖形,通過(guò)編程繪制也是可以的。下面將介紹向Word繪制形狀和組合形狀的方法,方法中的代碼供參考。
創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)、網(wǎng)站重做改版、磴口網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為磴口等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
PS:
- Spire.Doc for .NET獲取地址
- 安裝后,dll文件可在安裝路徑下的Bin文件夾中獲取
Dll引用二、代碼示例
(一)、繪制單一形狀
步驟1:添加如下using指定
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
步驟2:創(chuàng)建示例,添加section、paragraph
//創(chuàng)建一個(gè)Document實(shí)例
Document doc = new Document();
//添加一個(gè)section paragraph
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();
步驟3:在文檔指定位置插入形狀,并設(shè)置形狀類(lèi)型、大小、填充顏色、線條樣式等
(這里簡(jiǎn)單列舉幾個(gè)形狀的添加方法,方法比較簡(jiǎn)單,不做贅述,效果圖中列舉了部分形狀樣式,需要其他樣式的形狀可自行設(shè)置添加)
//插入一個(gè)矩形
ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Rectangle);
shape1.FillColor = Color.Blue;
shape1.StrokeColor = Color.LightSkyBlue;
shape1.HorizontalPosition = 20;
shape1.VerticalPosition = 20;
//插入一個(gè)圓形
ShapeObject shape2 = para1.AppendShape(50, 50, ShapeType.Ellipse);
shape2.FillColor = Color.Purple;
shape2.StrokeColor = Color.LightPink;
shape2.LineStyle = ShapeLineStyle.Single;
shape2.StrokeWeight = 1;
shape2.HorizontalPosition = 80;
shape2.VerticalPosition = 20;
//插入一個(gè)公式符號(hào) +
ShapeObject shape3 = para1.AppendShape(50, 50, ShapeType.Plus);
shape3.FillColor = Color.DarkCyan;
shape3.StrokeColor = Color.LightGreen;
shape3.LineStyle = ShapeLineStyle.Single;
shape3.StrokeWeight = 1;
shape3.HorizontalPosition = 140;
shape3.VerticalPosition = 20;
//插入一顆星形
ShapeObject shape4 = para1.AppendShape(50, 50, ShapeType.Star);
shape4.FillColor = Color.Red;
shape4.StrokeColor = Color.Gold;
shape4.LineStyle = ShapeLineStyle.Single;
shape4.HorizontalPosition = 200;
shape4.VerticalPosition = 20;
步驟4:保存文檔
//保存并打開(kāi)文檔
doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapes.docx");
形狀添加效果:
(二)、添加組合形狀
步驟1:添加如下using指令
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
步驟2:創(chuàng)建文檔,添加section、paragraph
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();
步驟3:添加文字,并應(yīng)用格式到文字
para1.AppendText("中日文化交流");
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "titleStyle";
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.FontName = "隸書(shū)";
style1.CharacterFormat.FontSize = 30f;
doc.Styles.Add(style1);
para1.ApplyStyle("titleStyle");
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
步驟4:實(shí)例化段落2,并創(chuàng)建一個(gè)形狀組合,并設(shè)置大小
//實(shí)例化段落2
Paragraph para2 = sec.AddParagraph();
//創(chuàng)建一個(gè)形狀組合并設(shè)置大小
ShapeGroup shapegr = para2.AppendShapeGroup(300, 300);
步驟5:繪制一個(gè)中國(guó)國(guó)旗,這里需要組合形狀矩形和五角星形,并填充相應(yīng)的顏色
//添加一個(gè)矩形到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = 900,
Height = 500,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = 1,
});
//添加第一個(gè)五角星到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 100,
Height = 100,
VerticalPosition = 90,
HorizontalPosition = 90,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第二個(gè)五角星到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 40,
HorizontalPosition = 210,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第三個(gè)五角星到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 80,
HorizontalPosition = 280,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第四個(gè)五角星到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 160,
HorizontalPosition = 280,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
//添加第五個(gè)五角星到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
{
Width = 50,
Height = 50,
VerticalPosition = 220,
HorizontalPosition = 210,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Yellow,
StrokeColor = Color.Yellow,
StrokeWeight = 1,
});
步驟6:繪制一個(gè)日本國(guó)旗,需要組合形狀矩形和圓形,并填充顏色
//繪制一個(gè)矩形并添加到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
Width = 900,
Height = 500,
VerticalPosition = 700,
HorizontalPosition = 600,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.WhiteSmoke,
StrokeColor = Color.WhiteSmoke,
StrokeWeight = 1,
});
//繪制一個(gè)圓形并添加到形狀組合
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Ellipse)
{
Width = 250,
Height = 250,
VerticalPosition = 800,
HorizontalPosition = 900,
LineStyle = ShapeLineStyle.Single,
FillColor = Color.Red,
StrokeColor = Color.Red,
StrokeWeight = 1,
});
步驟7:保存文檔
//保存并打開(kāi)文檔
doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapegroups.docx");
添加效果:
以上全部是關(guān)于Word中繪制圖形形狀的內(nèi)容。如需轉(zhuǎn)載,請(qǐng)注明出處!
感謝閱讀!
標(biāo)題名稱(chēng):C#繪制Word圖形、組合圖形
網(wǎng)頁(yè)地址:http://fisionsoft.com.cn/article/jegogp.html