C# 操作Word 中的OLE——插入、编辑、读取 OLE【分

作者:编程技术

【示例3】读取Word中的OLE

 步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;

步骤 2 :加载文件

//实例化一个Document对象,加载一个有OLE对象的文档
Document doc = new Document();          
doc.LoadFromFile(@"test.docx");

步骤 3 :遍历section,读取OLE

//遍历文档所有Section          
            foreach (Section sec in doc.Sections)
            {
                //遍历Section下面所有的子元素
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph par = obj as Paragraph;
                        //遍历这个section下面的段落
                        foreach (DocumentObject o in par.ChildObjects)
                        {
                            //找到OLE对象,并根据类型提取
                            if (o.DocumentObjectType == DocumentObjectType.OleObject)
                            {
                                DocOleObject Ole = o as DocOleObject;
                                /*ObjectType属性可以获取ole对象的具体类型。
                                注意,如果是用Spire.Doc添加的ole对象的话,需要在AppendOleObject
                                的时候先声明OleObjectType,不然这里得不到具体的类型,只会得到Package*/
                                string s = Ole.ObjectType;
                                //"AcroExch.Document.11"是指PDF对象对应的ProgID
                                if (s == "AcroExch.Document.11")
                                {
                                    File.WriteAllBytes("Result.pdf", Ole.NativeData);
                                }
                                //"Excel.Sheet.12"是指 Excel03之后的工作表对应的ProgID
                                else if (s == "Excel.Sheet.12")
                                {
                                    File.WriteAllBytes("Result.xlsx", Ole.NativeData);
                                }
                                //"Word.Document.12"是指03之后的Word对应的ProgID
                                else if (s == "Word.Document.12")
                                {
                                    File.WriteAllBytes("Result.docx", Ole.NativeData);
                                }
                            }
                        }
                    }
                }
            }

疗养运营程序,生成文档,如下:

 分分快三计划 1

全总代码:

分分快三计划 2分分快三计划 3

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;

namespace ReadOLE_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document对象,加载一个有OLE对象的文档
            Document doc = new Document();          
            doc.LoadFromFile(@"test.docx");

            //遍历文档所有Section          
            foreach (Section sec in doc.Sections)
            {
                //遍历Section下面所有的子元素
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph par = obj as Paragraph;
                        //遍历这个section下面的段落
                        foreach (DocumentObject o in par.ChildObjects)
                        {
                            //找到OLE对象,并根据类型提取
                            if (o.DocumentObjectType == DocumentObjectType.OleObject)
                            {
                                DocOleObject Ole = o as DocOleObject;
                                /*ObjectType属性可以获取ole对象的具体类型。
                                注意,如果是用Spire.Doc添加的ole对象的话,需要在AppendOleObject
                                的时候先声明OleObjectType,不然这里得不到具体的类型,只会得到Package*/
                                string s = Ole.ObjectType;
                                //"AcroExch.Document.11"是指PDF对象对应的ProgID
                                if (s == "AcroExch.Document.11")
                                {
                                    File.WriteAllBytes("Result.pdf", Ole.NativeData);
                                }
                                //"Excel.Sheet.12"是指 Excel03之后的工作表对应的ProgID
                                else if (s == "Excel.Sheet.12")
                                {
                                    File.WriteAllBytes("Result.xlsx", Ole.NativeData);
                                }
                                //"Word.Document.12"是指03之后的Word对应的ProgID
                                else if (s == "Word.Document.12")
                                {
                                    File.WriteAllBytes("Result.docx", Ole.NativeData);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

View Code

上述是本次关于C# 操作word中的OLE的全体内容。示例方法供参考。

 

本文完。

如需转发,请注解出处!

【示例3】读取书签地方的原委

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

步骤 2 :获取书签地方的剧情

//初始化BookmarkNavigator类对象
BookmarksNavigator navigator = new BookmarksNavigator(doc);
//定位到指定书签位置,获取书签位置的文档内容
navigator.MoveToBookmark("bookmark1");
TextBodyPart textBodyPart = navigator.GetBookmarkContent();

手续 3 :读取书签内容

//遍历书签内容中的子项目,并将文本信息提取至string类型变量中
string text = null;
foreach (var item in textBodyPart.BodyItems)
{
    if (item is Paragraph)
    {
        foreach (var childObject in (item as Paragraph).ChildObjects)
        {
            if (childObject is TextRange)
            {
                text  = (childObject as TextRange).Text;
            }
        }
    }
}
//控制台输出文本
Console.WriteLine(text);
Console.ReadLine();

 

 读取结果:

分分快三计划 4

全套代码:

分分快三计划 5分分快三计划 6

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

namespace GetTextOfBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document类,加载测试文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //初始化BookmarkNavigator类对象
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            //定位到指定书签位置,获取书签位置的文档内容
            navigator.MoveToBookmark("bookmark1");
            TextBodyPart textBodyPart = navigator.GetBookmarkContent();

            //遍历书签内容中的子项目,并将文本信息提取至string类型变量中
            string text = null;
            foreach (var item in textBodyPart.BodyItems)
            {
                if (item is Paragraph)
                {
                    foreach (var childObject in (item as Paragraph).ChildObjects)
                    {
                        if (childObject is TextRange)
                        {
                            text  = (childObject as TextRange).Text;
                        }
                    }
                }
            }
            //控制台输出文本
            Console.WriteLine(text);
            Console.ReadLine();
        }
    }
}

View Code

 

(二)加多组合造型

手续1:增添如下using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

 

手续2:创设文书档案,增加section、paragraph

Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();

 

步骤3:添Gavin字,并利用格式到文字

para1.AppendText("中日文化交流");
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "titleStyle";
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.FontName = "隶书";
style1.CharacterFormat.FontSize = 30f;
doc.Styles.Add(style1);
para1.ApplyStyle("titleStyle");
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;

 

手续4:实例化段落2,并创办二个形态组合,并安装大小

//实例化段落2
Paragraph para2 = sec.AddParagraph();
//创建一个形状组合并设置大小
ShapeGroup shapegr = para2.AppendShapeGroup(300, 300);

 

手续5:绘制三个神州国旗,这里供给结合形象矩形和五角星形,并填写相应的颜色

 //添加一个矩形到形状组合
            shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
            {
                Width = 900,
                Height = 500,
                LineStyle = ShapeLineStyle.Single,
                FillColor = Color.Red,
                StrokeColor = Color.Red,                
                StrokeWeight = 1,
            });

            //添加第一个五角星到形状组合
            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,
            });
            //添加第二个五角星到形状组合
            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,
            });
            //添加第三个五角星到形状组合
            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,
            });
            //添加第四个五角星到形状组合
            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,
            });
            //添加第五个五角星到形状组合
            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:绘制一个东瀛国旗,须要整合造型矩形和圆形,并填写颜色

//绘制一个矩形并添加到形状组合
            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,
            });
            //绘制一个圆形并添加到形状组合
            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:保存文书档案

//保存并打开文档
doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapegroups.docx");

 

加多遵从:

分分快三计划 7

(此时的图片是组成后的机能,猖狂拖动图形不会冒出各种形态分离、错位的动静。)

任何代码:

分分快三计划 8分分快三计划 9

  1 using Spire.Doc;
  2 using Spire.Doc.Documents;
  3 using Spire.Doc.Fields;
  4 using System.Drawing;
  5 
  6 namespace InsertShapesGroup_Doc
  7 {
  8     class Program
  9     {
 10         static void Main(string[] args)
 11         {
 12             //创建一个Document实例并添加section及paragraph
 13             Document doc = new Document();
 14             Section sec = doc.AddSection();
 15             Paragraph para1 = sec.AddParagraph();
 16             //添加文字,并应用格式到文字
 17             para1.AppendText("中日文化交流");
 18             ParagraphStyle style1 = new ParagraphStyle(doc);
 19             style1.Name = "titleStyle";
 20             style1.CharacterFormat.Bold = true;
 21             style1.CharacterFormat.FontName = "隶书";
 22             style1.CharacterFormat.FontSize = 30f;
 23             doc.Styles.Add(style1);
 24             para1.ApplyStyle("titleStyle");
 25             para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
 26 
 27             //实例化段落2
 28             Paragraph para2 = sec.AddParagraph();
 29             //创建一个形状组合并设置大小
 30             ShapeGroup shapegr = para2.AppendShapeGroup(300, 300);
 31 
 32             //添加一个矩形到形状组合
 33             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
 34             {
 35                 Width = 900,
 36                 Height = 500,
 37                 LineStyle = ShapeLineStyle.Single,
 38                 FillColor = Color.Red,
 39                 StrokeColor = Color.Red,                
 40                 StrokeWeight = 1,
 41             });
 42 
 43             //添加第一个五角星到形状组合
 44             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
 45             {
 46                 Width = 100,
 47                 Height = 100,
 48                 VerticalPosition = 90,
 49                 HorizontalPosition = 90,
 50                 LineStyle = ShapeLineStyle.Single,
 51                 FillColor = Color.Yellow,
 52                 StrokeColor = Color.Yellow,
 53                 StrokeWeight = 1,
 54             });
 55             //添加第二个五角星到形状组合
 56             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
 57             {
 58                 Width = 50,
 59                 Height = 50,
 60                 VerticalPosition = 40,
 61                 HorizontalPosition = 210,
 62                 LineStyle = ShapeLineStyle.Single,
 63                 FillColor = Color.Yellow,
 64                 StrokeColor = Color.Yellow,
 65                 StrokeWeight = 1,
 66             });
 67             //添加第三个五角星到形状组合
 68             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
 69             {
 70                 Width = 50,
 71                 Height = 50,
 72                 VerticalPosition = 80,
 73                 HorizontalPosition = 280,
 74                 LineStyle = ShapeLineStyle.Single,
 75                 FillColor = Color.Yellow,
 76                 StrokeColor = Color.Yellow,
 77                 StrokeWeight = 1,
 78             });
 79             //添加第四个五角星到形状组合
 80             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
 81             {
 82                 Width = 50,
 83                 Height = 50,
 84                 VerticalPosition = 160,
 85                 HorizontalPosition = 280,
 86                 LineStyle = ShapeLineStyle.Single,
 87                 FillColor = Color.Yellow,
 88                 StrokeColor = Color.Yellow,
 89                 StrokeWeight = 1,
 90             });
 91             //添加第五个五角星到形状组合
 92             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Star)
 93             {
 94                 Width = 50,
 95                 Height = 50,
 96                 VerticalPosition = 220,
 97                 HorizontalPosition = 210,
 98                 LineStyle = ShapeLineStyle.Single,
 99                 FillColor = Color.Yellow,
100                 StrokeColor = Color.Yellow,
101                 StrokeWeight = 1,
102             });
103 
104             //绘制一个矩形并添加到形状组合
105             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
106             {
107                 Width = 900,
108                 Height = 500,
109                 VerticalPosition = 700,
110                 HorizontalPosition = 600,
111                 LineStyle = ShapeLineStyle.Single,
112                 FillColor = Color.WhiteSmoke,
113                 StrokeColor = Color.Wheat,
114                 StrokeWeight = 1,
115             });
116             //绘制一个圆形并添加到形状组合
117             shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Ellipse)
118             {
119                 Width = 250,
120                 Height = 250,
121                 VerticalPosition = 800,
122                 HorizontalPosition = 900,
123                 LineStyle = ShapeLineStyle.Single,
124                 FillColor = Color.Red,
125                 StrokeColor = Color.Red,
126                 StrokeWeight = 1,
127             });    
128 
129             //保存并打开文档
130             doc.SaveToFile("InsertShapegroups.docx", FileFormat.Docx2010);
131             System.Diagnostics.Process.Start("InsertShapegroups.docx");
132         }
133     }
134 }

