2017-02-14 153 views
0

我使用GemBox.Document從模板生成輸出文檔。我想在TextBox中插入一個圖像,該圖像的大小與TextBox的大小相同。將圖像插入Word文檔TextBox

Word document with TextBox

我怎麼能這樣做?

DocumentModel document = DocumentModel.Load("mytemplate.dotx"); 
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First(); 
Picture picture = new Picture(document, "myimage.png"); 
textBox.Blocks.Add(new Paragraph(document, picture)); 

回答

1

嘗試以下操作:

DocumentModel document = DocumentModel.Load("mytemplate.dotx"); 
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First(); 

// If needed you can adjust the TextBox element's inner margin to your requirement. 
textBox.TextBoxFormat.InternalMargin = new Padding(0); 

// If needed you can remove any existing content from TextBox element. 
textBox.Blocks.Clear(); 

// Get TextBox element's size. 
var textBoxSize = textBox.Layout.Size; 

// Create and add Picture element. 
textBox.Blocks.Add(
    new Paragraph(document, 
     new Picture(document, "myimage.png", textBoxSize.Width, textBoxSize.Height))); 

我希望這有助於。

+0

謝謝,它的工作原理! –