You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by bu...@apache.org on 2002/09/13 11:34:55 UTC

DO NOT REPLY [Bug 12610] New: - onLoad Action for PDF documents or how to automatically print PDF documents

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=12610>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=12610

onLoad Action for PDF documents or how to automatically print PDF documents

           Summary: onLoad Action for PDF documents or how to automatically
                    print PDF documents
           Product: Fop
           Version: 0.20.3
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: pdf renderer
        AssignedTo: fop-dev@xml.apache.org
        ReportedBy: pik@uniquare.com


PDF documents allows the execution of JavaScript if the document is loaded. I used
<fo:root...
<fox:init xmlns:fox="http://xml.apache.org/fop/extensions">this.print\(false, 0,
this.numPages-1\);</fox:init>
...
to automatically print the document on the default printer.

To achive this the following changes are necessary in FOP 0.20.3:
- add a new FO extension (Init)
- modify the ExtensionElementMapping
- add a new PDF object (PDFInit)
- modify the PDFDocument and PDFRoot Classes
- modify the PDFRenderer

I hope this, or a similar mechanism will be included in future FOP 
releases.

New classes:
-Init.java--------------------------------------------------------------
package org.apache.fop.extensions;

import org.apache.fop.fo.*;
import org.apache.fop.apps.FOPException;
import java.util.*;
import org.xml.sax.Attributes;

/**
 * @author pik
 *
 */
public class Init extends ExtensionObj 
{
    private String javaScript = "";
    
    public static class Maker extends org.apache.fop.fo.FObj.Maker
    {
        public FObj make(FObj fobj, PropertyList propertylist)
        {
            return new Init(fobj, propertylist);
        }

        public Maker()
        {
        }
    }
    
    public static org.apache.fop.fo.FObj.Maker maker()
    {
        return new Maker();
    }    
    
    public Init(FObj parent, PropertyList propertylist)
    {
      super(parent, propertylist);
    }
    
    protected void addCharacters(char data[], int start, int end)
    {
      javaScript += new String(data, start, end - start);
    }

    public String getJavaScript()
    {
      return javaScript;
    }    
}
-end of Init.java---------------------------------------------------------

-PDFInit.java-------------------------------------------------------------
package org.apache.fop.pdf;

/**
 * @author pik
 */
public class PDFInit extends PDFObject
{
    private String javaScript = "";
    private String jsReference;
    private int type;

    public PDFInit(int number, int type)
    {
        super(number);
        this.type = type;
    }

    public void setJSReference(String jsReference)
    {
      this.jsReference = jsReference;   
    }

    public String getJSReference()
    {
      return jsReference;    
    }
    
    public void setJavaScript(String javaScript)
    {
      if (javaScript == null)
        this.javaScript = "";
      else    
        this.javaScript = javaScript;   
    }

    public byte[] toPDF()
    {
        String p;
         
        if (type == 1)
        {
          p = super.number + " " + super.generation + " obj\n";
          p += "<<\n/JavaScript " + (super.number+1) + " " + super.generation +
" R\n>>\nendobj\n";
        }
        else 
        if (type == 2)
        {
          p = super.number + " " + super.generation + " obj\n";
          p += "<<\n/Names [ (init)" + (super.number+1) + " " + super.generation
+ " R ]\n>>\nendobj\n";
        }
        else
        {
          p = super.number + " " + super.generation + " obj\n";
          p += "<<\n/S /JavaScript\n/JS (" + javaScript + ")\n>>\nendobj\n";
        }

        return p.getBytes();
    }
}
-end of PDFInit.java------------------------------------------

Modified classes (code from decompiled classes !):
-ExtensionElementMapping.java---------------------------------
    private static synchronized void setupExt()
    {
        if(foObjs == null)
        {
            foObjs = new HashMap();
            foObjs.put("outline", Outline.maker());
            foObjs.put("label", Label.maker());
++            foObjs.put("init", Init.maker());
        }
    }
-end of ExtensionElementMapping.java--------------------------

-PDFDocument.java---------------------------------------------
++    protected PDFInit pdfInit;

    public PDFDocument()
    {
        position = 0;
        location = new Vector();
        trailerObjects = new Vector();
        objectcount = 0;
        objects = new Vector();
        outlineRoot = null;
        colorspace = new ColorSpace(ColorSpace.DEVICE_RGB);
        patternCount = 0;
        shadingCount = 0;
        xObjectCount = 0;
        xObjects = new Vector();
        xObjectsMap = new Hashtable();
        pendingLinks = null;
        pages = makePages();
--      //   root = makeRoot(pages);
        
++        pdfInit = makeInit();
++        root = makeRoot(pages, pdfInit.getJSReference());
        
        resources = makeResources();
        info = makeInfo();
    }

++    public PDFInit getInit()
++    {
++      return pdfInit;    
++    }
++    protected PDFInit makeInit()
++    {
++        PDFInit pdfI = new PDFInit(++objectcount, 1);
++        String reference = pdfI.referencePDF();
++        
++        this.objects.add(pdfI);
++        this.objects.add(new PDFInit(++objectcount, 2));
++        
++        pdfI = new PDFInit(++objectcount, 3);
++        pdfI.setJSReference(reference);    // hold reference to init obj
++        addTrailerObject(pdfI);
++        
++        return pdfI;
++    }

+-    public PDFRoot makeRoot(PDFPages pdfpages, String reference)  //PIK00
(String reference added)
    {
+-        PDFRoot pdfroot = new PDFRoot(++objectcount, pdfpages, reference);
        addTrailerObject(pdfroot);
        return pdfroot;
    }
-end of PDFDocument.java--------------------------------------
-PDFRoot.java-------------------------------------------------
++    private String reference = null;

++    public PDFRoot(int i, PDFPages pdfpages, String reference) {
++        super(i);
++        this.reference=reference;    // hold reference to init obj
++        setRootPages(pdfpages);
++    }

    public byte[] toPDF()
        throws IllegalStateException
    {
        StringBuffer stringbuffer = new StringBuffer(super.number + " " +
super.generation + " obj\n<< /Type /Catalog\n/Pages " + rootPages.referencePDF()
+ "\n");
        if(_outline != null)
        {
            stringbuffer.append(" /Outlines " + _outline.referencePDF() + "\n");
            stringbuffer.append(" /PageMode /UseOutlines\n");
        }
        
++        if(reference != null)
++            stringbuffer.append("/Names " + reference + "\n");
        
        stringbuffer.append(" >>\nendobj\n");
        return stringbuffer.toString().getBytes();
    }
-end of PDFRoot.java------------------------------------------
-PDFRenderer.java---------------------------------------------
    protected void renderRootExtensions(Vector vector)
    {
        if(vector != null)
        {
            for(Enumeration enumeration = vector.elements();
enumeration.hasMoreElements();)
            {
                ExtensionObj extensionobj = (ExtensionObj)enumeration.nextElement();
                if(extensionobj instanceof Outline)
                    renderOutline((Outline)extensionobj);
++              if(extensionobj instanceof Init)
++              {
++                PDFInit pdfInit = pdfDoc.getInit();
++                pdfInit.setJavaScript(((Init)extensionobj).getJavaScript());
++              }    
            }
        }
    }
-end of PDFRenderer.java-----------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org