1

I am making a docx generator using Docx.dll. So far i have been able to insert images and text into the document. The images and paragraph are not aligned. I need to wrap text the image. How do i do it? I looked for it in google and found this link Adding Images to Documents in Word 2007 by Using the Open XML SDK 2.0. The code is working and creating the word document too, but the docx file is not opening.

How do i wrap text 'In Front Of Text' in 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;
    }
Newton Sheikh
  • 1,376
  • 2
  • 19
  • 42
  • Can you please post code you are working with. And Please read the FAQ , to learn how to post a question . – Shail Feb 26 '13 at 09:39

1 Answers1

0

Solution to the problem: I used the Interop of MS-Word to apply word-wrap across images.

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");
        }
    }
Newton Sheikh
  • 1,376
  • 2
  • 19
  • 42
  • This is not a solution, but a workaround. DocX and similar libraries are used when installing Word on the webserver is not desired. – Sebazzz Nov 15 '17 at 14:56
  • @Sebazzz It was long time back, i was new to IT world. For me it was a "solution", it did what it was supposed to do :) but yes u r right working with Word can be a headache on server. I had big trouble when i was working on more extensions for office tools. Never did it again. May be i am not a good programmer :) – Newton Sheikh Nov 16 '17 at 10:48