Word处理控件Spire.Doc常见问题解答

简介: 为方便使用者快速掌握和了解Spire.Doc,本文列举了Word处理控件Spire.Doc常见问题及解答欢迎下载体验!

为方便使用者快速掌握和了解Spire.Doc,本文列举了Word处理控件Spire.Doc常见问题及解答欢迎下载最新版体验!


如何从word文档中获取文本?

A:您可以调用方法方法 document.GetText() 来执行此操作。完整代码:

Document document = new Document();

document.LoadFromFile(@"..\..\test.docx");

using (StreamWriter sw = File.CreateText("output.txt"))

{

sw.Write(document.GetText());

}

如何插入具有指定高度和宽度的图像?

A : 您可以设置 DocPicture 的属性 height 和 width 来调整图像的大小。完整代码:

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);

如何对齐word文档中的文字?

A : 请设置段落的属性 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");

如何更改现有书签上的文本?

A : 您可以使用 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);

怎么把word转成html?

A : 你可以调用 SaveToFile 方法指定文件格式 HTML 来将 word 文档转换为 html。完整代码:

Document document = new Document();

document.LoadFromFile("sample.doc");


//save word document as html file

document.SaveToFile("result.html", FileFormat.Html);

document.Close();

如何将html转换为word文档?

A : 请调用 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();


如何将word2007转换为word2003?

A : 只需调用方法 SaveToFile 指定文件格式 doc 将 word2007 转换为 word2003。完整代码:

Document document = new Document("word2007.docx");


//convert word2007 to word2003

document.SaveToFile("word2003.doc", FileFormat.Doc);

document.Close();

如何替换和删除word文档中的页眉或页脚?

A : 请使用 Section 获取页眉或页脚。您可以调用方法 Replace 替换页眉,调用方法 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");

如何合并word文档?

A : 请调用方法 Clone 复制一个部分。然后调用方法 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");

如何浏览word文档中表格的单元格?

A : Rows 是表中行的集合,而 Cells 是行中单元格的集合。因此,您可以使用两个循环浏览表格的单元格。完整代码:

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++;

}

}

如何设置带阴影的文本?

A : 你只需要设置 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");

如何在Word中插入行号?

A : 你需要设置节的属性 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");

如何在图像周围制作文字?

A:需要设置图片的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");

如何在word文档中编辑现有表格?

A:使用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)

{

如何设置不带下划线的超链接格式?

A : 请设置超链接字段的 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");

如何将word文档设置为只读?

A : 请调用 Protect 方法设置 ProtectionType。完整代码:

Document document = new Document();

document.LoadFromFile("sample.docx");

document.Protect(ProtectionType.AllowOnlyReading);

document.SaveToFile("result.doc");

相关文章
|
XML 缓存 前端开发
Thymeleaf一篇就够了
Thymeleaf是Springboot官方支持的模板引擎,有着动静分离等独有特点,通过本文简单学习下吧!
65331 25
Thymeleaf一篇就够了
|
数据采集 并行计算 JavaScript
实战指南:在 Node.js 中利用多线程提升性能
在 Node.js 的世界中,多线程技术一直是一个受到广泛关注的领域。最初,Node.js 设计为单线程模式。随着技术发展,Node.js 引入了多线程支持,进而利用多核处理器的强大性能,提升了应用性能。接下来的内容将深入探讨 Node.js 如何实现多线程,以及在何种场合应该采用这种技术。
|
Web App开发 JavaScript 前端开发
网页VUE纯前端在线预览编辑Office,支持doc/docx、xls/xlsx、ppt/pptx、pdf等格式
随着互联网技术的不断发展,越来越多的企业开始采用在线办公模式,微软Office Word 是最好用的文档编辑工具,然而doc、docx、xls、xlsx、ppt、pptx等格式的Office文档是无法直接在浏览器中直接打开的,如果可以实现Web在线预览编辑OffIce,肯定会还带来了更高效、便捷的办公体验,为我们的工作带来了更多可能性。
4927 21
|
Web App开发 安全 前端开发
绿盟WEB服务扫描漏洞处理
文章目录 1、检测到目标X-Content-Type-Options响应头缺失 2、检测到目标X-XSS-Protection响应头缺失 3、检测到目标Content-Security-Policy响应头缺失 4、检测到目标Referrer-Policy响应头缺失 5、检测到目标X-Permitted-Cross-Domain-Policies响应头缺失 6、检测到目标X-Download-Options响应头缺失 7、点击劫持:X-Frame-Options未配置
5486 0
绿盟WEB服务扫描漏洞处理
|
算法 Windows
Winform控件优化之实现无锯齿的圆角窗体(或任意图形的无锯齿丝滑的窗体或控件)【借助LayeredWindow】
在一般能搜到的所有实现圆角窗体的示例中,都有着惨不忍睹的锯齿...而借助于Layered Windows,是可以实现丝滑无锯齿效果的Form窗体的,其具体原理就是分层窗体....
2587 0
Winform控件优化之实现无锯齿的圆角窗体(或任意图形的无锯齿丝滑的窗体或控件)【借助LayeredWindow】
|
11月前
|
数据可视化 知识图谱
LightRAG 实战: 基于 Ollama 搭建带知识图谱的可控 RAG 系统
LightRAG 是一款开源、模块化的检索增强生成(RAG)框架,支持快速构建基于知识图谱与向量检索的混合搜索系统。它兼容多种LLM与嵌入模型,如Ollama、Gemini等,提供灵活配置和本地部署能力,助力高效、准确的问答系统开发。
2592 2
LightRAG 实战: 基于 Ollama 搭建带知识图谱的可控 RAG 系统
|
XML Linux 区块链
Python提取Word表格数据教程(含.doc/.docx)
本文介绍了使用LibreOffice和python-docx库处理DOC文档表格的方法。首先需安装LibreOffice进行DOC到DOCX的格式转换,然后通过python-docx读取和修改表格数据。文中提供了详细的代码示例,包括格式转换函数、表格读取函数以及修改保存功能。该方法适用于Windows和Linux系统,解决了老旧DOC格式文档的处理难题,为需要处理历史文档的用户提供了实用解决方案。
1581 1
|
JSON Java API
实战 | JAVA读取WORD,包含表格。
我们有这样一个需求,需要抽取出WORD文档中的内容,然后组装成特定的json格式发送给第三方引擎接口
2381 0
|
缓存 前端开发 应用服务中间件
Nginx Windows详细安装部署教程
Nginx Windows详细安装部署教程
4784 0
Nginx Windows详细安装部署教程