[seam-commits] Seam SVN: r8828 - in trunk/src/pdf: org/jboss/seam/pdf/ui and 1 other directory.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Wed Aug 27 05:35:03 EDT 2008
Author: nickarls
Date: 2008-08-27 05:35:03 -0400 (Wed, 27 Aug 2008)
New Revision: 8828
Added:
trunk/src/pdf/org/jboss/seam/pdf/ui/FormComponent.java
trunk/src/pdf/org/jboss/seam/pdf/ui/UIField.java
trunk/src/pdf/org/jboss/seam/pdf/ui/UIForm.java
Modified:
trunk/src/pdf/META-INF/faces-config.xml
trunk/src/pdf/META-INF/seam-pdf.taglib.xml
Log:
Support for filling of PDF form fields. Preliminary pre-alpha.
Modified: trunk/src/pdf/META-INF/faces-config.xml
===================================================================
--- trunk/src/pdf/META-INF/faces-config.xml 2008-08-27 06:18:18 UTC (rev 8827)
+++ trunk/src/pdf/META-INF/faces-config.xml 2008-08-27 09:35:03 UTC (rev 8828)
@@ -149,6 +149,17 @@
<component-class>org.jboss.seam.pdf.ui.UISwingComponent</component-class>
</component>
+ <component>
+ <component-type>org.jboss.seam.pdf.ui.UIField</component-type>
+ <component-class>org.jboss.seam.pdf.ui.UIField</component-class>
+ </component>
+
+ <component>
+ <component-type>org.jboss.seam.pdf.ui.UIForm</component-type>
+ <component-class>org.jboss.seam.pdf.ui.UIForm</component-class>
+ </component>
+
+
<!--
<render-kit>
<render-kit-id>PDF</render-kit-id>
Modified: trunk/src/pdf/META-INF/seam-pdf.taglib.xml
===================================================================
--- trunk/src/pdf/META-INF/seam-pdf.taglib.xml 2008-08-27 06:18:18 UTC (rev 8827)
+++ trunk/src/pdf/META-INF/seam-pdf.taglib.xml 2008-08-27 09:35:03 UTC (rev 8828)
@@ -208,4 +208,19 @@
<component-type>org.jboss.seam.pdf.ui.UISwingComponent</component-type>
</component>
</tag>
+
+ <tag>
+ <tag-name>form</tag-name>
+ <component>
+ <component-type>org.jboss.seam.pdf.ui.UIForm</component-type>
+ </component>
+ </tag>
+
+ <tag>
+ <tag-name>field</tag-name>
+ <component>
+ <component-type>org.jboss.seam.pdf.ui.UIField</component-type>
+ </component>
+ </tag>
+
</facelet-taglib>
Added: trunk/src/pdf/org/jboss/seam/pdf/ui/FormComponent.java
===================================================================
--- trunk/src/pdf/org/jboss/seam/pdf/ui/FormComponent.java (rev 0)
+++ trunk/src/pdf/org/jboss/seam/pdf/ui/FormComponent.java 2008-08-27 09:35:03 UTC (rev 8828)
@@ -0,0 +1,19 @@
+package org.jboss.seam.pdf.ui;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.context.FacesContext;
+
+public abstract class FormComponent extends UIComponentBase
+{
+ protected static final String FIELDS_KEY = "acrofields";
+
+ protected Object valueOf(String name, Object defaultValue)
+ {
+ Object value = defaultValue;
+ if (getValueExpression(name) != null)
+ {
+ value = getValueExpression(name).getValue(FacesContext.getCurrentInstance().getELContext());
+ }
+ return value;
+ }
+}
Added: trunk/src/pdf/org/jboss/seam/pdf/ui/UIField.java
===================================================================
--- trunk/src/pdf/org/jboss/seam/pdf/ui/UIField.java (rev 0)
+++ trunk/src/pdf/org/jboss/seam/pdf/ui/UIField.java 2008-08-27 09:35:03 UTC (rev 8828)
@@ -0,0 +1,61 @@
+package org.jboss.seam.pdf.ui;
+
+import java.io.IOException;
+
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.core.Interpolator;
+
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.pdf.AcroFields;
+
+public class UIField extends FormComponent
+{
+ public static final String COMPONENT_FAMILY = "org.jboss.seam.pdf.UIField";
+
+ private String name;
+ private String value;
+
+ @Override
+ public void encodeBegin(FacesContext facesContext) throws IOException
+ {
+ AcroFields fields = (AcroFields) Contexts.getEventContext().get(FIELDS_KEY);
+ try
+ {
+ fields.setField(getName(), getValue());
+ }
+ catch (DocumentException e)
+ {
+ String message = Interpolator.instance().interpolate("Could not set field #0 to #1", getName(), getValue());
+ throw new IOException(message, e);
+ }
+ }
+
+ @Override
+ public String getFamily()
+ {
+ return COMPONENT_FAMILY;
+ }
+
+ public String getName()
+ {
+ return (String) valueOf("name", name);
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public String getValue()
+ {
+ return (String) valueOf("value", value);
+ }
+
+ public void setValue(String value)
+ {
+ this.value = value;
+ }
+
+}
Added: trunk/src/pdf/org/jboss/seam/pdf/ui/UIForm.java
===================================================================
--- trunk/src/pdf/org/jboss/seam/pdf/ui/UIForm.java (rev 0)
+++ trunk/src/pdf/org/jboss/seam/pdf/ui/UIForm.java 2008-08-27 09:35:03 UTC (rev 8828)
@@ -0,0 +1,114 @@
+package org.jboss.seam.pdf.ui;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URL;
+
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.core.Manager;
+import org.jboss.seam.document.DocumentData;
+import org.jboss.seam.document.DocumentStore;
+import org.jboss.seam.document.DocumentData.DocumentType;
+import org.jboss.seam.log.Log;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.navigation.Pages;
+
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.pdf.AcroFields;
+import com.lowagie.text.pdf.PdfReader;
+import com.lowagie.text.pdf.PdfStamper;
+
+public class UIForm extends FormComponent
+{
+ public static final String COMPONENT_FAMILY = "org.jboss.seam.pdf.UIForm";
+ private static final String CONTENT_TYPE = "application/pdf";
+
+ private Log log = Logging.getLog(getClass());
+
+ private String URL;
+
+ PdfReader reader;
+ PdfStamper stamper;
+ AcroFields fields;
+ ByteArrayOutputStream buffer;
+
+ public String getURL()
+ {
+ return (String) valueOf("URL", URL);
+ }
+
+ public void setURL(String url)
+ {
+ URL = url;
+ }
+
+ @Override
+ public void encodeBegin(FacesContext facesContext) throws IOException
+ {
+ reader = new PdfReader(new URL(getURL()));
+ buffer = new ByteArrayOutputStream();
+ try
+ {
+ stamper = new PdfStamper(reader, buffer);
+ }
+ catch (DocumentException e)
+ {
+ throw new IOException("Could not create PDF stamper", e);
+ }
+ fields = stamper.getAcroFields();
+ Contexts.getEventContext().set(FIELDS_KEY, fields);
+ }
+
+ @Override
+ public void encodeEnd(FacesContext facesContext) throws IOException
+ {
+ stamper.setFormFlattening(true);
+ try
+ {
+ stamper.close();
+ }
+ catch (DocumentException e)
+ {
+ throw new IOException("Could not flush PDF", e);
+ }
+
+ String viewId = Pages.getViewId(facesContext);
+ String baseName = baseNameForViewId(viewId);
+ DocumentStore store = DocumentStore.instance();
+ DocumentType documentType = new DocumentData.DocumentType("pdf", "application/pdf");
+ DocumentData documentData = new DocumentData(baseName, documentType, buffer.toByteArray());
+ String id = store.newId();
+ String url = store.preferredUrlForContent(baseName, documentType.getExtension(), id);
+ url = Manager.instance().encodeConversationId(url, viewId);
+ store.saveData(id, documentData);
+ log.info("Redirecting to #0", url);
+ facesContext.getExternalContext().redirect(url);
+ }
+
+
+ public static String baseNameForViewId(String viewId)
+ {
+ int pos = viewId.lastIndexOf("/");
+ if (pos != -1)
+ {
+ viewId = viewId.substring(pos + 1);
+ }
+
+ pos = viewId.lastIndexOf(".");
+ if (pos != -1)
+ {
+ viewId = viewId.substring(0, pos);
+ }
+
+ return viewId;
+ }
+
+ @Override
+ public String getFamily()
+ {
+ return COMPONENT_FAMILY;
+ }
+
+}
More information about the seam-commits
mailing list