C# 如何使用OpenXML获取格式化word文档的内容?

C# 如何使用OpenXML获取格式化word文档的内容?,c#,itextsharp,openxml-sdk,C#,Itextsharp,Openxml Sdk,我使用以下代码读取word文档的内容,并使用iTextSharp库将内容保存到PDF文件中: WordprocessingDocument wordprocessing = WordprocessingDocument.Open(stream, false); Body body = wordprocessing.MainDocumentPart.Document.Body; using (var pdfDoc = new Document(PageSize.A4)) { var pdf

我使用以下代码读取word文档的内容,并使用iTextSharp库将内容保存到PDF文件中:

WordprocessingDocument wordprocessing = WordprocessingDocument.Open(stream, false);
Body body = wordprocessing.MainDocumentPart.Document.Body;
using (var pdfDoc = new Document(PageSize.A4))
{
    var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));
    pdfDoc.Open();
    var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\tahoma.ttf";
    var baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    var tahomaFont = new iTextSharp.text.Font(baseFont, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
    var ct = new ColumnText(pdfWriter.DirectContent) {RunDirection = PdfWriter.RUN_DIRECTION_RTL};
    ct.SetSimpleColumn(100, 100, 500, 800, 24, Element.ALIGN_RIGHT);
    var stringBuilder = new StringBuilder();
    foreach (var item in body)
    {
        stringBuilder.Append(item.InnerText);
    }
    var chunk = new Chunk(stringBuilder.ToString(), tahomaFont);
    ct.AddElement(chunk);
    ct.Go();
}
一切正常,但文档内容保存时没有任何格式。事实上,我想将文档文件及其格式转换为PDF文件。所以我的问题是,我怎样才能把内容和格式结合起来呢。正如我提到的,我想要同样的结果。
有什么想法吗?

您只使用body元素的innerText,而不使用它的其他属性。因此,显然,您不传输任何格式。你必须考虑所有的属性来保持格式。所以,我如何用格式传输内容?学习WordPuffice文档API以了解源对象中的所有格式化细节。学习iTextSharp API,了解如何将这些属性放入目标对象中。使生效根据您想要复制的详细信息,这可能需要花费相当多的时间来实现。