1. 程式人生 > 實用技巧 >Spire.Doc基本操作

Spire.Doc基本操作

Spire.Doc基本操作,你會了嗎?

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文件中的文字?

答:請設定段落的屬性Horizo​​ntalAlignment以對齊文字。完整程式碼:

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和ShapeHorizo​​ntalAlignment。完整程式碼:

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