Previous | Next | (P-PDF) Engineering
Topic: iText 3D PDF and JavaScript
Conf: (P-PDF) Engineering, Msg: 160606
From: aheckmeier
Date: 6/11/2007 12:36 AM
Hello,
I've added a 3D modell to an existing PDF with iText. When i add a Script like "host.getField("testField").value = clickedMesh.name;", i get the error message "host is undefined".
If i add the 3D modell and script manually, it works fine ...
Any clue?
Thanks in advance!
Toni
Here is the code of iTexte:
public class PDF3DObject extends PdfAnnotation
{
// See also PDF reference 1.6 (Page 757)
public static final String PDF_NAMESAPCE = "iTextSharp.text.pdf.";
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";
/**
*
* @param writer PdfWriter
* @param u3dFile File
* @param previewImage File
* @param rectangle Rectangle
*/
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();
// 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"));
// Write view dictionary, get reference
objRef = writer.addToBody(dict, false);
//
// Create appearance
PdfAppearance ap = cb.createAppearance(rectangle.width(), rectangle.height());
ap.setBoundingBox(rectangle);
put(PdfName.CONTENTS, new PdfString("3D Model"));
// Mandatory keys
put(PdfName.SUBTYPE, new PdfName(PDF_NAME_3D));
put(PdfName.TYPE, PdfName.ANNOT);
// 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);
}
}
}