翻譯|使用教程|編輯:李顯亮|2019-07-12 11:20:08.183|閱讀 578 次
概述:本系列教程將為大家帶來Spire.Doc for .NET在使用過程中的各類實際操作,本篇文章介紹了如何創建藝術字并插入圖片。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.Doc for .NET是一個專業的Word .NET庫,設計用于幫助開發人員高效地開發創建、閱讀、編寫、轉換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。
本系列教程將為大家帶來Spire.Doc for .NET在使用過程中的各類實際操作,本篇文章介紹了如何創建藝術字并插入圖片。
在編輯word文檔時,我們會為文檔添加藝術字,讓文檔更美觀和具有吸引力。Spire.Doc里通過ShapeType 枚舉類型,我們可以添加多種類型的藝術字,并實例化一個ShapeObject 來設置藝術字的類型,并添加藝術字內容和設置其樣式。接下來將詳細介紹如何使用Spire.Doc 創建藝術字并設置樣式和效果。
[C#]
//實例化一個word Document并添加一個section和段落
Document doc = new Document();
Section section = doc.AddSection(); 
Paragraph paragraph = section.AddParagraph();
//添加一個Shape,并設置其大小和樣式
ShapeObject shape = paragraph.AppendShape(240, 60, ShapeType.TextWave);
//設置shape的位置
shape.VerticalPosition = 80;
shape.HorizontalPosition = 100;
//寫入藝術字文本和設置斜體
shape.WordArt.Text = "藝術字效果";
shape.WordArt.Italic = true;
//設置文字填充樣式
shape.FillColor = System.Drawing.Color.Red;
shape.StrokeColor = System.Drawing.Color.Gray;
      
//保存文檔        
doc.SaveToFile("Output.docx", FileFormat.Docx2013);[VB.NET]
Dim doc As New Document()
Dim section As Section = doc.AddSection()
Dim paragraph As Paragraph = section.AddParagraph()
Dim shape As ShapeObject = paragraph.AppendShape(240, 60, ShapeType.TextWave)
shape.VerticalPosition = 80
shape.HorizontalPosition = 100
shape.WordArt.Text = "藝術字效果"
shape.WordArt.Italic = True
shape.FillColor = System.Drawing.Color.Red
shape.StrokeColor = System.Drawing.Color.Gray
doc.SaveToFile("Output.docx", FileFormat.Docx2013)
圖片是Word文檔的基本要素之一,常見的對Word圖片的操作有插入、刪除、替換和提取。接下來將介紹如何使通過編程的方式添加圖片到指定位置,以及如何獲取Word文檔中的圖片并保存到本地路徑。
在指定位置插入圖片
//實例化一個Document對象
Document doc = new Document();
//添加section和段落
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();
//加載圖片到System.Drawing.Image對象, 使用AppendPicture方法將圖片插入到段落
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
DocPicture picture = doc.Sections[0].Paragraphs[0].AppendPicture(image);
//設置文字環繞方式
picture.TextWrappingStyle = TextWrappingStyle.Square;
//指定圖片位置
picture.HorizontalPosition = 50.0f;
picture.VerticalPosition = 50.0f;
//設置圖片大小
picture.Width = 100;
picture.Height = 100;
//保存到文檔
doc.SaveToFile("Image.doc", FileFormat.Doc);
提取Word文檔中的圖片
//初始化一個Document實例并加載Word文檔
Document doc = new Document();
doc.LoadFromFile(@"Image.doc");
int index = 0;
//遍歷Word文檔中每一個section
foreach (Section section in doc.Sections)
{
    //遍歷section中的每個段落
    foreach (Paragraph paragraph in section.Paragraphs)
    {
        //遍歷段落中的每個DocumentObject
        foreach (DocumentObject docObject in paragraph.ChildObjects)
        {
            //判斷DocumentObject是否為圖片
            if (docObject.DocumentObjectType == DocumentObjectType.Picture)
            {
                //保存圖片到指定路徑并設置圖片格式
                DocPicture picture = docObject as DocPicture;
                String imageName = String.Format(@"images\Image-{0}.png", index);
                picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
                index++;
            }
        }
    }
}
*購買Spire.Doc for .NET正版授權的朋友可以點擊哦~~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn