relative paths of .jpg target in Word 2007


J

james3mg

Hello, all!

I'm trying to write a program which will allow the user to select a group of
pictures, insert those pictures in a .docx document, and save the document in
the same folder as the pictures.

I have created a .docx document which will serve as my starting point: it
has only

_rels\.rels
app.xml and core.xml under docProps\
[Content_Types].xml
document.xml, header1.xml, footer1.xml under word\
and word\_rels\document.xml.rels

in it. I have gone through and stripped out the sidR tags so that word will
create them new each time (causing them to be unique and correct through
Word's system) as well as some unnecessary tags. (i.e. tags referring to
broken relationships, since I don't need files like settings.xml,
fontmap.xml, etc, as Word will create the appropriate files upon first save).
The document.xml file under word\ references the header and footer I have
created, establishes the columns and contains the tags for the table and text
I want to accompany each picture inserted. I have also identified all the
tags I need for the insertion of the pictures. In this way, I will be able
to have my program copy most of the core files into a new .docx file, then
create on-the-fly the word\_rels\document.rels directory of pictures.
Finally, it will create the document.xml file itself by writing the tags that
don't change, then copying the block of code for the table, picture and text
for each picture desired in the correct place.

My only problem is that I don't want the paths to the inserted pictures to
be absolute paths, nor do I want the pictures to be embedded within the
document. I'm using tags such as
<Relationship Id="rId6"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="file:///c:\whatever\moo.JPG" TargetMode="External"/>
to link the pictures into the document, but I haven't found a way to make
the paths relative to the .docx file. Simply referencing Target="moo.JPG",
or "..\moo.JPG" or "file:///moo.JPG" etc, results in everybody's favorite red
"x" icon within the document.

The reason I want them to be relative is that the pictures are of particular
jobs, initially in the active jobs directory on our network, but will
eventually get moved into the archived jobs directory. Since all the linked
pictures are in the same directory as the .docx file itself, I want to figure
out how to change the target tag so that word looks for the filename in the
same directory as the currently open file. That way, when the job gets moved
to the inactive jobs directory, the pictures and word document will remain
together, simply in a new root directory, and the links (relationship) will
not be broken, as they would be if absolute paths were used, since the path
will change when the project is moved.

If this should be deleted and reposted elsewhere, please inform me where.
This was the best-fitting discussion group I could find, and I have had very
poor luck finding information on the Word2007 xml tags themselves anywhere on
the internet, beyond the VERY basics such as at
http://msdn2.microsoft.com/en-us/li...eformat_walkthroughcreatingawordxmlformatfile

Thank you all for your help!
 
Ad

Advertisements

J

james3mg

I've solved my problem. I'm almost positive I tried it, but all I had to do
was set Target="moo.JPG" TargetMode="External" and it worked, just like I
hoped it would. I don't know why it didn't work before, but anyway, my
question is answered.

Feel free to post, anyone, if you know of a good tutorial/dictionary of
Word2007 xml tags.

Thanks, and sorry for wasting your time.
 
T

Thierry

Hi,

I have the exact same problem as you, but in my case, the docx is in a
folder, and the images are in many subfloders below it.
\doc\document.docx
\doc\images\aaa\toto.jpg
\doc\images\bbb\tata.jpg

I've tried also many ways to set each xml file to stop using full path, and
in my case TargetMode was already set to External. But I guess I'm not lucky
as you because it still doesn't work ...

:/
 
J

james3mg

Based on the same logic that worked for me, have you tried
<... Target="images\aaa\toto.jpg" ...> ?
This may be related to a lingering problem I have with this...when opened on
the machine that ran my program to write the .docx file, the pictures won't
show up, sometimes for quite a while, almost as if there's an error getting
the pictures the first time I load the document, and the error gets 'cached'
on that machine, because any other machine in our network can open the
document just fine. There may still be more than I realize to referencing
external images...
 
T

Thierry

Yes, I used the same logic as you, and I expected it working also .. :/
But maybe there is another issue, as the formats are different between
document.xml, and document.xml.rels, as one accept spaces charcters between
words in the path and for the other I need to put a %20 instead, not really
logical to my point of view .. :/
 
T

Thierry