View Code

 

如上全体是有关Word中绘制图形形状的源委。如需转发,请申明出处!

多谢阅读!

示范代码(供参谋)

【示例4】替换现存书签内容

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

手续 2 :加载文档

Document document = new Document();
document.LoadFromFile("test.docx");

手续 3 :增加段落及文字

Section sec = document.AddSection();//添加section
sec.AddParagraph().AppendText("Welcome Back, n My Friend!"); //添加段落到section,并添加字符串内容

手续 4 :获取段落

//获取段落内容 
ParagraphBase firstReplacementParagraph = sec.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase lastReplacementParagraph = sec.Paragraphs[sec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
//实例化类TextBodySelection和TextBodyPart
TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);
TextBodyPart part = new TextBodyPart(selection);

手续 5 :替换书签内容

BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);//实例化BookmarksNavigator类 
bookmarkNavigator.MoveToBookmark("bookmark1", true, true);//定位到书签“bookmark1”所在段落的位置
bookmarkNavigator.DeleteBookmarkContent(true);//删除原有书签位置的内容
bookmarkNavigator.ReplaceBookmarkContent(part, true, true);//用新添加段落的内容替换掉原书签的内容并保留格式
document.Sections.Remove(sec); //移除section

手续 6 :保存文书档案

//保存文档并打开
document.SaveToFile("替换书签.docx");
System.Diagnostics.Process.Start("替换书签.docx");

 

轮换效果:

分分快三计划 10

全总代码:

分分快三计划 11分分快三计划 12

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace EditOrReplaceBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document类实例,加载文档 
            Document document = new Document();
            document.LoadFromFile("test.docx");

            Section sec = document.AddSection();//添加section
            sec.AddParagraph().AppendText("Welcome Back, n My Friend!"); //添加段落到section,并添加字符串内容

            //获取段落内容 
            ParagraphBase firstReplacementParagraph = sec.Paragraphs[0].Items.FirstItem as ParagraphBase;
            ParagraphBase lastReplacementParagraph = sec.Paragraphs[sec.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
            //实例化类TextBodySelection和TextBodyPart
            TextBodySelection selection = new TextBodySelection(firstReplacementParagraph, lastReplacementParagraph);
            TextBodyPart part = new TextBodyPart(selection);

            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);//实例化BookmarksNavigator类 
            bookmarkNavigator.MoveToBookmark("bookmark1", true, true);//定位到书签“bookmark1”所在段落的位置
            bookmarkNavigator.DeleteBookmarkContent(true);//删除原有书签位置的内容
            bookmarkNavigator.ReplaceBookmarkContent(part, true, true);//用新添加段落的内容替换掉原书签的内容并保留格式            
            document.Sections.Remove(sec); //移除section

            //保存文档并打开
            document.SaveToFile("替换书签.docx");
            System.Diagnostics.Process.Start("替换书签.docx");
        }
    }
}

View Code

 

如上是此番关于C# 操作Word书签成效的补偿介绍,如需转发,请申明出处。

(本文完)

 

一、序言

在Office Word中,支持在Word文书档案中插入类型极度丰硕的形态,包含线条、矩形、基本造型(诸如圆形、多边形、星形、括号、笑貌等等图形)、箭头形状、公式形状、流程图、旗帜图形、标记图形等等,大家在编制程序进度中,想要在Word中绘制分歧体系的图纸,能够因而类库来操作。控件Spire.Doc for .NET 6.0及以上版本初步援助Office Word中的全体图形,能够透过代码操作某些单一的造型,也足以因而将单纯形状实行结合来收获想要的图样或形状效果,当然,也援助自身自定义图形,通过编制程序绘制也是足以的。上面将介绍向Word绘制形状和整合形象的措施,方法中的代码供参谋。

PS:

  • Spire.Doc for .NET赢得地址
  • 设置后,dll文件可在安装路线下的Bin文件夹中收获

Dll引用

分分快三计划 13

【示例2】编辑Word中的OLE

 (这里以上文中生成的文书档案为测量试验文件)

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.IO;

步骤 2 :加载文书档案

//实例化一个Document对象,加载含有OLE的Word文档
Document doc = new Document();
doc.LoadFromFile("test.docx");

手续 3 :获取section中的全数OLE,遵照要求来更动OLE的花色和链接对象

//获取第一个Section
Section sec = doc.Sections[0];

//遍历这个Section中的所有子元素,找到段落下的OLE对象
foreach (DocumentObject obj in sec.Body.ChildObjects)
{
    if (obj is Paragraph)
    {
        Paragraph par = obj as Paragraph;
        foreach (DocumentObject paraObj in par.ChildObjects)
        {
            //找到OLE对象,根据类型来进行更改操作
            if (paraObj.DocumentObjectType == DocumentObjectType.OleObject)
            {
                DocOleObject Ole = paraObj as DocOleObject;
                //如果是链接, 修改对象的链接路径
                if (Ole.LinkType == OleLinkType.Link)
                {
                    //同时还要手动去更改OLE的图片
                    DocPicture pic = Ole.OlePicture;
                    pic.LoadImage(Image.FromFile("Img.png"));
                    Ole.LinkPath = @"sample.docx";
                }
                //如果是嵌入,更改数据即可
                byte[] bys = File.ReadAllBytes(@"sample.docx");
                if (Ole.LinkType == OleLinkType.Embed)
                {
                    DocPicture pic = new DocPicture(doc);
                    pic.LoadImage(Image.FromFile(@"Img.png"));
                    Ole.ObjectType = "Word.Document.12";
                    Ole.SetOlePicture(pic);
                    Ole.SetNativeData(bys);
                }
            }
        }
    }
}

步骤 4 :保存文书档案

 //保存修改后的文档,并打开
 doc.SaveToFile("修改OLE.docx", Spire.Doc.FileFormat.Docx2010);
 System.Diagnostics.Process.Start("修改OLE.docx");

调治运维程序后,生成文书档案。展开文档时,生成的文书档案中原来的插入OLE的图样和链接的文档都早就转移了,如下所示:

 分分快三计划 14

方方面面代码:

分分快三计划 15分分快三计划 16

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.IO;

namespace EditOLE_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document对象,加载含有OLE的Word文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //获取第一个Section
            Section sec = doc.Sections[0];

            //遍历这个Section中的所有子元素,找到段落下的OLE对象
            foreach (DocumentObject obj in sec.Body.ChildObjects)
            {
                if (obj is Paragraph)
                {
                    Paragraph par = obj as Paragraph;
                    foreach (DocumentObject paraObj in par.ChildObjects)
                    {
                        //找到OLE对象,根据类型来进行更改操作
                        if (paraObj.DocumentObjectType == DocumentObjectType.OleObject)
                        {
                            DocOleObject Ole = paraObj as DocOleObject;
                            //如果是链接, 修改对象的链接路径
                            if (Ole.LinkType == OleLinkType.Link)
                            {
                                //同时还要手动去更改OLE的图片
                                DocPicture pic = Ole.OlePicture;
                                pic.LoadImage(Image.FromFile("Img.png"));
                                Ole.LinkPath = @"sample.docx";
                            }
                            //如果是嵌入,更改数据即可
                            byte[] bys = File.ReadAllBytes(@"sample.docx");
                            if (Ole.LinkType == OleLinkType.Embed)
                            {
                                DocPicture pic = new DocPicture(doc);
                                pic.LoadImage(Image.FromFile(@"Img.png"));
                                Ole.ObjectType = "Word.Document.12";
                                Ole.SetOlePicture(pic);
                                Ole.SetNativeData(bys);
                            }
                        }
                    }
                }
            }
            //保存修改后的文档,并打开
            doc.SaveToFile("修改OLE.docx", Spire.Doc.FileFormat.Docx2010);
            System.Diagnostics.Process.Start("修改OLE.docx");
        }
    }
}

View Code

概要

书签的设置能够扶持我们快速的一定某段文字,使用起来很有益,也很节省时间。在前一篇小说“C# 怎样增加/删除Word书签”中牵线了插入、删除书签的办法,本篇作品将对C# 操作Word书签的功能做进一步介绍。示例内容将包罗以下要点:

  • 在存活书签地点插入图片
  • 在存活书签地方插入表格
  • 读取书签地方的文书档案内容
  • 轮换现存书签地方内容

 

二、代码示例

概述

OLE,Object Linking and Embedding,即对象连接与嵌入。大家在陈设程序时,OLE能够用来创设复合文书档案,把文字、声音、图像、表格、应用程序等品种的信息整合在一道,在Word中,大家得以由此OLE来落到实处以上因素新闻的三结合。上面包车型大巴示范司令员介绍如何通过C# 来操作Word中的OLE,示例内容包涵以下要点:

  • 插入OLE到Word
  • 编辑Word中的OLE
  • 读取Word中的OLE

 

 【示例 2】在存活书签地方插入表格

步骤 1:添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using System.Data;

手续 2 :创立文书档案,加载文件

Document doc = new Document();
doc.LoadFromFile("test.docx");

手续 3 :创立表格

//创建Table对象
Table table = new Table(doc, true);

//创建模拟数据
DataTable dt = new DataTable();
dt.Columns.Add("PI", typeof(string));
dt.Columns.Add("Type", typeof(string));
dt.Columns.Add("Nov.", typeof(string));
dt.Columns.Add("YTD", typeof(string));                     
dt.Rows.Add(new string[] { "PI","Type", "Nov.", "YTD" });
dt.Rows.Add(new string[] { "1", "BAIC EC-Series", "15.719", "64.910" });
dt.Rows.Add(new string[] { "2", "Tesla Model S", "3.889", "46.951" });
dt.Rows.Add(new string[] { "3", "Nissan Leaf", "2.817", "46.423" });

//将数据填充至表格
table.ResetCells(dt.Rows.Count, dt.Columns.Count);
for (int i = 0; i < dt.Rows.Count; i  )
{
    for (int j = 0; j < dt.Columns.Count; j  )
    {
        table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
    }
}

手续 4 :插入表格到书签地点

//获取指定书签位置
BookmarksNavigator navigator = new BookmarksNavigator(doc);
navigator.MoveToBookmark("origin2");

//将表格添加至TextBodyPart
TextBodyPart part = navigator.GetBookmarkContent();
part.BodyItems.Add(table);
//替换原有书签内容
navigator.ReplaceBookmarkContent(part);

手续 5 :保存文件

//保存并打开文件
doc.SaveToFile("output.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("output.docx");

 

 表格插入效果:

分分快三计划 17

全总代码:

分分快三计划 18分分快三计划 19

using Spire.Doc;
using Spire.Doc.Documents;
using System.Data;

namespace InsertTableToBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建文档,加载文件
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //创建Table对象
            Table table = new Table(doc, true);

            //创建模拟数据
            DataTable dt = new DataTable();
            dt.Columns.Add("PI", typeof(string));
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("Nov.", typeof(string));
            dt.Columns.Add("YTD", typeof(string));                     
            dt.Rows.Add(new string[] { "PI","Type", "Nov.", "YTD" });
            dt.Rows.Add(new string[] { "1", "BAIC EC-Series", "15.719", "64.910" });
            dt.Rows.Add(new string[] { "2", "Tesla Model S", "3.889", "46.951" });
            dt.Rows.Add(new string[] { "3", "Nissan Leaf", "2.817", "46.423" });

            //将数据填充至表格
            table.ResetCells(dt.Rows.Count, dt.Columns.Count);
            for (int i = 0; i < dt.Rows.Count; i  )
            {
                for (int j = 0; j < dt.Columns.Count; j  )
                {
                    table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
                }
            }

            //获取指定书签位置
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark("origin2");

            //将表格添加至TextBodyPart
            TextBodyPart part = navigator.GetBookmarkContent();
            part.BodyItems.Add(table);

            //替换书签内容
            navigator.ReplaceBookmarkContent(part);

            //保存并打开文件
            doc.SaveToFile("output.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("output.docx");
        }
    }
}

