Hello,
I am hoping that someone will be able to tell me what I've done wrong. I am
attempting to build a GUI interface for using FOP for a customer to publish
their XML based manuals to PDF. I am pretty green in Java, but following a few
tutorials, I have managed to create an app that 90% works. FOP is processing
the XML, but it is not rendering any of the graphics into the PDF. When I run
the same XML/XSL-FO from the CMD prompt, everything runs fine. I am using FOP
1.0.
I have attached a .txt file copy of the Java code.
I am pretty confident that it is something simple that I have omitted, or did
not realize should be included.
Thank you for your help.
Tom Furst
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NumberAdditionUI.java
*
* Created on Nov 20, 2010, 6:22:11 PM
*/
package my.NumberAddition;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.ButtonModel;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
//FOP
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
//Graphics
/**
*
* @author tfurst
*/
public class NumberAdditionUI extends javax.swing.JFrame {
private ButtonModel b;
private ButtonModel a;
/** Creates new form NumberAdditionUI */
public NumberAdditionUI() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
XMLlabel = new javax.swing.JLabel();
XMLfield = new javax.swing.JTextField();
clearFieldsButton = new javax.swing.JButton();
createPDFbutton = new javax.swing.JButton();
XSLlabel = new javax.swing.JLabel();
XSLfield = new javax.swing.JTextField();
OutDirLabel = new javax.swing.JLabel();
OutDirField = new javax.swing.JTextField();
PDFfileNameLabel = new javax.swing.JLabel();
PDFfileNameField = new javax.swing.JTextField();
fileChooser = new javax.swing.JFileChooser();
jScrollPane1 = new javax.swing.JScrollPane();
OutputArea = new javax.swing.JTextArea();
exitButton = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
chooseXML = new javax.swing.JMenuItem();
chooseXSL = new javax.swing.JMenuItem();
chooseDir = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("FOP Tool");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("File
Information"));
XMLlabel.setText("XML File");
clearFieldsButton.setText("Clear");
clearFieldsButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearFieldsButtonActionPerformed(evt);
}
});
createPDFbutton.setText("Create PDF");
createPDFbutton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createPDFbuttonActionPerformed(evt);
}
});
XSLlabel.setText("XSL File");
XSLfield.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
XSLfieldActionPerformed(evt);
}
});
OutDirLabel.setText("Output Dir");
PDFfileNameLabel.setText("PDF Filename");
javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(XMLlabel,
javax.swing.GroupLayout.PREFERRED_SIZE, 75,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(XSLlabel)
.addComponent(OutDirLabel)
.addComponent(PDFfileNameLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(PDFfileNameField,
javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(createPDFbutton,
javax.swing.GroupLayout.PREFERRED_SIZE, 103,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(281, 281, 281)
.addComponent(clearFieldsButton,
javax.swing.GroupLayout.PREFERRED_SIZE, 107,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(XMLfield,
javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
.addComponent(XSLfield,
javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
.addComponent(OutDirField,
javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(XMLlabel)
.addComponent(XMLfield,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(XSLlabel)
.addComponent(XSLfield,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(OutDirLabel)
.addComponent(OutDirField,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PDFfileNameLabel)
.addComponent(PDFfileNameField,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22,
Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(createPDFbutton)
.addComponent(clearFieldsButton))
.addContainerGap())
);
fileChooser.setCurrentDirectory(new java.io.File("C:\\Program
Files\\NetBeans 6.9.1"));
fileChooser.setDialogTitle("Choose XML");
fileChooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setCursor(new
java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
fileChooser.setInheritsPopupMenu(true);
fileChooser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileChooserActionPerformed(evt);
}
});
OutputArea.setColumns(20);
OutputArea.setRows(5);
jScrollPane1.setViewportView(OutputArea);
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
fileMenu.setText("File");
fileMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileMenuActionPerformed(evt);
}
});
chooseXML.setText("Choose XML Source");
chooseXML.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chooseXMLActionPerformed(evt);
}
});
fileMenu.add(chooseXML);
chooseXSL.setText("Choose XSL File");
chooseXSL.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chooseXSLActionPerformed(evt);
}
});
fileMenu.add(chooseXSL);
chooseDir.setText("Choose Output Dir");
chooseDir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chooseDirActionPerformed(evt);
}
});
fileMenu.add(chooseDir);
jMenuBar1.add(fileMenu);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(541, Short.MAX_VALUE)
.addComponent(exitButton)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
.addContainerGap())
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(fileChooser,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 117,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(exitButton)
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(fileChooser,
javax.swing.GroupLayout.PREFERRED_SIZE, 87,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(289, Short.MAX_VALUE)))
);
jPanel1.getAccessibleContext().setAccessibleName("FOP Tool");
getAccessibleContext().setAccessibleName("FOP Build");
pack();
}// </editor-fold>
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void clearFieldsButtonActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
XMLfield.setText("");
XSLfield.setText("");
OutDirField.setText("");
}
private void createPDFbuttonActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//float num1, num2, result;
try {
OutputArea.setText("FOP ExampleXML2PDF\n");
OutputArea.append("Preparing...");
File baseDir = new File(".");
File outDir = new File(OutDirField.getText());
//outDir.mkdirs();
File pdfFilename = new File(PDFfileNameField.getText());
File xmlfile = new File(XMLfield.getText());
File pdffile = new File(outDir + "\\" + pdfFilename);
//if(buttonGroup1.equals(jRadioButton1.isSelected())){
File xslfile = new File(XSLfield.getText());
OutputArea.append("Input: XML (" + xmlfile + ")");
OutputArea.append("\nStylesheet: " + xslfile);
OutputArea.append("\nOutput: PDF (" + pdffile + ")");
OutputArea.append("\n");
OutputArea.append("\nTransforming...");
//result = num1;
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new java.io.BufferedOutputStream(new
java.io.FileOutputStream(pdffile));
//out = new java.io.BufferedOutputStream(out);
try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new
StreamSource(xslfile));
//Transformer transformer = factory.newTransformer();
// Set the value of a <param> in the stylesheet
//transformer.setParameter("versionParam", "2.0");
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
// Resulting SAX events (the generated FO) must be piped
through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
OutputArea.append("\nSuccess!");
}catch (Exception e) {
String err = e.toString();
OutputArea.append(err);
//e.printStackTrace(OutputArea.append(System.err));
System.exit(-1);
}
}
private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void chooseXMLActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
FileReader filepath = new FileReader(file.getAbsolutePath());
XMLfield.setText(fileChooser.getSelectedFile().getAbsolutePath());
} catch (IOException ex) {
OutputArea.append("\nProblem accessing
file"+file.getAbsolutePath());
}
} else {
OutputArea.append("\nFile access cancelled by user.");
}
}
private void chooseXSLActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
FileReader filepath = new FileReader(file.getAbsolutePath());
XSLfield.setText(fileChooser.getSelectedFile().getAbsolutePath());
} catch (IOException ex) {
OutputArea.append("\nProblem accessing
file"+file.getAbsolutePath());
}
} else {
OutputArea.append("\nFile access cancelled by user.");
}
}
private void XSLfieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
@SuppressWarnings("static-access")
private void chooseDirActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
// What to do with the file, e.g. display it in a TextArea
FileReader filepath = new FileReader(file.getAbsolutePath());
String path = fileChooser.getSelectedFile().getAbsolutePath();
int dot = path.lastIndexOf(".");
int sep = path.lastIndexOf("\\");
path = path.substring(0,dot);
path = path.substring(0,sep);
OutDirField.setText(path);
//jTextField3.setText(fileChooser.getSelectedFile().getAbsolutePath());//getSelectedFile().getAbsolutePath()
} catch (IOException ex) {
OutputArea.append("\nProblem accessing
file"+file.getAbsolutePath());
}
} else {
OutputArea.append("\nFile access cancelled by user.");
}
}
private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NumberAdditionUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField OutDirField;
private javax.swing.JLabel OutDirLabel;
private javax.swing.JTextArea OutputArea;
private javax.swing.JTextField PDFfileNameField;
private javax.swing.JLabel PDFfileNameLabel;
private javax.swing.JTextField XMLfield;
private javax.swing.JLabel XMLlabel;
private javax.swing.JTextField XSLfield;
private javax.swing.JLabel XSLlabel;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JMenuItem chooseDir;
private javax.swing.JMenuItem chooseXML;
private javax.swing.JMenuItem chooseXSL;
private javax.swing.JButton clearFieldsButton;
private javax.swing.JButton createPDFbutton;
private javax.swing.JButton exitButton;
private javax.swing.JFileChooser fileChooser;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]