提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|使用教程|编辑:王香|2018-06-29 09:59:02.000|阅读 3967 次
概述:Spire.Doc的简单操作以及相关代码的部分整理。
#慧都22周年庆大促·界面/图表报表/文档/IDE/IOT/测试等千款热门软控件火热促销中>>
Spire.Doc基本操作,你会了吗?(文档管理控件技术交流:QQ群:767755948)
1. 问:如何从word文档中获取文本?
答:您可以调用方法method document.GetText()来执行此操作。完整代码:
Document document = new Document();
document.LoadFromFile(@"..\..\test.docx");
using (StreamWriter sw = File.CreateText("output.txt"))
{
sw.Write(document.GetText());
}
2. 问:如何插入具有指定高度和宽度的图像?
答:您可以设置DocPicture的属性高度和宽度来调整图像大小。完整代码:
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
Image image = Image.FromFile("image.jpg");
//specify the paragraph
Paragraph paragraph = document.Sections[0].Paragraphs[2];
DocPicture picture = paragraph.AppendPicture(image);
//resize the image
picture.Height = picture.Height * 0.8f;
picture.Width = picture.Width * 0.8f;
document.SaveToFile("result.docx", FileFormat.Docx);
3. 问:如何对齐word文档中的文本?
答:请设置段落的属性HorizontalAlignment以对齐文本。完整代码:
Document document = new Document();
document.LoadFromFile("sample.docx");
//set paragraph1 to align left
Paragraph paragraph1 = document.Sections[0].Paragraphs[0];
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left;
//set paragraph2 to align center
Paragraph paragraph2 = document.Sections[0].Paragraphs[1];
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
//set paragraph3 to align right
Paragraph paragraph3 = document.Sections[0].Paragraphs[2];
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Right;
document.SaveToFile("result.docx");
4. 问:如何更改现有书签上的文字?
答:您可以使用BookmarksNavigator来查找指定的书签。然后请调用ReplaceBookmarkContent方法替换书签上的文本。完整代码:
Document document = new Document();
document.LoadFromFile("sample.doc");
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark("mybookmark");
//replace text on bookmarks
bookmarkNavigator.ReplaceBookmarkContent("new context", false);
document.SaveToFile("result.doc", FileFormat.Doc);
5. 问:如何将word转换为html?
答:您可以使用指定的文件格式HTML调用SaveToFile方法将word文档转换为html。完整代码:
Document document = new Document();
document.LoadFromFile("sample.doc");
//save word document as html file
document.SaveToFile("result.html", FileFormat.Html);
document.Close();
6. 问:如何将html转换为word文档?
答:请调用LoadFromFile方法加载html文件。然后调用SaveToFile方法将html转换为word文档。完整代码:
Document document = new Document();
document.LoadFromFile("sample.html", FileFormat.Html, XHTMLValidationType.None);
//save html as word document
document.SaveToFile("result.doc");
document.Close();
7. 如何将word2007转换为word2003?
答:只需使用指定的文件格式doc调用SaveToFile方法即可将word2007转换为word2003。完整代码:
Document document = new Document("word2007.docx");
//convert word2007 to word2003
document.SaveToFile("word2003.doc", FileFormat.Doc);
document.Close();
8. 问:如何替换和删除Word文档中的页眉或页脚?
答:请使用Section获取页眉或页脚。您可以调用替换方法来替换标题并调用Clear方法以删除Word文档的页眉或页脚。
Document document = new Document();
Section section = document.AddSection();
//add a header
HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
TextRange text = headerParagraph.AppendText("Demo of Spire.Doc");
text.CharacterFormat.TextColor = Color.Blue;
document.SaveToFile("DocWithHeader.doc");
//replace the header
headerParagraph.Replace("Demo", "replaceText", true, true);
document.SaveToFile("DocHeaderReplace.doc");
document.LoadFromFile("DocWithHeader.doc");
//delete the heater
document.Sections[0].HeadersFooters.Header.Paragraphs.Clear();
document.SaveToFile("DocHeaderDelete.doc");
9. 问:如何合并word文档?
答:请调用克隆方法复制一节。然后调用Add方法将该部分的副本添加到指定的文档。完整代码:
Document document1 = new Document();
document1.LoadFromFile("merge1.docx");
Document document2 = new Document();
document2.LoadFromFile("merge2.docx");
//add sections from document1 to document2
foreach (Section sec in document2.Sections)
{
document1.Sections.Add(sec.Clone());
}
document1.SaveToFile("result.docx");
10. 问:如何遍历word文档中表格的单元格?
答:行是表中行的集合,单元是行中单元的集合。所以你可以用两个循环来遍历表格的单元格。完整代码:
Document document = new Document();
document.LoadFromFile("sample.docx");
Spire.Doc.Interface.ITable table = document.Sections[0].Tables[0];
int i=0;
//traverse the cells
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
i++;
}
}
11. 问:如何设置阴影文字?
答:您只需设置TextRange的属性IsShadow即可。完整代码:
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
TextRange HText = paragraph.AppendText("this is a test!");
//set the property IsShadow
HText.CharacterFormat.IsShadow = true;
HText.CharacterFormat.FontSize = 80;
document.SaveToFile("result.doc");
12. 问:如何在Word中插入行号?
答:您需要设置节的属性LineNumberingRestartMode,LineNumberingStep,LineNumberingStartValue以在Word文档中插入行号。完整代码:
Document document = new Document();
Section section = document.AddSection();
//insert line numbers
section.PageSetup.LineNumberingRestartMode = LineNumberingRestartMode.RestartPage;
section.PageSetup.LineNumberingStep = 1;
section.PageSetup.LineNumberingStartValue = 1;
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("As an independent Word .NET component, Spire.Doc for .NET doesn't need Microsoft Word to be installed on the machine. However, it can incorporate Microsoft Word document creation capabilities into any developers .NET applications.");
document.SaveToFile("result.doc");
13. 问:如何使图像周围的文字?
答:您需要设置图片的属性TextWrappingStyle和ShapeHorizontalAlignment。完整代码:
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
string str = "As an independent Word .NET component, Spire.Doc for .NET doesn't need Microsoft Word to be installed on the machine. However, it can incorporate Microsoft Word document creation capabilities into any developers.NET applications.As an independent Word .NET component, Spire.Doc for .NET doesn't need Microsoft Word to be installed on the machine. However, it can incorporate Microsoft Word document creation capabilities into any developers’.NET applications.";
paragraph.AppendText(str);
DocPicture picture = paragraph.AppendPicture(Image.FromFile("logo.png"));
picture.TextWrappingStyle = TextWrappingStyle.Tight;
picture.HorizontalAlignment = ShapeHorizontalAlignment.Center;
document.SaveToFile("result.doc");
14. 问:如何编辑Word文档中的现有表?
答:使用Section获取表格,您可以编辑单元格中的文本,并且可以将新行插入到表格中。完整代码:
Document doc = new Document("sample.docx");
Section section = doc.Sections[0];
ITable table = section.Tables[0];
//edit text in a cell
TableCell cell1 = table.Rows[1].Cells[1];
Paragraph p1 = cell1.Paragraphs[0];
p1.Text = "abc";
TableCell cell2 = table.Rows[1].Cells[2];
Paragraph p2 = cell2.Paragraphs[0];
p2.Items.Clear();
p2.AppendText("def");
TableCell cell3 = table.Rows[1].Cells[3];
Paragraph p3 = cell3.Paragraphs[0];
(p3.Items[0] as TextRange).Text = "hij";
//insert new row
TableRow newRow = table.AddRow(true, true);
foreach (TableCell cell in newRow.Cells)
{
cell.AddParagraph().AppendText("new row");
}
doc.SaveToFile("result.doc");
15. 问:如何设置超链接的格式不带下划线?
答:请设置超链接字段的textRange节点来格式化超链接。完整代码:
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
Field hyperlink = paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
TextRange text = hyperlink.NextSibling.NextSibling as TextRange;
text.CharacterFormat.Bold = true;
text.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
document.SaveToFile("result.doc");
16. 问:如何设置word文档只读?
答:请调用Protect方法设置ProtectionType。完整代码:
Document document = new Document();
document.LoadFromFile("sample.docx");
document.Protect(ProtectionType.AllowOnlyReading);
document.SaveToFile("result.doc");
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@hmdbvip.cn


Spire.Doc for WPF 是一款在WPF平台上操作的MS Word组件,可以轻松并专业地访问大量各种不同的word文档处理任务。
Spire.Doc for Silverlight是一款在Silverlight平台上操作的MS Word组件,让用户可以执行基本的文字处理任务。
本文主要介绍如何在MVVM应用程序中使用虚拟源,欢迎下载最新版组件体验!
Aspose.Slides for Java使用户能够轻松地操作幻灯片、添加注释和转换文件。其易用性和集成能力提高了工作效率,使开发人员能够专注于更关键的任务。
在嵌入式软件测试领域,对交叉编译代码进行单元测试是一大挑战。Parasoft C/C++test作为专业的C/C++测试工具,能够与劳特巴赫Trace32调试器深度集成。下面会详细介绍如何在C++test中配置Trace32调试器,实现对PowerPC架构程序的单元测试,涵盖环境设置、项目导入到测试执行的全过程。
本文将为大家介绍如何使用DevExpress WinForms数据网格控件实现摘要文本的格式化,欢迎下载最新版组件体验!
相关产品
Spire.Doc for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。
Spire.DocViewer for .NET强大的Word查看器,可以轻松实现Word文档显示、转换、交互
Spire.Doc for WPFSpire.Doc for WPF 是一款在WPF平台上操作的MS Word组件,可以轻松并专业地访问大量各种不同的word文档处理任务。
Spire.Doc for SilverlightSpire.Doc for Silverlight是一款在Silverlight平台上操作的MS Word组件,让用户可以执行基本的文字处理任务。
Spire.DocViewer for WPFSpire.DocViewer for WPF是一款独立的Word查看器WPF组件。
最新文章 MORE
永利最大(官方)网站相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@hmdbvip.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
永利最大(官方)网站