如何在c#docx文件中的图像中换行文本

时间:2013-02-26 09:20:00

标签: c# .net ms-word openxml docx

我正在使用Docx.dll创建一个docx生成器。到目前为止,我已经能够将图像和文本插入到文档中。图像和段落未对齐。我需要将图像包装成文本。我该怎么做? 我在谷歌找了它,发现了这个链接 Adding Images to Documents in Word 2007 by Using the Open XML SDK 2.0。代码正在工作并创建word文档,但docx文件没有打开。

如何在c#中包装文本'在文本前面'?

public static DocX CreateDocumentFile(List<CompanyInfo> info)
    {

        DocX document = DocX.Load(@"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\RetailWrite.docx");

        foreach (var companies in info)
        {

            Formatting fm = new Formatting();

            /*Inserting Image*/
            Novacode.Image img = document.AddImage(@"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\logos\slime.png");
            Novacode.Paragraph companyLogo = document.InsertParagraph("");
            Picture pic1 = img.CreatePicture();
            companyLogo.InsertPicture(pic1, 0);


            Novacode.Paragraph CompanyName = document.InsertParagraph(companies.Name.ToString());
            CompanyName.StyleName = "COMPANY";


            Novacode.Paragraph CompanyPosition = document.InsertParagraph(companies.Position.ToString());
            CompanyPosition.StyleName = "posit";


            Novacode.Paragraph CompanyDescription = document.InsertParagraph(companies.Description.ToString());
            CompanyDescription.StyleName = "descrip";

            Novacode.Paragraph blankPara = document.InsertParagraph(" ");
            Novacode.Paragraph blankPara2 = document.InsertParagraph(" ");
        }

        return document;
    }

1 个答案:

答案 0 :(得分:0)

问题的解决方案:我使用MS-Word的Interop在图像上应用自动换行。

public static void FormatImages()
    {
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        string filePath = @"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\Temp.docx";
        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(filePath, false);

        object save_changes = false;
        foreach (Microsoft.Office.Interop.Word.InlineShape item in wordApp.ActiveDocument.InlineShapes)
        {
            if (item != null)
            {
                if (item.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
                {
                    item.Select();
                    Microsoft.Office.Interop.Word.Shape shape = item.ConvertToShape();
                    shape.WrapFormat.Type = WdWrapType.wdWrapFront;
                }
            }
        }

        doc.SaveAs(@"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\RetailWrite.docx");
        doc.Close(save_changes);
        wordApp.Quit(save_changes);
        if (System.IO.File.Exists(@"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\Temp.docx"))
        {
            System.IO.File.Delete(@"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\Temp.docx");
        }
    }