xceedsoftware / DocX

Fast and easy to use .NET library that creates or modifies Microsoft Word files without installing Word.
Other
1.78k stars 475 forks source link

Replace Tag to image not work #466

Open BenLampson opened 1 year ago

BenLampson commented 1 year ago
DocX document = DocX.Load(@"C:\Users\benla\Desktop\a1.docx");
{
    var x =  document.AddImage(@"D:\Codes\BDS\Src\BDS\Z.TestNewDataConverter\bin\Debug\net6.0-windows\tmp\a.png").CreatePicture();

    document.ReplaceTextWithObject("{{@circumferential_distance_img}}", x);
    var zxc = document.ParagraphsDeepSearch;
    // Save changes made to this document.
    //document.Save();
} // Release

image Nothing changed.

XceedBoucherS commented 1 year ago

Hi, With the Latest version of DocX (v2.5), you can do it this way: // Load a document. using( var document = DocX.Load( "ReplaceTextWithObjects.docx" ) ) { // Create the image from disk and set its size. var image = document.AddImage( "2018.jpg" ); var picture = image.CreatePicture( 175f, 325f );

// Do the replacement of all the found tags with the specified image and ignore the case when searching for the tags. document.ReplaceTextWithObject( new ObjectReplaceTextOptions() { SearchValue = "", NewObject = picture, RegExOptions = RegexOptions.IgnoreCase } );

// Save this document to disk. document.SaveAs( "ReplacedTextWithObjects.docx" ); }

Thanks