We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Set "move object with text" property

Hi,
How can I set this property, as in the attached image?

Thanks.



Attachment: image_2a313e43.zip

3 Replies

DB Dilli Babu Nandha Gopal Syncfusion Team December 11, 2018 10:29 AM UTC

Hi Pio, 
 
Greetings from Syncfusion. 
 
Yes, you can set move object with text for picture, textbox and shapes in the Word document using VerticalOrigin API of DocIO. To enable the move object with text option in the Word document, we suggest you to set VerticalOrigin as paragraph and also set wrapping style (other than inline wrapping style) for the element.

Please refer the below example code snippet and let us know if it helps.:  
//Creates a Word document.  
WordDocument document = new WordDocument();  
//Adds a section and paragraph in the document.  
document.EnsureMinimal();  
  
//Appends picture in the paragraph.  
IWPicture picture = document.LastParagraph.AppendPicture(Image.FromFile("image.jpg"));  
//Sets vertical origin as paragraph to enable move with text property.  
picture.VerticalOrigin = VerticalOrigin.Paragraph;  
//Sets wrapping style for picture.  
picture.TextWrappingStyle = TextWrappingStyle.Square;  
  
//Appends shape in the paragraph.  
Shape shape = document.LastParagraph.AppendShape(AutoShapeType.Rectangle, 70, 50);  
//Sets vertical origin as paragraph to enable move with text property.  
shape.VerticalOrigin = VerticalOrigin.Paragraph;  
//Sets wrapping style for shape.  
shape.WrapFormat.TextWrappingStyle = TextWrappingStyle.Square;  
  
//Appends textbox in the paragraph.  
IWTextBox textBox = document.LastParagraph.AppendTextBox(70, 50);  
//Sets vertical origin as paragraph to enable move with text property.  
textBox.TextBoxFormat.VerticalOrigin = VerticalOrigin.Paragraph;  
//Sets wrapping style for textbox.  
textBox.TextBoxFormat.TextWrappingStyle = TextWrappingStyle.Square;  
  
//Saves the Word document.  
document.Save("Sample.docx");  
//Closes the Word document.  
document.Close();  

Please let us know if you have any questions. 
 
Regards, 
Dilli babu. 



PL Pio Luca Valvona December 11, 2018 02:24 PM UTC

Fine, but in the case of replace of a placeholder with an image using the following text?

 WordDocument document = new WordDocument("../../Data/Input.docx");
            WParagraph paragraph = new WParagraph(document);
            paragraph.AppendPicture(Image.FromFile("../../Data/input.jpg"));
            TextBodyPart bodyPart = new TextBodyPart(document);
            bodyPart.BodyItems.Add(paragraph);
            //Replaces a particular text with the text body part
            document.Replace("Replace", bodyPart, false, true, true);
            document.Save("Output.docx", FormatType.Docx);


DB Dilli Babu Nandha Gopal Syncfusion Team December 12, 2018 05:16 PM UTC

Hi Pio, 

Thank you for your update. 

Yes, VerticalOrigin property of Picture will be considered in Replace functionality which is illustrated in the following code example. 
 WordDocument document = new WordDocument(@"../../Data/Input.docx"); 
 WParagraph paragraph = new WParagraph(document); 
 IWPicture pic = paragraph.AppendPicture(Image.FromFile(@"../../Data/input.jpg")); 
 pic.VerticalOrigin = VerticalOrigin.Paragraph; 
 pic.TextWrappingStyle = TextWrappingStyle.Square;   
 TextBodyPart bodyPart = new TextBodyPart(document); 
 bodyPart.BodyItems.Add(paragraph); 
 //Replaces a particular text with the text body part 
 document.Replace("Replace", bodyPart, false, true, true); 
 document.Save(@"Output.docx", FormatType.Docx); 

Please let us know if your requirement is fulfilled else kindly share us the complete scenario so that we can provide a solution in-line with your requirement. 

Regards, 
Dilli babu. 


Loader.
Live Chat Icon For mobile
Up arrow icon