Spire.Doc常見(jiàn)問(wèn)題解答
為方便用戶快速掌握和解Spire.Doc,本文列出了Word處理控件Spire.Doc??磫?wèn)題及解答歡迎下載最新版本體驗(yàn)!
技術(shù)交流群:767755948
如何從word文檔中獲取文檔?
A:您可以調(diào)用方法document.GetText()來(lái)執(zhí)行此操作。完整代碼:
文件文件=新文件(); 文檔.LoadFromFile(@"..\..\test.docx"); 使用 (StreamWriter sw = File.CreateText("output.txt")) { sw.Write(document.GetText()); }
如何插入工具具有指定的高度和寬度的圖像?
A : 您可以設(shè)置DocPicture的屬性高度和寬度來(lái)調(diào)整圖的大小。完整代碼:
文件文件=新文件(); document.LoadFromFile("sample.docx", FileFormat.Docx); 圖片 image = Image.FromFile("圖片.jpg"); //指定段落 段落 paragraph = document.Sections[0].Paragraphs[2]; DocPicture 圖片 = paragraph.AppendPicture(image); //調(diào)整圖像大小 圖片.Height = 圖片.Height * 0.8f; 圖片.寬度 = 圖片.寬度 * 0.8f; document.SaveToFile("result.docx", FileFormat.Docx);
如何對(duì)齊word文檔中的文字?
A : 請(qǐng)?jiān)O(shè)置段落的屬性 HorizontalAlignment 來(lái)對(duì)齊文本。完整代碼:
文件文件=新文件(); 文檔.LoadFromFile("sample.docx"); //設(shè)置段落1左對(duì)齊 段落 paragraph1 = document.Sections[0].Paragraphs[0]; paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left; //設(shè)置段落2居中 段落 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");
如何更改現(xiàn)有書簽上的文本?
A : 您可以使用 BookmarksNavigator 來(lái)定位指定的書簽。然后請(qǐng)調(diào)用方法 ReplaceBookmarkContent 來(lái)替換書簽上的文本。完整代碼:
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轉(zhuǎn)成html?
A : 你可以調(diào)用 SaveToFile 方法指定文件格式 HTML 來(lái)將 word 文檔轉(zhuǎn)換為 html。完整代碼:
Document document = new Document(); document.LoadFromFile("sample.doc"); //save word document as html file document.SaveToFile("result.html", FileFormat.Html); document.Close();
如何將html轉(zhuǎn)換為word文檔?
A : 請(qǐng)調(diào)用 LoadFromFile 方法加載 html 文件。然后調(diào)用方法 SaveToFile 將 html 轉(zhuǎn)換為 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轉(zhuǎn)換為word2003?
A : 只需調(diào)用方法 SaveToFile 指定文件格式 doc 將 word2007 轉(zhuǎn)換為 word2003。完整代碼:
Document document = new Document("word2007.docx"); //convert word2007 to word2003 document.SaveToFile("word2003.doc", FileFormat.Doc); document.Close();
如何替換和刪除word文檔中的頁(yè)眉或頁(yè)腳?
A : 請(qǐng)使用 Section 獲取頁(yè)眉或頁(yè)腳。您可以調(diào)用方法 Replace 替換頁(yè)眉,調(diào)用方法 Clear 刪除 word 文檔的頁(yè)眉或頁(yè)腳。
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 : 請(qǐng)調(diào)用方法 Clone 復(fù)制一個(gè)部分。然后調(diào)用方法 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 是行中單元格的集合。因此,您可以使用兩個(gè)循環(huán)瀏覽表格的單元格。完整代碼:
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++; } }
如何設(shè)置帶陰影的文本?
A : 你只需要設(shè)置 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中插入行號(hào)?
A : 你需要設(shè)置節(jié)的屬性 LineNumberingRestartMode, LineNumberingStep, LineNumberingStartValue 來(lái)在word文檔中插入行號(hào)。完整代碼:
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:需要設(shè)置圖片的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文檔中編輯現(xiàn)有表格?
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) {
如何設(shè)置不帶下劃線的超鏈接格式?
A : 請(qǐng)?jiān)O(shè)置超鏈接字段的 textRange 節(jié)點(diǎn)為超鏈接格式。完整代碼:
Document document = new Document(); Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph(); 字段超鏈接 = 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("結(jié)果.doc");
如何將word文檔設(shè)置為只讀?
A : 請(qǐng)調(diào)用Protect方法設(shè)置ProtectionType。完整代碼:
文件文件=新文件(); 文檔.LoadFromFile("sample.docx"); 文檔.Protect(ProtectionType.AllowOnlyReading); document.SaveToFile("結(jié)果.doc");更多E-iceblue產(chǎn)品體驗(yàn)