2013-02-26 50 views
1

我正在使用Docx.dll一個DOCX發電機。到目前爲止,我已經能夠將圖像和文本插入到文檔中。圖像和段落不對齊。我需要將圖像換文。我該怎麼做? 我看着它在谷歌和發現這個鏈接 Adding Images to Documents in Word 2007 by Using the Open XML SDK 2.0。代碼工作並創建word文檔,但docx文件未打開。如何包裝形式在圖像中的docx文件在C#

我如何在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; 
    } 
+0

您能否寄出您正在使用的代碼。請閱讀常見問題解答,瞭解如何發佈問題。 – Shail 2013-02-26 09:39:58

+0

我已經添加了代碼。 – 2013-02-26 09:43:48

回答

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"); 
     } 
    } 
+0

這不是一個解決方案,而是一種解決方法。不希望在Web服務器上安裝Word時使用DocX和類似的庫。 – Sebazzz 2017-11-15 14:56:22

+0

@Sebazzz很久以前,我對IT界很陌生。對我來說,這是一個「解決方案」,它做了它應該做的事情:)但是,你正確使用Word可能會令服務器頭痛。我在爲辦公工具開發更多擴展程序時遇到了很大的麻煩。從來沒有再做過。可能是我不是一個好的程序員:) – 2017-11-16 10:48:37

相關問題