Hyperlink in PDF export?

Is there any way to create a hyperlink in a PDF file exported by Processing?

Answers

  • I found some documentation on how to do this with the library that Processing uses to handle PDF functionality. I need access to the PGraphicsPDF.document element, but it is a protected element.

    import processing.pdf.*;
    
    class myPDF extends PGraphicsPDF {
    
      com.lowagie.text.Document getDocument(){
        return document;
      }
    
    }
    
    void setup(){
      myPDF p = createGraphics(100,100,PDF);
    }
    

    So I try to extend the PGraphicsPDF class and add a method to get access to the element. I don't have enough java experience to tackle this issue. Any ideas?

  • No, I still need to modify the Processing source code to access the element, and the libraries used in that example are from 2007 and are no longer the same.

  • Answer ✓

    PGraphicsPDF is a bit more extensible than at the time, so perhaps you can make a sub-class of it to add this functionality. I did that in the past to get the PDF result in memory (not on disk), to send it as a byte array to a server. The first hack was complex, it became trivial later.

  • PhilLho, your example was exactly what I was looking for.

    pdfTest.pde:

    import processing.pdf.*;
    
    import java.awt.Color;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import com.lowagie.text.Anchor;
    import com.lowagie.text.Chunk;
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Font;
    import com.lowagie.text.FontFactory;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.html.HtmlWriter;
    import com.lowagie.text.pdf.PdfWriter;
    
    void setup(){
      PGraphics pdf = createGraphics(512,512,"myPDF","test.pdf");
    
      myPDF p = (myPDF) pdf;
    
      beginRecord(p);
    
        println(p.getDocument());
    
        p.background(0,0,0);
    
        Document document = p.getDocument();
    
        try{
    
          p.fill(0);
    
          Paragraph paragraph = new Paragraph("Please visit my ");
          Anchor anchor1 = new Anchor("website (external reference)", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255)));
          anchor1.setReference("http://www.java2s.com");
          paragraph.add(anchor1);
    
          document.add(paragraph);
          document.add(paragraph);
          document.add(paragraph);
          document.add(paragraph);
    
        } catch (Exception e){
    
        }
    
      endRecord();
      exit();
    
    }
    

    myPDF.java

    import processing.core.*;
    import processing.pdf.*;
    
    public class myPDF extends PGraphicsPDF {
    
      com.lowagie.text.Document getDocument(){
        return document;
      }
    
    }
    

    This creates a PDF file, and the text (with link!!) is added, but the text is not visible unless I highlight it. It's super weird.

  • I think the p.fill(0) is Processing specific and might not be used by the Document. You might need to set the color of the Anchor the iText way.

    Ah, also, you draw black text on black background?

  • That was just the results of my trying all different parameters for background and fill and stroke prior to adding the paragraph(anchor) to the document. I do specify the Color of the Anchor (0,0,255) at the end of the Anchor constructor.

    I presume Processing is changing all the text into shapes, and rendering them directly to the document as if they were any other type of graphical element.

  • edited October 2013

    It worked when I removed the Processing's background() and fill() calls...

    import processing.pdf.*;
    
    import java.awt.Color;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import com.lowagie.text.Anchor;
    import com.lowagie.text.Chunk;
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Font;
    import com.lowagie.text.FontFactory;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.html.HtmlWriter;
    import com.lowagie.text.pdf.PdfWriter;
    
    PGraphicsPDF pdf;
    
    void setup() {
      PGraphics pdf = createGraphics(512, 512, "ExtendedPDF", "H:/Temp/test.pdf");
    
      ExtendedPDF p = (ExtendedPDF) pdf;
    
      beginRecord(p);
    
      Document document = p.getDocument();
    
      try {
    
        Font font1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, Color.BLACK);
        Font font2 = FontFactory.getFont(FontFactory.HELVETICA, 11, Font.UNDERLINE, new Color(0, 0, 255));
        Paragraph paragraph = new Paragraph("Please visit my ", font1);
        Anchor anchor = new Anchor("website (external reference)", font2);
        anchor.setReference("http://www.java2s.com");
        paragraph.add(anchor);
    
        document.add(paragraph);
        document.add(paragraph);
        document.add(paragraph);
        anchor.setReference("http://www.processing.org");
        document.add(paragraph);
      } 
      catch (Exception e) {
      }
    
      endRecord();
      println("Done");
      exit();
    }
    

    font1 isn't really necessary, mostly here for experimentation.

  • For the life of me, I can't find the lines of code that actually do the heavy lifting for drawing shapes in PDF mode.

    Where would I find this?

  • Processing just relies on the capability of iText of transforming Java2D calls to PDF code... So it just draw on an iText implementation of Java2D's Graphics2D instead of drawing on a pure Java2D's Graphics2D.

Sign In or Register to comment.