Spire.Doc是专业的Word组件,可以帮助开发人员高效地开发创建、阅读、编写、转换和打印任何Word文档文件的功能,本文整理了Spire.Doc的技术FAQ。
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;
//