简体   繁体   English

在C#中的Word文档中创建和编辑文本

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

I have used itextsharp for PDF documents and now I need to create and add a text to a Word document(I'm using OpenXml SDK) so I would like to know what classes and objects are used here to add a paragraph or to set the alingment and indentation or to set the basefont and size of font. 我已经将itextsharp用于PDF文档,现在我需要创建文本并将其添加到Word文档中(我正在使用OpenXml SDK),所以我想知道在这里使用了哪些类和对象来添加段落或设置alingment和indentation或设置基本字体和字体大小。 For example, this is my code for creating PDF using iTextSharp and now I want to translate it to create Word: 例如,这是我使用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);

The best way to discover the things you ask about is to download the Open XML SDK Productivity Tool from Microsoft's site. 发现问题的最好方法是从Microsoft网站下载Open XML SDK生产率工具。 Create a small, sample document in Word (as a user), save it, close it, then open it in the Productivity Tool. (以用户身份)在Word中创建一个小的示例文档,将其保存,关闭,然后在“生产力工具”中将其打开。 That can show you both the underlying XML as well as standard code for generating the document. 这样可以向您显示基础XML以及用于生成文档的标准代码。 That way you can see what objects are used and how they're put together. 这样,您可以查看使用了哪些对象以及如何将它们组合在一起。

This is have I done it with DocX.dll: 这是我用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