简体   繁体   中英

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. For example, this is my code for creating PDF using iTextSharp and now I want to translate it to create 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. Create a small, sample document in Word (as a user), save it, close it, then open it in the Productivity Tool. That can show you both the underlying XML as well as standard code for generating the document. That way you can see what objects are used and how they're put together.

This is have I done it with 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM