[Solved] Linking to a personal signature image from template

Using them, Making them, Finding them
Post Reply
Melange
Posts: 4
Joined: Wed Nov 25, 2009 3:31 pm

[Solved] Linking to a personal signature image from template

Post by Melange »

Hi,

My goal is to write a letter template which has multiple user-dependend "personal" fields, for example full name, telephone number, e-mail address and the like. All users of this template know how to configure their OOo appropriately, and so these textual fields work fine.

I also want to include a personal signature PNG image on the fist page of that template, and here is where I run into problems. When using OOo default settings, image file paths are saved realtive to the path of the document. Unfortunately I cannot make any assumptions on the location of the template file on my users PCs, and at the same time I cannot ask them to activate absolute file paths globally in OOo since that would break many of our other documents instantly.

Best setup (I think) would be to tell my users "put an image called foo.png in your C:/ root", then linking to it from within the template using file:///C:/foo.png.

I cannot find an option at the image link "Make this link absolute" - I only find the global setting which is not appropriate in my case.

I'm now tinkering with the thought to patch the OTT by making this single URL absolute with some script magic before publishing it to my users. But before going to these lengths, I'd kindly ask for some help on this one.

So, what else can I do?

Thanks for any suggestions,
Christian.
Last edited by Melange on Thu Nov 26, 2009 10:58 pm, edited 1 time in total.
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Linking to a personal signature image from a template

Post by acknak »

Have you tried it? Maybe OOo behaves differently between Windows and Linux in how links are handled, but it does seem to work reasonably under Linux.

If I create a template that links in an image $HOME/images/sig.png, then I copy the template and open it as a different user, in a completely different location, Writer tries to find the image $HOME/images/sig.png, where the $HOME is the correct home directory for the other user.

I think that means that you can tell the user to simply create a signature image with a specific name in a location relative to their home/user directory--e.g. My Documents/sig.png, and Writer will look for the image in the appropriate place for each user.
AOO4/LO5 • Linux • Fedora 23
Melange
Posts: 4
Joined: Wed Nov 25, 2009 3:31 pm

Re: Linking to a personal signature image from a template

Post by Melange »

acknak wrote:If I create a template that links in an image $HOME/images/sig.png, then I copy the template and open it as a different user, in a completely different location, Writer tries to find the image $HOME/images/sig.png, where the $HOME is the correct home directory for the other user.
I hadn't tried environment variables as you suggested yet, many thanks for that input.

However, Windows OOo doesn't seem to play along (please correct me if I'm wrong). I tried it with environment variable USERPROFILE. The correct DOS syntax for foo.png at that location would be "%USERPROFILE%"\foo.png and so the OOo URL I tried is file:///%USERPROFILE%/foo.png (I also tried all sorts of variations) and it didn't work at all. OOo re-encodes this string to file:///%25USERPROFILE%/foo.png (the %25 is correct URL encoding since % is a special character in URLs) which cannot work.

So we now know that OOo/Linux expands environment variables in file paths, but I could not yet find out if that applies to OOo/Windows, too. If it does apply, how am I supposed to declare my file path correctly to OOo?

Thanks,
Christian.
OpenOffice 3.1.1 on Windows XP
User avatar
acknak
Moderator
Posts: 22756
Joined: Mon Oct 08, 2007 1:25 am
Location: USA:NJ:E3

Re: Linking to a personal signature image from a template

Post by acknak »

Sorry, I should've explained more clearly. I only wrote "$HOME" as a textual abbreviation, not to indicate that I used a variable of any kind. I simply used it instead of writing out the entire path to the file.

In the document, I did not do anything at all special. I just used Insert > Picture > From File, browsed to the image, ticked the "Link" option, and OK. When I opened the template under the other user account, OOo automatically looked for the image file in the other user's directory and not in the directory pointed to by the link when it was created initially.

I think OOo tries to be smart about handling file locations; it doesn't use a simple relative or absolute path to resolve the link. The link in the document file looks like this: xlink:href="../../images/sig.png", a relative path. When Writer loads the document, it combines that relative path with the location of the document to come up with a full location for the file. The location it constructs is correct in some cases, and incorrect in others, depending on where the template is located when I open it. I think you will just have to experiment, but you may find some arrangement that works.
AOO4/LO5 • Linux • Fedora 23
Melange
Posts: 4
Joined: Wed Nov 25, 2009 3:31 pm

Re: Linking to a personal signature image from a template

Post by Melange »

acknak wrote:Sorry, I should've explained more clearly. I only wrote "$HOME" as a textual abbreviation, not to indicate that I used a variable of any kind. I simply used it instead of writing out the entire path to the file.
Yeah well as I stated in my initial post, dictating directory layouts to the users is not an option in my case.

Yesterday evening I played around with a database-based solution to my problem and scratched it after it was beginning to work due to its inherent uglyness. The best I came up with so far is this macro:

Code: Select all

Sub sig

Dim sigUrl

sigUrl = "file:///" & Environ("USERPROFILE") & "/ooo-signature.png"
ThisComponent.getGraphicObjects().getByName("signature").GraphicURL = sigUrl

End Sub
It basically realizes what I (mis)understood in your first reply: It espands the DOS environment variable "USERPROFILE", appends the relative file name "ooo-signature.png" to it and assigns that newly constructed file URL to the file path of the image named "signature" in the currently opened document.

It's my first macro ever, perhaps someone with experience can take a critical look at it. What I'm trying next is to have this macro executed when a new document is created from the template - anyone have some tips here?

Regards,
Christian.
Melange
Posts: 4
Joined: Wed Nov 25, 2009 3:31 pm

Re: Linking to a personal signature image from a template

Post by Melange »

Here is how I solved it now:

Code: Select all

sub sig

dim sHomePath, sSignatureFilename

' check that a graphics object named "signature" exists
if thisComponent.getGraphicObjects().hasByName("signature") then
    ' get platforms user home directory (MyDocuments under Windows)
    sHomePath = createUnoService("com.sun.star.util.PathSubstitution").getSubstituteVariableValue("$(work)")
    ' check that the signature image file exists
    sSignatureFilename = sHomePath & "/ooo-signature.png"
    if FileExists(sSignatureFilename) then
        ' point objects graphic file URL to the users image file
        thisComponent.getGraphicObjects().getByName("signature").GraphicURL = ConvertToURL(sSignatureFilename)
    end if
end if

end sub
This macro checks for the existence of two things:
  • a graphics object in the current document named "signature"
  • a graphics file named "ooo-signature.png" in the users home directory
If both exist, the graphics file path is assigned to the graphics object.

Unfortunately Windows (XP) does not have an environment variable that represents the users "My Documents" folder, but it is possible to find that path via OOo's PathSubstitution utility class, where $(work) expands to "My Documents" on Windows and to "~" on linux etc.

I moved the macro into a macro library and then assigned the library function to the on-create-document action handler of several templates. Now, when I double-click one of these template files, a new document instance is created and my personal signature image is assigned automatically to the placeholder image, as will be the case for anyone who installs the library and puts their signature at the right location on their hard drive.

You'll need a digital certificate to get this to work without security warnings from OOo. You will have to digitally sign the macros of a template after linking its on-create-document handler to the library function. It is not neccessary to sign the template itself though.

Christian
OpenOffice 3.1.1 on Windows XP
Post Reply