sende fra form til pdf udvikler
Jeg har 3 klasser.Main: starter en gui klasse op.
asd: indeholder en textarea og en submit button
FormTextField: udskriver en pdf fil
Indtil videre har jeg manipuleret det således at den starter op. trykker submit, og så laves der en pdf fil.
Men, Den pdf fil der laves, bliver lavet ud fra forudbestemt string værdi "hej".
Det jeg gerne vil, er at værdien fra textarea fra klassen asd bliver sendt over, og bliver udskret istedet for. Men har problemer med at gøre dette. Regner med at man skal kalde variablen som den ligger i, istedet for at have en string værdi. men hvordan..
her er koden:
/****************************************************************/
/* asd */
/* */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Summary description for asd
*
*/
public class asd extends JFrame
{
// Variables declaration
private JLabel jLabel1;
public JTextArea jTextArea1;
private JScrollPane jScrollPane1;
private JButton Submit;
private JPanel contentPane;
// End of variables declaration
public asd()
{
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
/**
* 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 Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent()
{
jLabel1 = new JLabel();
jTextArea1 = new JTextArea();
jScrollPane1 = new JScrollPane();
Submit = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// jLabel1
//
jLabel1.setText("Første Eksempel");
//
// jTextArea1
//
jTextArea1.setText("Indtast");
//
// jScrollPane1
//
jScrollPane1.setViewportView(jTextArea1);
//
// Submit
//
Submit.setText("Submit");
Submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Submit_actionPerformed(e);
}
});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 19,32,102,18);
addComponent(contentPane, jScrollPane1, 121,29,186,30);
addComponent(contentPane, Submit, 120,67,83,28);
//
// asd
//
this.setTitle("PDF bygger");
this.setLocation(new Point(35, 16));
this.setSize(new Dimension(424, 519));
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private static void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private static void Submit_actionPerformed(ActionEvent e)
{
System.out.println("\nSubmit_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
FormTextField f = new FormTextField();
FormTextField.FormTextField();
}
//
// TODO: Add any method code to meet your needs in the following area
//
//============================= Testing ================================//
//= =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it. =//
//======================================================================//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new asd();
}
//= End of Testing =
}
/****************************************************************/
/* FormTextField*/
/* */
/****************************************************************/
/*
* $Id: FormTextField.java 1742 2005-05-09 11:52:51Z blowagie $
* $Name$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfAppearance;
import com.lowagie.text.pdf.PdfBorderDictionary;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Generates an Acroform with a TextField
* @author blowagie
*/
public class FormTextField {
public JTextArea jTextArea1;
/**
* Generates an Acroform with a TextField
* @param args no arguments needed here
*/
public static void FormTextField() {
System.out.println("Textfield");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("testmedgui.pdf"));
// step 3: we open the document
document.open();
// step 4:
BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false);
PdfContentByte cb = writer.getDirectContent();
cb.moveTo(0, 0);
String text = "hej";
float fontSize = 12;
Color textColor = new GrayColor(0f);
PdfFormField field = PdfFormField.createTextField(writer, false, false, 0);
field.setWidget(new Rectangle(171, 750, 342, 769), PdfAnnotation.HIGHLIGHT_INVERT);
field.setFlags(PdfAnnotation.FLAGS_PRINT);
field.setFieldName("ATextField");
field.setValueAsString(text);
field.setDefaultValueAsString(text);
field.setBorderStyle(new PdfBorderDictionary(2, PdfBorderDictionary.STYLE_SOLID));
field.setPage();
PdfAppearance tp = cb.createAppearance(171, 19);
PdfAppearance da = (PdfAppearance)tp.getDuplicate();
da.setFontAndSize(helv, fontSize);
da.setColorFill(textColor);
field.setDefaultAppearanceString(da);
tp.beginVariableText();
tp.saveState();
tp.rectangle(2, 2, 167, 15);
tp.clip();
tp.newPath();
tp.beginText();
tp.setFontAndSize(helv, fontSize);
tp.setColorFill(textColor);
tp.setTextMatrix(4, 5);
tp.showText(text);
tp.endText();
tp.restoreState();
tp.endVariableText();
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
writer.addAnnotation(field);
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
}