View Code

 

(一)绘制单一形状

手续1:增添如下using钦命

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

 

手续2:创设示范,增加section、paragraph

//创建一个Document实例
Document doc = new Document();
//添加一个section paragraph
 Section sec = doc.AddSection();
 Paragraph para1 = sec.AddParagraph();

 

手续3:在文书档案钦点位置插入形状,并安装形状类型、大小、填充颜色、线条样式等

(这里大致列举多少个样子的丰硕方法,方法相比简单,不做赘述,效果图中罗列了有的模样样式,必要任何样式的样子可自动安装加多)

           //插入一个矩形
            ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Rectangle);
            shape1.FillColor = Color.Blue;
            shape1.StrokeColor = Color.LightSkyBlue;
            shape1.HorizontalPosition = 20;
            shape1.VerticalPosition = 20;

            //插入一个圆形
            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;

            //插入一个公式符号  
            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:保存文书档案

//保存并打开文档
doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("InsertShapes.docx");

 

形状增加效果:

分分快三计划 20

整个代码:

分分快三计划 21分分快三计划 22

  1 using Spire.Doc;
  2 using Spire.Doc.Documents;
  3 using Spire.Doc.Fields;
  4 using System.Drawing;
  5 
  6 namespace AddShapes_Doc
  7 {
  8     class Program
  9     {
 10         static void Main(string[] args)
 11         {
 12             //创建一个Document实例
 13             Document doc = new Document();
 14 
 15             //添加一个section paragraph
 16             Section sec = doc.AddSection();
 17             Paragraph para1 = sec.AddParagraph();
 18 
 19             //插入一个矩形
 20             ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Rectangle);
 21             shape1.FillColor = Color.Blue;
 22             shape1.StrokeColor = Color.LightSkyBlue;
 23             shape1.HorizontalPosition = 20;
 24             shape1.VerticalPosition = 20;
 25 
 26             //插入一个圆形
 27             ShapeObject shape2 = para1.AppendShape(50, 50, ShapeType.Ellipse);
 28             shape2.FillColor = Color.Purple;
 29             shape2.StrokeColor = Color.LightPink;
 30             shape2.LineStyle = ShapeLineStyle.Single;
 31             shape2.StrokeWeight = 1;
 32             shape2.HorizontalPosition = 80;
 33             shape2.VerticalPosition = 20;
 34 
 35             //插入一个公式符号  
 36             ShapeObject shape3 = para1.AppendShape(50, 50, ShapeType.Plus);
 37             shape3.FillColor = Color.DarkCyan;
 38             shape3.StrokeColor = Color.LightGreen;
 39             shape3.LineStyle = ShapeLineStyle.Single;
 40             shape3.StrokeWeight = 1;
 41             shape3.HorizontalPosition = 140;
 42             shape3.VerticalPosition = 20;
 43 
 44             //插入一颗星形
 45             ShapeObject shape4 = para1.AppendShape(50, 50, ShapeType.Star);
 46             shape4.FillColor = Color.Red;
 47             shape4.StrokeColor = Color.Gold;
 48             shape4.LineStyle = ShapeLineStyle.Single;
 49             shape4.HorizontalPosition = 200;
 50             shape4.VerticalPosition = 20;
 51 
 52             //插入一个立方体
 53             ShapeObject shape5 = para1.AppendShape(50, 50, ShapeType.Cube);
 54             shape5.FillColor = Color.OrangeRed;
 55             shape5.StrokeColor = Color.Orange;
 56             shape5.LineStyle = ShapeLineStyle.Single;
 57             shape5.HorizontalPosition = 260;
 58             shape5.VerticalPosition = 20;
 59 
 60             //插入一个圆柱体
 61             ShapeObject shape6 = para1.AppendShape(50, 50, ShapeType.Can);
 62             shape6.FillColor = Color.Goldenrod;
 63             shape6.StrokeColor = Color.Gold;
 64             shape6.LineStyle = ShapeLineStyle.Single;
 65             shape6.HorizontalPosition = 320;
 66             shape6.VerticalPosition = 20;
 67 
 68             //插入一个箭头
 69             ShapeObject shape7 = para1.AppendShape(50, 50, ShapeType.Arrow);
 70             shape7.FillColor = Color.Yellow;
 71             shape7.StrokeColor = Color.Yellow;
 72             shape7.LineStyle = ShapeLineStyle.Single;
 73             shape7.HorizontalPosition = 20;
 74             shape7.VerticalPosition = 80;
 75 
 76             //插入一个v形臂章图形
 77             ShapeObject shape8 = para1.AppendShape(50, 50, ShapeType.Chevron);
 78             shape8.FillColor = Color.YellowGreen;
 79             shape8.StrokeColor = Color.Yellow;
 80             shape8.LineStyle = ShapeLineStyle.Single;
 81             shape8.HorizontalPosition = 80;
 82             shape8.VerticalPosition = 80;
 83 
 84             //插入一个循环箭头图形
 85             ShapeObject shape9 = para1.AppendShape(50, 50, ShapeType.CircularArrow);
 86             shape9.FillColor = Color.Green;
 87             shape9.StrokeColor = Color.Yellow;
 88             shape9.LineStyle = ShapeLineStyle.Single;
 89             shape9.HorizontalPosition = 140;
 90             shape9.VerticalPosition = 80;
 91 
 92             //插入一个云图形
 93             ShapeObject shape10 = para1.AppendShape(50, 50, ShapeType.CloudCallout);
 94             shape10.FillColor = Color.LightSkyBlue;
 95             shape10.StrokeColor = Color.White;
 96             shape10.LineStyle = ShapeLineStyle.Single;
 97             shape10.HorizontalPosition = 200;
 98             shape10.VerticalPosition = 80;
 99 
100             //插入一个环形图
101             ShapeObject shape11 = para1.AppendShape(50, 50, ShapeType.Donut);
102             shape11.FillColor = Color.Pink;
103             shape11.StrokeColor = Color.White;
104             shape11.LineStyle = ShapeLineStyle.Single;
105             shape11.HorizontalPosition = 260;
106             shape11.VerticalPosition = 80;
107 
108             //插入一个波浪形状图
109             ShapeObject shape12 = para1.AppendShape(50, 50, ShapeType.DoubleWave);
110             shape12.FillColor = Color.Plum;
111             shape12.StrokeColor = Color.White;
112             shape12.LineStyle = ShapeLineStyle.Single;
113             shape12.HorizontalPosition = 320;
114             shape12.VerticalPosition = 80;
115 
116             //插入一个礼结状图形
117             ShapeObject shape13 = para1.AppendShape(50, 50, ShapeType.EllipseRibbon);
118             shape13.FillColor = Color.RosyBrown;
119             shape13.StrokeColor = Color.White;
120             shape13.LineStyle = ShapeLineStyle.Single;
121             shape13.HorizontalPosition = 20;
122             shape13.VerticalPosition = 140;
123 
124             //插入一个心形图
125             ShapeObject shape14 = para1.AppendShape(50, 50, ShapeType.Heart);
126             shape14.FillColor = Color.Red;
127             shape14.StrokeColor = Color.White;
128             shape14.LineStyle = ShapeLineStyle.Single;
129             shape14.HorizontalPosition = 80;
130             shape14.VerticalPosition = 140;
131 
132             //插入一个六边形图形
133             ShapeObject shape15 = para1.AppendShape(50, 50, ShapeType.Hexagon);
134             shape15.FillColor = Color.DarkCyan;
135             shape15.StrokeColor = Color.White;
136             shape15.LineStyle = ShapeLineStyle.Single;
137             shape15.HorizontalPosition = 140;
138             shape15.VerticalPosition = 140;
139 
140             //插入一个不规则图形
141             ShapeObject shape16 = para1.AppendShape(50, 50, ShapeType.IrregularSeal1);
142             shape16.FillColor = Color.DeepPink;
143             shape16.StrokeColor = Color.White;
144             shape16.LineStyle = ShapeLineStyle.Single;
145             shape16.HorizontalPosition = 200;
146             shape16.VerticalPosition = 140;
147 
148             //插入一个月亮形状
149             ShapeObject shape17 = para1.AppendShape(50, 50, ShapeType.Moon);
150             shape17.FillColor = Color.Violet;
151             shape17.StrokeColor = Color.White;
152             shape17.LineStyle = ShapeLineStyle.Single;
153             shape17.HorizontalPosition = 260;
154             shape17.VerticalPosition = 140;
155 
156             //插入一个"禁止"形状
157             ShapeObject shape18 = para1.AppendShape(50, 50, ShapeType.NoSmoking);
158             shape18.FillColor = Color.Yellow;
159             shape18.StrokeColor = Color.Goldenrod;
160             shape18.LineStyle = ShapeLineStyle.Single;
161             shape18.HorizontalPosition = 320;
162             shape18.VerticalPosition = 140;
163 
164             //保存并打开文档
165             doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
166             System.Diagnostics.Process.Start("InsertShapes.docx");
167         }
168     }
169 }

View Code

 

【示例1】插入OLE到Word

步骤 1 :添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

步骤 2 :创设文书档案

//实例化一个Document类对象
Document doc = new Document();
//向文档中添加一个Section对象,并添加段落
Section sec = doc.AddSection();
Paragraph p = sec.AddParagraph();

步骤 3 :加载图片

//实例化一个DocPicture类对象,加载图片
DocPicture picture = new DocPicture(doc);
Image image = Image.FromFile(@"chart1.png");
picture.LoadImage(image);

步骤 4 :插入OLE

//在文档中插入一个工作表, OleLinkType 枚举值控制该OLE是链接还是嵌入
DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Link);
//DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Embed);

步骤 5 :保存文书档案

//保存并打开文档
doc.SaveToFile("添加OLE.docx");
System.Diagnostics.Process.Start("添加OLE.docx");

成功代码后,调节和测量检验运转程序,生成文书档案。

测量试验结果,如下图所示:

分分快三计划 23

方方面面代码:

分分快三计划 24分分快三计划 25

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertOLE_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document类对象
            Document doc = new Document();
            //向文档中添加一个Section对象,并添加段落
            Section sec = doc.AddSection();
            Paragraph p = sec.AddParagraph();

            //实例化一个DocPicture类对象,加载图片
            DocPicture picture = new DocPicture(doc);
            Image image = Image.FromFile(@"chart1.png");
            picture.LoadImage(image);

            //在文档中插入一个工作表, OleLinkType 枚举值控制该OLE是链接还是嵌入         
            DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Link);
            //DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Embed);

            //保存并打开文档
            doc.SaveToFile("添加OLE.docx");
            System.Diagnostics.Process.Start("添加OLE.docx");
        }
    }
}

