翻譯|行業資訊|編輯:李顯亮|2020-01-09 09:48:55.563|閱讀 268 次
概述:你是否正在尋找.NET Word自動化解決方案 以在C#中生成和處理文字處理文檔?讓我們嘗試一下Aspose.Words for .NET API。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
你是否正在尋找.NET Word自動化解決方案 以在C#中生成和處理文字處理文檔?是否要以編程方式創建、閱讀、編輯、修改和轉換Word文檔而無需MS Office?并且,企業對MS Word文檔自動化和報告生成有很高的要求。
讓我們嘗試一下Aspose.Words for .NET API- 在基于.NET或.NET Core的應用程序中用于生成和處理Word文檔(.doc,.docx等)的完整功能集。通過接下來的介紹,你將了解到:
DocumentBuilder類包含從頭創建Word文檔的所有方法和屬性。與Document類結合使用時,DocumentBuilder支持插入元素,例如文本/段落,復選框,表格,列表,圖像以及Word文檔可以包含的其他對象。此外,您可以使用此類指定字體和其他格式選項。以下是使用DocumentBuilder類創建Word文檔的步驟。
下面的代碼示例演示如何使用C#格式的文本,表格和圖像創建Word DOCX文檔。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting Font font = builder.Font; font.Size = 32; font.Bold = true; font.Color = System.Drawing.Color.Black; font.Name = "Arial"; font.Underline = Underline.Single; // Insert text builder.Writeln("This is the first page."); builder.Writeln(); // Change formatting for next elements. font.Underline = Underline.None; font.Size = 10; font.Color = System.Drawing.Color.Blue; builder.Writeln("This following is a table"); // Insert a table Table table = builder.StartTable(); // Insert a cell builder.InsertCell(); // Use fixed column widths. table.AutoFit(AutoFitBehavior.AutoFitToContents); builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; builder.Write("This is row 1 cell 1"); // Insert a cell builder.InsertCell(); builder.Write("This is row 1 cell 2"); builder.EndRow(); builder.InsertCell(); builder.Write("This is row 2 cell 1"); builder.InsertCell(); builder.Write("This is row 2 cell 2"); builder.EndRow(); builder.EndTable(); builder.Writeln(); // Insert image builder.InsertImage("image.png"); // Insert page break builder.InsertBreak(BreakType.PageBreak); // all the elements after page break will be inserted to next page. // Save the document doc.Save("Document.docx");
效果展示:
假設我們需要在最近創建的文檔中更新文本“ This is first page。”。由于這是文檔第一部分中的第一段,因此我們可以通過指定第一部分和第一段的索引來訪問它。下面的代碼示例演示如何使用C#在Word文檔中編輯段落。
// Load document Document doc = new Document("Document.docx"); DocumentBuilder builder = new DocumentBuilder(doc); // Access the paragraph var paragraph=doc.Sections[0].Body.Paragraphs[0].Runs[0]; paragraph.Text = "This is updated text"; // Save the document doc.Save("Document_updated.docx");
更新后效果:
除了創建和處理Word文檔外,Aspose.Words for .NET還允許將文檔轉換為其他格式,包括(但不限于)PDF,XPS,EPUB,HTML和圖像格式,例如BMP,PNG或JPEG。下面的代碼示例演示如何在C#中將Word文檔轉換為PDF。
Document doc = new Document("word.docx"); // Provide PDFSaveOption compliance to PDF17 PdfSaveOptions options = new PdfSaveOptions(); options.Compliance = PdfCompliance.Pdf17; // Convert Word to PDF doc.Save("output.pdf", options);
通過將Word文檔的內容提取為純文本來解析Word文檔。下面的代碼示例演示如何從Word文檔中提取文本并將其保存到.txt文件中。
// Load the document from disk. Document doc = new Document("document.docx"); // Save as plain text doc.Save("output.txt");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn