1

我正在使用 Docx.dll 制作一个 docx 生成器。到目前为止,我已经能够在文档中插入图像和文本。图像和段落未对齐。我需要将文本包装在图像中。我该怎么做?我在 google 中查找它并找到此链接 通过使用 Open XML SDK 2.0 在 Word 2007 中添加图像到文档。该代码也在工作并创建 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;
    }
4

1 回答 1

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");
        }
    }
于 2013-04-12T10:31:15.397 回答