簡體   English   中英

在C#中的Word文檔中創建和編輯文本

[英]Creating and editing text in Word document in C#

我已經將itextsharp用於PDF文檔,現在我需要創建文本並將其添加到Word文檔中(我正在使用OpenXml SDK),所以我想知道在這里使用了哪些類和對象來添加段落或設置alingment和indentation或設置基本字體和字體大小。 例如,這是我使用iTextSharp創建PDF的代碼,現在我想將其翻譯以創建Word:

 Document document = new Document(iTextSharp.text.PageSize.A4, 40, 40, 50, 50);
 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFile.FileName, FileMode.Create));
 document.Open();

 document.NewPage();
 Paragraph title = new Paragraph("aaa",titleFont);
 title.Alignment = Element.ALIGN_CENTER;
 document.Add(title);
 document.Add(Chunk.NEWLINE);

 Paragraph p1 = new Paragraph("",Font11);
 p1.IndentationLeft = 20f;
 document.Add(p1);

發現問題的最好方法是從Microsoft網站下載Open XML SDK生產率工具。 (以用戶身份)在Word中創建一個小的示例文檔,將其保存,關閉,然后在“生產力工具”中將其打開。 這樣可以向您顯示基礎XML以及用於生成文檔的標准代碼。 這樣,您可以查看使用了哪些對象以及如何將它們組合在一起。

這是我用DocX.dll完成的:

var document = DocX.Create(wordFile.FileName);

Novacode.Paragraph title = document.InsertParagraph("AAA", false, titleFormat);
title.Alignment = Alignment.center;
document.InsertParagraph(Environment.NewLine);

document.Save();
Process.Start("WINWORD.exe", wordFile.FileName);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM