Add an image to a Word document such as drag and drop

I need to be able to insert an image into a Word document via my .Net (VB) VSTO Word Add.

The problem is that I need to insert it so that it ends up just as if the user were using Drag and Drop to insert an image instead of the Insert Picture button.

I usually do it like this:

wdRange.InlineShapes.AddPicture(imagePath, LinkToFile:=False, SaveWithDocument:=True)

      

But this is the same as the Insert Picture buttons.

The reason why I need to replicate the drag and drop method is because in some cases the images change in different ways.

I can replicate this by inserting a large colored rectangle in the header of the document and placing the image in the body of the document so that it sits on top of the colored rectangle. If the pasted image has transparency (like many logos), in some cases it will be a dark background rather than completely transparent. (I didn't find the exact situation needed to replicate the problem, but it seems to require a specific combination of printer drivers like PCL or PS.)

And just to make it difficult to debug this problem, it is not visible when printing to a PDF printer, it only appears on paper. And only in a few rare configurations. This can happen on both color and black and white printers, both new (<1 year old) and old printers.

A scanned example of the above script while printing, with two images positioned above a colored rectangle. One image (with a dark background) is inserted with the Insert Image button, and the other (with the correct transparency) by dragging. Both images are inserted using the same .png file. (One is slightly different from the other, but I tested thoroughly and scaling or moving the image did not affect the problem.) An example of a problem

I tried to record a macro while dragging an image, but nothing was recorded: - \

As an alternative solution, perhaps there is some property of the inserted image that can be changed to act like the one that was inserted using drag and drop? That would be perfectly acceptable!

Note. I am unable to replicate this issue to any of the 8 printers I have at home and only experience this issue when there are multiple (large) clients. Therefore, testing the proposed solution may take some time: - (

Update It turns out that I can only copy the image with the correct transparent background when using Drag and Drop from Windows 10 to Word 2016. Windows 7 generates a different result, so my previous tests (done on Windows 7) were invalid.

Two picture elements (one inserted through Insert Picture, one by dragging and dropping) look different when selected in Word. The wrong image ("Insert Image") has white selection handles as shown below:

enter image description here

The right image (Drag from Windows 10) has gray selection handles as shown below:

enter image description here

The image I used for tests is here ('Transparent.gif'. Same image file for both images)

enter image description here

I looked at the OpenXML markup for each image and they are very different. Wrong image ends up as element w:drawing

:

<w:drawing>
    <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251662336" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1">
        <wp:simplePos x="0" y="0"/>
        <wp:positionH relativeFrom="margin">
            <wp:align>left</wp:align>
        </wp:positionH>
        <wp:positionV relativeFrom="paragraph">
            <wp:posOffset>817245</wp:posOffset>
        </wp:positionV>
        <wp:extent cx="3048000" cy="647700"/>
        <wp:effectExtent l="0" t="0" r="0" b="0"/>
        <wp:wrapSquare wrapText="bothSides"/>
        <wp:docPr id="9" name="Billede 9"/>
        <wp:cNvGraphicFramePr>
            <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
        </wp:cNvGraphicFramePr>
        <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
            <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                    <pic:nvPicPr>
                        <pic:cNvPr id="9" name="Transparent.gif"/>
                        <pic:cNvPicPr/>
                    </pic:nvPicPr>
                    <pic:blipFill>
                        <a:blip r:embed="rId1">
                            <a:extLst>
                                <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                    <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
                                </a:ext>
                            </a:extLst>
                        </a:blip>
                        <a:stretch>
                            <a:fillRect/>
                        </a:stretch>
                    </pic:blipFill>
                    <pic:spPr>
                        <a:xfrm>
                            <a:off x="0" y="0"/>
                            <a:ext cx="3048000" cy="647700"/>
                        </a:xfrm>
                        <a:prstGeom prst="rect">
                            <a:avLst/>
                        </a:prstGeom>
                    </pic:spPr>
                </pic:pic>
            </a:graphicData>
        </a:graphic>
        <wp14:sizeRelH relativeFrom="page">
            <wp14:pctWidth>0</wp14:pctWidth>
        </wp14:sizeRelH>
        <wp14:sizeRelV relativeFrom="page">
            <wp14:pctHeight>0</wp14:pctHeight>
        </wp14:sizeRelV>
    </wp:anchor>
</w:drawing>

      

The correct image ends up v:shape

with much less markup:

<v:shape id="_x0000_s2049" type="#_x0000_t75" style="position:absolute;margin-left:.3pt;margin-top:0;width:240pt;height:51pt;z-index:251661312;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:bottom;mso-position-vertical-relative:text;mso-width-relative:page;mso-height-relative:page">
    <v:imagedata r:id="rId2" o:title="Transparent"/>
    <w10:wrap type="square"/>
</v:shape>

      

So far, I have not been able to recreate the latter via VBA or VB.NET (via VSTO AddIn).

If anyone can figure it out, I am convinced that the problem has been resolved.

Here's a link to a sample document I'm working with, the top image prints correctly, the bottom image prints with a darker background color (not mine on some printers, unfortunately). Simple-test.docx (Everything is placed in the title of the page, since according to my tester it might be a factor.)

+3


source to share


1 answer


To set <a:noFill/>

, you need to set the shape's fill properties. This should do something similar to the following inlShape

- this is the object set for your inserted image:

inlShape.Fill.ForeColor.RGB = -1
inlShape.Fill.Visible = False

      



Note that you need to set the ForeColor and Visible properties. Installing only one of them will not produce the desired result.

+1


source







All Articles