Hello,

I have a strange behaviour with iText.

I've added a 3d modell programmatically with iText (see code below). 
When I add the JavaScript with Acrobat, I get the JavaScript message "host 
is undefined".

If I add the 3D modell and script manually, it works fine ...

An example pdf can be found on 
http://www.bct-technology.com/fileadmin/pdf/bct_report/example.pdf (1.9 
MB)
The upper model was added with iText, the lower manually.
If you select a part, the partname should be shown in the textfield.

Any clue? Thanks in advance!

Toni


(PS: how to add the JavaScript pro)




Here is the java code of iText:
(Based on the code of Paulo)

public class PDF3DObject extends PdfAnnotation
{
    public static final String PDF_NAME_3D = "3D";
    public static final String PDF_NAME_3DD = "3DD";
    public static final String PDF_NAME_3DV = "3DV";
    public static final String PDF_NAME_3DVIEW = "3DView";
    public static final String PDF_NAME_C2W = "C2W";  // Transformation 
matrix (optional)
    public static final String PDF_NAME_IN = "IN"; // internal name of the 
view (optional)
    public static final String PDF_NAME_MS = "MS"; //
    public static final String PDF_NAME_XN = "XN"; // The external name of 
the view (required)

    public static final String PDF_NAME_U3D = "U3D";
    public static final String PDF_NAME_U3DPATH = "U3DPath";
    public static final String PDF_NAME_ONINSTANTIATE = "OnInstantiate";


    public PDF3DObject(PdfStamper stamper, File u3dFile, Rectangle 
rectangle, int page)
    {
        super(stamper.getWriter(), rectangle);

        try
        {
            PdfWriter writer = stamper.getWriter();

            PdfIndirectReference streamRef;
            PdfIndirectObject objRef;

            PdfContentByte cb = stamper.getOverContent(page);

            // Create stream to carry attachment
            FileInputStream fistrm = new FileInputStream(u3dFile);
            PdfStream stream = new PdfStream(fistrm, writer);

            // Mandatory keys
            stream.put(PdfName.TYPE, new PdfName(PDF_NAME_3D));
            stream.put(PdfName.SUBTYPE, new PdfName(PDF_NAME_U3D));
            stream.flateCompress();

            // Write stream contents, get reference to stream object, 
write actual stream length
            streamRef = writer.addToBody(stream, 
false).getIndirectReference();
            stream.writeLength();


/* JavaScript Stream
            FileInputStream jsFiStream = new FileInputStream(new 
File("C:\\TEMP\\_techdoc\\SelectPart1.js"));
            PdfStream jsStream = new PdfStream(jsFiStream, writer);
            jsStream.flateCompress();
            jsStream.writeLength();
*/


            // Create 3D view dictionary
            // PDF documentation states that this can be left out, but 
without normally we will just get a blank
            //   3D image because of wrong coordinate space 
transformations, etc.
            // Instead of providing camera-to-world transformation here, 
we could also reference view in U3D file itself
            //   (would be U3DPath key instead of C2W key, U3D value 
instead of M value for MS key), but i haven't tried
            //   up to now
            // We could also provide an activation dictionary (defining 
activation behavior), and field-of-view for
            //   P entry if needed
            PdfDictionary dict = new PdfDictionary(new 
PdfName(PDF_NAME_3DVIEW));

            dict.put(new PdfName(PDF_NAME_XN), new 
PdfString("DefaultView"));
            dict.put(new PdfName(PDF_NAME_IN), new PdfString("Unnamed"));

            dict.put(new PdfName(PDF3DObject.PDF_NAME_MS), new 
PdfName(PDF3DObject.PDF_NAME_U3D));

            // NOTE: The initial release of Acrobat 7.0 does not correctly 
display the default view in the U3D data.
            //       This behavior is expected to be corrected in future 
releases and hence.
            dict.put(new PdfName(PDF3DObject.PDF_NAME_U3DPATH), new 
PdfString("DefaultView"));

            // Manual camera
            // States that we have to provide camera-to-world coordinate 
transformation
            // dict.put(new PdfName(PDF_NAME_MS), PdfName.M);
            // dict.put(new PdfName(PDF_NAME_C2W), new PdfArray(new 
float[] {1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, 28F}));

            // Camera distance along z-axis
            // dict.put(PdfName.CO, new PdfNumber(235));

//            dict.put(new PdfName(PDF_NAME_ONINSTANTIATE), jsStream);

            // Write view dictionary, get reference
            objRef = writer.addToBody(dict, false);

            //
            // Create appearance
            PdfAppearance ap = cb.createAppearance(rectangle.getWidth(), 
rectangle.getHeight());
            ap.setBoundingBox(rectangle);

            put(PdfName.CONTENTS, new PdfString("3D Model"));

            // Mandatory keys
            put(PdfName.TYPE, PdfName.ANNOT);
            put(PdfName.SUBTYPE, new PdfName(PDF_NAME_3D));

            // Reference to stream object
            put(new PdfName(PDF_NAME_3DD), streamRef);

            // Reference to view dictionary object
            put(new PdfName(PDF_NAME_3DV), objRef.getIndirectReference());

            // Assign appearance and page
            setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);

        }
        catch (IOException ex)
        {
            Debug.error("Internal Error: ", ex);
        }
    }
}





And the Java Script



var mouseDragged = true;
var selectedMesh = null;

//------------------------------------------------------------------------------
// function used by the Mouse Event Handler
//------------------------------------------------------------------------------

myMouseHandlingFunction = function( event )
{
  if( event.isMouseDown )
    mouseDragged = false;
 
  //-----------------------------------------------
  // mouse UP
  //-----------------------------------------------
  if ( event.isMouseUp )
  {
    var clickedMesh = null;
    if(event.hits.length > 0)
    {
      clickedMesh = event.hits[0].target;
    }
 
    if(selectedMesh == clickedMesh || mouseDragged)
      return;
 
    if (selectedMesh != null)
      if(clickedMesh != null)
      {
        host.getField("TeilMouseDown").value = clickedMesh.name;
      }
      else
      {
        host.getField("TeilMouseDown").value = "";
      }
 
    selectedMesh = clickedMesh.name;
  }
 
  if ( event.isMouseMove )
    mouseDragged = true;
}

var mouseEventHandler = new MouseEventHandler();
mouseEventHandler.onMouseDown = true;
mouseEventHandler.onMouseMove = true;
mouseEventHandler.onMouseUp = true; 
mouseEventHandler.onEvent = myMouseHandlingFunction;
runtime.addEventHandler( mouseEventHandler );

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to