Home Forums General XMetaL Discussion Display Default Image Reply To: Display Default Image

Derek Read

Reply to: Display Default Image

Here's something that will set the rendered content to essentially nothing (actually renders a space), then adds CSS:before to display an image before the element.

var doc = ActiveDocument;
if (doc.ViewType == sqViewNormal || doc.ViewType == sqViewTagsOn) {
var rng = doc.Range;
var elemName = "graphic";
var attrName = "fileref";
var cssToAppend = ""; rng.MoveToDocumentStart();
while(rng.MoveToElement(elemName)) {
var attrVal = rng.ContainerAttribute(attrName);
//if(endsWith(attrVal,"pdf")) {
doc.SetRenderedContent(rng.ContainerNode," ");
cssToAppend += elemName + "[" + attrName + "='" + attrVal + "']:before{content:url(C:\pdficon.jpg)}";


I've commented out your call to your endsWith() method so there is currently no check and the CSS is built up for [u]all[/u] elements. This will let others run the code as is without seeing errors. You'll want to enable that obviously.

I don't think the check for ViewType is really needed but you can test that. Doesn't hurt anything to have it here either.

The call to setRenderedContent tells it to render a space. This is because we want the “missing image” icon for the to not appear when the PDF icon image is added. Without doing this both images would be present and probably confuse the author. If you set it to an empty string then the API does nothing (hence the space).

I use a different method of traversing the document. My testing over the years shows that this method is generally faster (under the covers XMetaL sort of does something similar to build up your list when you call getElementsByTagName before the rest of your code runs, meaning that it is almost doing it twice). You won't notice a difference unless you have really large documents though.