Word处理控件Spire.Doc热门问题解答

为方便使用者快速掌握和了解Spire.Doc,小编列举了Word处理控件Spire.Doc常见问题及解答欢迎大家留言评论!
猛戳此处→→<<Spire.Doc最新版下载>> 

(1)如何从word文档中获取文本?

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

Document document = new Document();
document.LoadFromFile(@"..\..\test.docx");
using (StreamWriter sw = File.CreateText("output.txt"))
{
sw.Write(document.GetText());
}

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

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

(3)如何对齐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");

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

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

(5)怎么把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();

(6)如何将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();

(7)如何将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();

(8)如何替换和删除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");

(9)如何合并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");

(10)如何浏览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++;
}
}

(11)如何设置带阴影的文本?

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

(11)如何在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");

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

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

(13)如何在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)
{

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

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

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

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

Document document = new Document();
document.LoadFromFile("sample.docx");
document.Protect(ProtectionType.AllowOnlyReading);
document.SaveToFile("result.doc");

若您还有其他疑问,可在评论区留言~~小编看到了第一时间给大家回复哟~~

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
免费Spire.Doc for .NET是e-iceblue公司推出的一款专门对Microsoft Word 文档进行操作的.NET类控件。这款控件的主要功能在于帮助开发人员轻松快捷地生成、编辑和查看Word文档。同时,开发人员还可以通过使用Spire.Doc for .NET 设置Word文档的格式,插入图片,表格,超链接等。Spire.Doc for .NET 最大的便利之处在于它不依赖于Microsoft Word以及任何其他第三方软件。只需将此款控件安装在您的电脑上,您就可以对word文档进行操作。此款控件支持所有的Word格式,即Word97,Word2003,Word2007, Word2010以及Word2013。 主要功能: • 可生成、打开、查看、编辑以及保存Word文档。 • 可对word文档进行加密、解密、设置权限已达到保护Word文档的效果。 • 可对页面进行设置,包括页眉、页脚的设计,插入分页符等。 • 可对文档格式进行设置,包括字体(大小、颜色等)和段落(行间距、项目符号等)的格式设置。 • 可在word文档中插入外部资源,如图片、超链接、水印、表格、批注等。 • 支持Word文档和其他文件格式间的转换。如Word to HTML/XML/PDF/EPub/RTF等。同时,其他格式也可以转换为Word,如HTML/XML/RTF/Text to WordWord不同版本间的格式也可相互转换,即Docx to Doc 或者Doc to Docx. • 支持邮件合并功能。开发人员可使用Spire.Doc for .NET提供的邮件合并功能轻松地完成对报表的批量设计及打印。
Free Spire.Doc 是一款免费的专门对 Word 文档进行操作的 .NET类库。适用于商业或个人用途。这款控件的主要功能在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。作为一款独立的 Word .NET 控件,能执行多种 Microsoft Word 文档处理任务的 .NET API。支持 Word97-2003,Word2007,Word2010 以及 Word2013。能在 Word 97/2003/2007/2010/2013 和 XML、RTF、TXT、XPS、EPUB、EMF、HTML 等格式文件之间进行双向转换,还能将 Word 文件高质量地转换为 PDF 文件格式。 主要功能如下: 1. 高质量的文档转换。Free Spire.Doc for .NET 能帮助用户将 Word 文件保存在流中,也可以保存为 Web response,还支持将 Word 文件与 XML、RTF、EMF、 TXT、XPS、EPUB、HTML 等格式文件之间的双向转换。同时,它还支持将 Word 文件转换为 PDF 文件,HTML 文件转换为图像文件。 2. 多样化的 Word 文档功能。支持动态创建一个全新的 Word 文档,并支持几乎所有的 Word 文档元素,它们主要包括页面、节、页眉、页脚、脚注、尾注、段落、项目符号和编号、表格、 文本、域、超链接、书签、注释、图片、样式、背景设置、打印功能、文档设置和文档保护。同时,也支持形状、文本框、图片、OLE 对象和内容控件。 3. 对已有的 Word 文档进行操作处理。支持搜索和替换、设置对齐方式、分页、分节、填充域、文档合并、复制、打印以及邮件合并等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值