原創(chuàng)|其它|編輯:王香|2017-09-28 15:10:49.000|閱讀 754 次
概述:Spire.Doc是專業(yè)的Word組件,可以幫助開發(fā)人員高效地開發(fā)創(chuàng)建、閱讀、編寫、轉(zhuǎn)換和打印任何Word文檔文件的功能,本文整理了Spire.Doc的技術(shù)FAQ。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Q.如何從word文檔中獲取文本?
A:你可以調(diào)用方法document.GetText()。全部代碼:
Document document = new Document(); document.LoadFromFile(@"..\..\test.docx"); using (StreamWriter sw = File.CreateText("output.txt")) { sw.Write(document.GetText()); }
Q.如何插入具有指定高度和寬度的圖像?
A:您可以設置DocPicture的屬性高度和寬度來調(diào)整圖像的大小。全部代碼:
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.如何更改現(xiàn)有書簽的文字?
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轉(zhuǎn)換為html?
A:您可以使用指定的文件格式HTML調(diào)用SaveToFile方法將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();
Q.如何將html轉(zhuǎn)換成word文檔?
A:請調(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();
Q.如何將word2007轉(zhuǎn)換為word2003?
A:只需使用指定的文件格式doc調(diào)用SaveToFile方法將word2007轉(zhuǎn)換為word2003。全部代碼:
Document document = new Document("word2007.docx"); //convert word2007 to word2003 document.SaveToFile("word2003.doc", FileFormat.Doc); document.Close();
Q.如何在word文檔中替換和刪除頁眉或頁腳?
A:請使用Section來獲取頁眉或頁腳。 并且您可以調(diào)用Replace替換頁眉并調(diào)用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:請調(diào)用克隆方法復制部分,然后調(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");
Q.如何遍歷Word文檔中的表單元格?
A:Rows是表中行的集合,Cells是行中的單元格集合。 因此,您可以使用兩個循環(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++; } }
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文檔中的現(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) { cell.AddParagraph().AppendText("new row"); } doc.SaveToFile("result.doc");
Q.如何設置無下劃線的超鏈接格式?
A:請將超鏈接字段的textRange節(jié)點設置為超鏈接格式。全部代碼:
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:請調(diào)用“保護”方法設置ProtectionType。全部代碼:
Document document = new Document(); document.LoadFromFile("sample.docx"); document.Protect(ProtectionType.AllowOnlyReading); document.SaveToFile("result.doc");
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn