首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在c#中对docx文件中的图像自动换行

如何在c#中对docx文件中的图像自动换行
EN

Stack Overflow用户
提问于 2013-02-26 17:20:00
回答 1查看 4.3K关注 0票数 1

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

如何在c#中“在文本前面”换行?

代码语言:javascript
运行
复制
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;
    }
EN

回答 1

Stack Overflow用户

发布于 2013-04-12 18:31:15

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

代码语言:javascript
运行
复制
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");
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15085308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档