【FAQ】Word组件Spire.Doc 技术FAQ汇总 | 附下载

图片1

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

A:你可以调用方法document.GetText()。全部代码:

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

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

A:您可以设置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);

Q.如何在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");

Q.如何更改现有书签的文字?

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

Q.如何将Word转换为html?

A:您可以使用指定的文件格式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();

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

Q.如何将word2007转换为word2003?

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

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

//convert word2007 to word2003
document.SaveToFile("word2003.doc", FileFormat.Doc);
document.Close();

Q.如何在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");

Q.如何合并Word?

A:请调用克隆方法复制部分,然后调用方法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");

Q.如何遍历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++;
    }
}

Q.如何用阴影设置文字?

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

Q.如何在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");

Q.如何设置图像周围的文字?

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

Q.如何编辑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)
{
    cell.AddParagraph().AppendText("new row");
}
doc.SaveToFile("result.doc");

Q.如何设置无下划线的超链接格式?

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

Q.如何设置单词文档只读?

A:请调用“保护”方法设置ProtectionType。全部代码:

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

点击下载Spire.Doc

查看原文

转载于:https://my.oschina.net/u/3006003/blog/1544791

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值