I've just renamed one of the image from \xxx ccc.jpg to \xxx_ccc.jpg, and fix
all path in both xml and rels, and it worked ...

Crazy isn't it ...

I will continue to investigate this problem ...
 
Ad

Advertisements

T

Thierry

OK so it worked for some but not for all .. :/


Thierry said:
I've just renamed one of the image from \xxx ccc.jpg to \xxx_ccc.jpg, and fix
all path in both xml and rels, and it worked ...

Crazy isn't it ...

I will continue to investigate this problem ...
 
T

Thierry

ok finally I know ...

If my images are not in a drawing canvas, Word 2007 doesn't solve the
relative path, but if they are, it works (that was the difference between
working images inserted as link to a file, but not in a drawing canvas and
the others, with the exact same manipulation but in a drawing canvas) ...
So maybe maybe it is just that, and no relation with the name containing
spaces or not ...
 
J

james3mg

I've been experimenting a bit, and I'm finding that sub-folders work fine for
me, but no relative path with a space in the filename will work, even if I
replace the space with %20. The only way that I got it to work was to insert
the link to the file within Word and examine the code it wrote for it...it
used Target="file:///drive:\folder\subfolder\picture%20one.jpg" and was
obviously able to find it. My problem, of course, is that this is an
absolute path, not a relative one. As soon as I delete
"file:///drive:\folder\subfolder\" leaving only "picture%20one.jpg", the
picture isn't resolved in Word. If I put a space in the path, like the
picture filename, it breaks the word document so much it can't even open.
Target="file:///picture%20one.jpg" likewise breaks the entire document. Let
me know the syntax you were using that allowed the %20 to work (if it was a
relative path), and if you can figure anything more out on filenames with
spaces.

And thanks for pointing this out to me now, I had not realized this
potential problem. It's better to learn this now than when it was urgent.
Hopefully we can figure out something before it becomes so.
 
T

Thierry

I've built a very simple Word 2007 docx ... one page containing two images,
(in a sub-folder "My Pictures" for images). One inserted as link directly on
the page, and the other one inserted as link in a 'drawing canvas'.
The only relative path that works, with or without spaces is the one
embedded in a 'drawing canvas' ...
Also I had to modify the Target tag like this :
Target=".\My Pictures\image.jpg" in document.xml.
And Target="file:///.\My%20Pictures\image.jpg" in the .rels.
Pretty weird, but it works, well only for 'drawing canvas' inserted as link
images.
The important thing seems to be the ".\" I added for the .rels, without it
Word2007 won't open it.
 
J

james3mg

I can't get anything other than an absolute path to work after file:/// -
with or without spaces, .\ or %20.

Also, I don't have a target= tag in my document.xml file - it just inserts
the link to the file with the tag <a:blip r:link="MGId1" /> (MGId1 is a
custom relationship I created to avoid conflict with any "built-in"
relationships). MGId1 is defined in the document.xml.rels file with the tag:
<Relationship Id="MGId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="TestPic.jpg" TargetMode="External" />

This method works completely (even if it's Target="subfolder/TestPic.jpg")
except when there's a space in the filename (which only appears in the
above-referenced <Relationship> tag in document.xml.rels).

My tags for creating the picture in document.xml are (omitting actual data,
tag parameters and other tags not in the direct line):
<w:drawing>
<wp:inline>
<a:graphic>
<a:graphicData>
<pic:pic>
<pic:blipFill>
<a:blip r:link="MGId1" />
</pic:blipFill>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>

I think that the <w:drawing> tag is the drawing canvas you refer to, is it
not? So I don't understand what we're doing so different that would net such
different results. Is there something I'm missing that you can spot?
 
Ad

Advertisements

T

Thierry

nope, the code you have is the one that does not rwork ...
My code is like :

