Forum Samples, Tips and Tricks

Outline PDF links

Nicholas
Prince currently has fairly little support for PDF annotations, which you need to use to make the screen and print version of the PDF different. I underline my hyperlinks on my site, but there is no point using different styles for internal page links in ToC, Indices, and so on. So, the print version of the PDF has plain (unstyled) links, but I wanted to distinguish them visually when looking at the PDF on screen, because you can of course still click on them. I did this by adding borders to the Link annotations, post-processing the Prince PDF using PDFedit. This was new to me, but it was all surprisingly easy. PDF is a rather neat and tidy format, very easy to learn.

This is my solution:
// Console: dropAnnotation
// Description: Remove the first annotation from the first page of a file
// Parameters: [input file]

function onConsoleStart() {

}
if (name = takeParameter()) {
  var document = loadPdf(name, false);
  for (var i = 1; i <= document.getPageCount(); ++i) {
    var page = document.getPage(i).getDictionary();
    if (!page.exist("Annots")) continue;
    var annots = page.child("Annots");
    for (var j = 0; j < annots.count(); ++j) {
      var annot = annots.property(j).ref();
      if (annot.exist("Subtype") && annot.property("Subtype").value() == "Link") {
        annot.property("Border").delProperty(2);
        annot.property("Border").add(createInt(1));
        annot.add("C", createArray());
        var C = annot.property("C").ref();
        C.add(createReal(0.0));
        C.add(createReal(0.0));
        C.add(createReal(1.0));
      }
    }
  }
  document.save(false);
  exit(0);
} else {
  print('Error: specify a file');
  exit(1);
}


I run it from my automation framework using
pdfedit -s <path>/script.qs <doc>.pdf
and get beautiful results! Example: http://www.nicholaswilson.me.uk/pdf/2011/03/state-of-the-union

I would be glad to hear if there is an easier way to do this. If anyone else wants to give it a go, Adobe very kindly provide an extremely readable spec: http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf
oodavid
I've got a neat CSS trick to deal with links:

/* External links should show their URL */
	a[href]			{color: #0159A0};
	a[href]:after	{content: " (" attr(href) ")";}
	/* Internal links don't need it */
		a[href^="#"]		{color: #A00601;}
		a[href^="#"]:after	{content: "";}

Which does this:

  • Internal links like <a href="#heading">... are red
  • External links like <a href="everythingelse">... are blue AND have the URL appended to them to be referenced during print :-)

PS - am new to printing nomenclature so I may have misinterpreted the post