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

Replace a string place holder in a word document with an Image using a given path ?

How to do it with c# ?

6 Replies

TE Testname April 27, 2016 08:03 AM UTC

This is what I have by now, but this will ignore my placeholders:

  TextSelection[] textSelections = document.FindAll(findeDas, false, false);

                if (textSelections != null)
                {
                    foreach (TextSelection textSelectionsItem in textSelections)
                    {
                        if (textSelectionsItem != null) //nichts gefunden, kein Platzhalter, bzw. der wurde schon ersetzt oder er kommt nicht im Dokument vor, aber als Property aus dem übergebenen Objekt
                        {
                            //Adding a new section to the document.
                            IWSection section = document.AddSection();

                            //Adding a paragraph to the section
                            IWParagraph paragraph = section.AddParagraph();

                            //Adding a new paragraph
                            paragraph = section.AddParagraph();
                            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                            //Inserting .png
                            WPicture picture = (WPicture)paragraph.AppendPicture(Image.FromFile(pfadFuerDieDatei));
                            picture.AddCaption("Bildddddd [.png Image]", CaptionNumberingFormat.Roman, CaptionPosition.AboveImage);

                            //Adding a new paragraph.
                            paragraph = section.AddParagraph();

                            //Alignment für das Bild
                            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                        }
                        else
                        {
                            Debug.WriteLine(findeDas + "nicht gefunden");
                        }
                    }
                }


SV Sarathkumar V Syncfusion Team April 28, 2016 10:05 AM UTC

Hi Customer,

Thanks you for using Syncfusion products.

On further analyzing your requirement, we found that you are trying to replace all the occurrences of a string with image in the Word document. You can directly use our Replace API to replace all the entries of the given string in the document with TextBodyPart. For your reference, please use the below modified code snippet to achieve your requirement and let us know if it helps.

Code Snippet:
            //Open the file using DocIO 
            WordDocument document = new WordDocument(fileName); 
            //Create a new paragraph   
            WParagraph paragraph = new WParagraph(document); 
            //Set the paragraph horizontal alignment 
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; 
            //Append picture to the paragraph  
            WPicture picture = (WPicture)paragraph.AppendPicture(Image.FromFile(pfadFuerDieDatei)); 
            picture.AddCaption("Bildddddd [.png Image]", CaptionNumberingFormat.Roman, CaptionPosition.AboveImage); 
            //Create new TextBodyPart and add the paragraph to it  
            TextBodyPart textBodyPart = new TextBodyPart(document); 
            //Add the paragraph to  
            textBodyPart.BodyItems.Add(paragraph); 
            //Replace the text with the TextBodyPart  
            document.Replace(findeDas, textBodyPart, false, true); 
            //Save and close the document  
            document.Save("result.docx", FormatType.Docx); 
            document.Close();

If we misunderstood any of your requirement then kindly elaborate the actual requirement with detailed description. Thereby we will analyse further on the mentioned case and will provided you appropriate solution.

Regards,
Sarath
 



TE Testname April 28, 2016 11:25 AM UTC

Yes, thats what I needed, but how should I adjust that example that preserves the space(below), so just lay the picture over the text, if there is any.The problem is that the newly inserted picture will destroy the layout


SV Sarathkumar V Syncfusion Team April 29, 2016 09:47 AM UTC

Hi Customer,

Thank you for your update.

You can add an empty paragraph next to paragraph where you have added the picture to preserve the space between the text and picture. For your reference, please refer the below modified code snippet and let us know if it helps.

Code snippet:
            //Open the file using DocIO  
            WordDocument document = new WordDocument(fileName); 
            //Create a new paragraph    
            WParagraph paragraph = new WParagraph(document); 
            //Set the paragraph horizontal alignment  
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; 
            //Append picture to the paragraph   
            WPicture picture = (WPicture)paragraph.AppendPicture(Image.FromFile(pfadFuerDieDatei)); 
            picture.AddCaption("Bildddddd [.png Image]", CaptionNumberingFormat.Roman, CaptionPosition.AboveImage); 
            //Create new TextBodyPart and add the paragraph to it   
            TextBodyPart textBodyPart = new TextBodyPart(document); 
            //Add the paragraph to text body part 
            textBodyPart.BodyItems.Add(paragraph); 
            //Adding a new paragraph. 
            paragraph = new WParagraph(document); 
            //Add the paragraph to text body part 
            textBodyPart.BodyItems.Add(paragraph); 
            //Replace the text with the TextBodyPart   
            document.Replace(findeDas, textBodyPart, false, true); 
            //Save and close the document   
            document.Save("result.docx", FormatType.Docx); 
            document.Close();

For more details about formatting of picture and paragraph, please refer the following online UG documentation.

Online UG Link:

http://help.syncfusion.com/file-formats/docio/working-with-paragraph#working-with-images
http://help.syncfusion.com/file-formats/docio/working-with-paragraph#applying-paragraph-formatting

If we misunderstood any of your requirement then kindly elaborate the actual requirement with detailed description. Thereby we will analyse further on the mentioned case and will provided you appropriate solution.

Regards,
Sarath 



SR Sérgio Rodrigues Costa July 19, 2016 07:50 PM UTC

Does it work in the same way in XLSIO?
Do you have a sample in VB.net?
Thanks!


AK Ayswarya Krishna Kumar Syncfusion Team July 20, 2016 12:31 PM UTC

Hi Customer,

Thank you for contacting Syncfusion support.

Regarding the question - How to achieve in XlsIO? 
XlsIO doesn’t have a direct method to replace a string with image. But this can be achieved through a workaround by finding the string and extract its ranges, then add picture to those ranges.  
The sample can be downloaded from the following link.
Code Snippet:  
//Extract ranges for the string and add image to the range  
Bitmap bm = new Bitmap(GetFullTemplatePath(value + ".jpg"));  
  
            if(IsStringExist(worksheet,value))  
            {  
                IRange[] ranges = worksheet.FindAll(value, ExcelFindType.Values);  
                if(ranges != null)  
                {  
                    foreach (IRange range in ranges)  
                    {  
                        range.Clear();  
                        worksheet.Pictures.AddPicture(range.Row, range.Column, GetFullTemplatePath(value + ".jpg"));  
                        range.RowHeight = bm.Height;  
                    }  
                }  
            }  
  
 
  
 
Sample link:  

Regarding Find and Replace functionality of DocIO:
Using DocIO the text from the Word document can be replaced with image. We have prepared a VB sample to meet your requirement. Please find the sample from the below link.
Sample link:
http://www.syncfusion.com/downloads/support/forum/123854/ze/GenerateWord-427509218.zip

Regards,
Ayswarya
 


Loader.
Up arrow icon