<w:r>
<w:pict>
<v:group id="_x0000_s1027" editas="canvas"
style="width:468pt;height:280.8pt;mso-position-horizontal-relative:char;mso-position-vertical-relative:line" coordorigin="2527,3532" coordsize="7200,4320">
<o:lock v:ext="edit" aspectratio="t" />
<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75"
o:preferrelative="t" path="[email protected]@[email protected]@[email protected]@[email protected]@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter" />
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0" />
<v:f eqn="sum @0 1 0" />
<v:f eqn="sum 0 0 @1" />
<v:f eqn="prod @2 1 2" />
<v:f eqn="prod @3 21600 pixelWidth" />
<v:f eqn="prod @3 21600 pixelHeight" />
<v:f eqn="sum @0 0 1" />
<v:f eqn="prod @6 1 2" />
<v:f eqn="prod @7 21600 pixelWidth" />
<v:f eqn="sum @8 21600 0" />
<v:f eqn="prod @7 21600 pixelHeight" />
<v:f eqn="sum @10 21600 0" />
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />
<o:lock v:ext="edit" aspectratio="t" />
</v:shapetype>
<v:shape id="_x0000_s1026" type="#_x0000_t75"
style="position:absolute;left:2527;top:3532;width:7200;height:4320"
o:preferrelative="f">
<v:fill o:detectmouseclick="t" />
<v:path o:extrusionok="t" o:connecttype="none" />
<o:lock v:ext="edit" text="t" />
</v:shape>
<v:shape id="_x0000_s1029" type="#_x0000_t75"
style="position:absolute;left:4236;top:4274;width:3781;height:2836">
<v:imagedata r:id="rId4" />
</v:shape>
<w10:wrap type="none" />
<w10:anchorlock />
</v:group>
</w:pict>
</w:r>

A lot more complex for sure, but works in all situations ...
'rId4' is solved on the .rels

<Relationship Id="rId4"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target=".\My%20Pictures/Img0006/DSCN0004.JPG" TargetMode="External" />

Maybe should work also with './' style path.
 
Ad

Advertisements

J

james3mg

That's exactly what I needed, thanks!

For your information, I managed to trim down the code a LOT and figure out
what some of the numbers mean. Though it looks like you have specific
absolute positioning on your pictures on purpose, for my use I needed to get
rid of that and have it stick the picture "inline" with the current text.

Here's all the tags I used:

<w:pict>
<v:group
style="width:162pt;height:216pt;mso-position-horizontal-relative:char;mso-position-vertical-relative:line" coordorigin="0,0" coordsize="100,100">
<o:lock v:ext="edit" aspectratio="t" />
<v:shape type="#_x0000_t75"
style="position:relative;width:100;height:100">
<v:imagedata r:id="MGId1" />
</v:shape>
<w10:wrap type="none" />
<w10:anchorlock />
</v:group>
</w:pict>

setting coordorigin to 0,0 in the 2nd line told it to put the picture in the
current location, and setting the coordsize to 100,100 made a lot more sense
to me than the previous numbers, once I figured out what they represented.
Coordsize represents 100% of the canvas, referenced in the width and height
parameters of the v:shape tag. So with coordsize at 100,100 and width and
height both at 100, it fills the canvas, whose size is defined in the v:group
style=width;height. If I set the v:shape width to 200 when coordsize was
100,100, the picture would be twice the width of the canvas, meaning that
you'd only see the left half of the picture, stretched by twice its normal
width (it visually crops the picture off at the edge of the canvas). Setting
the width and height to 50 in the v:shape tag would shrink the picture to 1/4
the size of the canvas, keeping the same aspect ratio as the canvas, whose
size (thus aspect ratio) is defined in the v:group style="width;height" tag.

It's possible even more of these tags could be deleted without consequence,
but that's where I stopped; I figured it was small enough and they kind of
make sense to leave for my use (wrap type, anchor lock, lock aspect ratio,
etc). The only thing I lose by using this code instead of what I had before,
is easy access to the "Picture" tab on the ribbon when the picture is
highlighted. I can still get to it (as long as editstyle=canvas is not
present) by single-clicking the picture to highlight the shape, then
single-clicking the picture again to select the picture within the shape. If
its editstyle is set to canvas however, I can't seem to get into the picture
itself. However, this is good enough for me, since theoretically any changes
to the picture should be done to the file being referenced in, not within
word only.

Thanks SO MUCH for all your help! I'm much happier with the outcome now,
especially since files with spaces work now.

By the way, this solution also resolved the problem of the pictures not
always loading on the same computer that created the file, I don't know what
that was all about, but it's gone now, so that's all that matters!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top