View Code

 

 

【示例1】 在存活书签地点插入图片

步骤 1: 添加using指令

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

步骤 2 :主要代码段

//创建实例,加载文档
Document document = new Document();
document.LoadFromFile("test.docx");

//实例化BookmarksNavigator类,指定需要添加图片的书签“Bookmark1”
BookmarksNavigator bn = new BookmarksNavigator(document);
bn.MoveToBookmark("Bookmark1", true, true);

//添加段落,加载图片并插入到段落
Section section0 = document.AddSection();
Paragraph paragraph = section0.AddParagraph();
Image image = Image.FromFile("img.png");
DocPicture picture = paragraph.AppendPicture(image);
bn.InsertParagraph(paragraph);
document.Sections.Remove(section0);

//保存文档并打开
string output = "result.docx";
document.SaveToFile(output, FileFormat.Docx);
System.Diagnostics.Process.Start(output);

图表插入效果:

 分分快三计划 26

成套代码:

分分快三计划 27分分快三计划 28

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertImgToBookmark_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建实例,加载文档
            Document document = new Document();
            document.LoadFromFile("test.docx");

            //实例化BookmarksNavigator类,指定需要添加图片的书签“Bookmark1”
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("Bookmark1", true, true);

            //添加段落,加载图片并插入到段落
            Section section0 = document.AddSection();
            Paragraph paragraph = section0.AddParagraph();
            Image image = Image.FromFile("img.png");
            DocPicture picture = paragraph.AppendPicture(image);
            bn.InsertParagraph(paragraph);
            document.Sections.Remove(section0);

            //保存文档并打开
            string output = "result.docx";
            document.SaveToFile(output, FileFormat.Docx);
            System.Diagnostics.Process.Start(output);
        }
    }
}

View Code

 

行使工具

  •  Spire.Doc for .NET

:下载安装该类库,在编写代码时,注意在先后中加上援引Spire.Doc.dll(dll文件可在装置路线下的Bin文件夹中获得)

分分快三计划 29

行使工具

  • Spire.Doc for .NET 6.1
  • Visual Studio

ps:下载安装该类库后,注意加多援引Spire.Doc.dl到花色前后相继,dll文件可在安装路线的Bin文件夹中赢得。

分分快三计划 30

示范代码(供参考)

测量检验文书档案如下:

分分快三计划 31

 

 

本文由分分快三计划发布,转载请注明来源

关键词: 分分快三计划