How2:将JPanel添加到Document,然后导出为PDF

有关编程的任何论坛的第一篇文章…我通常只是搜索直到我找到答案…但这次我真的卡住了……

这是问题…我有一个JPanel,最近发现itext为您提供了一种将Java GUI导出为PDF的方法…

我似乎无法理解itext的语言,也不知道如何将简单的JPanel添加到文档然后将该文档导出为PDF …这就是我目前所拥有的…

import java.io.FileOutputStream; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import java.awt.Color; import javax.swing.*; public class HelloWorld { public static void main(String[] args) { try { //Panel creation and setup JPanel panel = new JPanel(); //just to ensure that the panel has content... JLabel label = new JLabel("i am a label"); panel.add(label); panel.setSize(100,100); //so that even if the label doesnt get added... //i can see that the panel does panel.setBackground(Color.red); //my understanding of the code below: the virtual document Document document = new Document(); //my interpretation just writes the virtual pdf document to the hdd PdfWriter writer = PdfWriter.getInstance (document, new FileOutputStream("C:/test.pdf")); //begin editing the vpdf document.open(); //i wanna do something like this //document.add(panel); //end editing the vpdf document.close(); } catch (Exception e) { System.out.println(e); } } 

请帮助…我试着让代码尽可能短,以避免无用的东西……

提前谢谢……克雷格

您需要在面板上调用print并指定要打印的pdf图形,如下所示:

 JPanel panel = new JPanel(); //just to ensure that the panel has content... JLabel label = new JLabel("i am a label"); panel.add(label); panel.setSize(100,100); //so that even if the label doesnt get added... //i can see that the panel does panel.setBackground(Color.red); //the frame containing the panel JFrame f = new JFrame(); f.add(panel); f.setVisible(true); f.setSize(100,100); //print the panel to pdf Document document = new Document(); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\temp\\test.pdf")); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(500, 500); Graphics2D g2 = template.createGraphics(500, 500); panel.print(g2); g2.dispose(); contentByte.addTemplate(template, 30, 300); } catch (Exception e) { e.printStackTrace(); } finally{ if(document.isOpen()){ document.close(); } } 

好吧,我所做的是为我使用导出到PDF的代码添加WHOLE类文档…它非常简单,因为它只是将WHOLE面板导出为Graphics2D图像然后太PDF …我仍然在努力,因为我打算将文件导出为FDF PDF …但是现在这个文件出口很好……

这是:如果你需要帮助,我会非常愿意帮助你:

 package nexusInsurance; //TODO export to pdf to email import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; import java.awt.*; import java.io.FileNotFoundException; import java.sql.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileOutputStream; import java.util.logging.Level; import java.util.logging.Logger; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import javax.swing.border.*; public final class NewClaimNotification extends JPanel implements ActionListener { //misc GridBagLayout layout = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); static JDialog dialog = new JDialog(); Boolean updated = false; String timeOut = null; DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Border border = new LineBorder(Color.BLACK, 1); static JPanel mainPanel = new JPanel(); //menu components JMenuBar menuBar = new JMenuBar(); JMenu optionsMenu = new JMenu("Options"); JMenu helpMenu = new JMenu("Help"); JMenuItem updateItem = new JMenuItem("Update"); JMenuItem saveItem = new JMenuItem("Save"); JMenuItem cancelItem = new JMenuItem("Cancel"); JMenuItem notesItem = new JMenuItem("Notes"); JMenuItem emailItem = new JMenuItem("Email"); //header components JLabel heading = new JLabel("New Claim Notification"); JLabel lblJobCode = new JLabel("Job No."); JFormattedTextField txtTimeIn = new JFormattedTextField(); JTextField txtJobCode = new JTextField(); JTextField txtICCode = new JTextField(); JTextField txtCityCode = new JTextField(); //claim details components JLabel lblClaimCause = new JLabel("Nature of Claim"); JLabel lblLossDate = new JLabel("Date of Loss"); JLabel lblPHName = new JLabel("Policy Holder"); JComboBox ddClaimCause = new JComboBox(); JComboBox ddPHName = new JComboBox(); JTextField txtLossDate = new JTextField(); JTextField txtPolCode = new JTextField(); JTextField txtExcess = new JTextField(15); JTextField txtClaimCode = new JTextField(); JTextField txtCons = new JTextField(); JTextField txtICName = new JTextField(30); JTextField txtBro = new JTextField(); JTextField txtManAg = new JTextField(); //SP components JLabel lblAssessor = new JLabel("Assessor"); JLabel lblApp = new JLabel("Appointment"); JComboBox ddAssessor = new JComboBox();; JTextField txtHandler = new JTextField(); JTextField txtApp = new JTextField(); //risk address components JLabel lblContact = new JLabel("Contact"); JTextField txtUnit = new JTextField(); JTextField txtStreet = new JTextField(); JTextField txtSuburb = new JTextField(); JTextField txtCity = new JTextField(); JTextField txtContact = new JTextField(); JTextField txtHomeCell = new JTextField(); JTextField txtHome = new JTextField(); JTextField txtWork = new JTextField(); JTextField txtHomeFax = new JTextField(); JTextField txtOther = new JTextField(); JTextField txtHomeEmail = new JTextField(); //banner components JLabel lblCompanyBanner = new JLabel(); ImageIcon companyBanner = new ImageIcon(getClass().getResource("companyBannerLandscape.PNG")); Statement stmt = null; String connectionUrl = null; static ResultSet rs = null; Connection con = null; String SQL = null; Boolean status = false; private NewClaimNotification(String func, String jobCode) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); connectionUrl = "jdbc:mysql://localhost/nexus_insurance?user=root&password=P7683286r!"; con = DriverManager.getConnection(connectionUrl); stmt = con.createStatement(); PrepareForm(func, jobCode); setLayout(layout); c.anchor = GridBagConstraints.LINE_START; cancelItem.setActionCommand("cancel"); cancelItem.addActionListener(this); notesItem.setActionCommand("viewClaimNotes"); notesItem.addActionListener(this); emailItem.setActionCommand("email"); emailItem.addActionListener(this); optionsMenu.add(notesItem); optionsMenu.add(cancelItem); optionsMenu.add(emailItem); menuBar.add(optionsMenu); menuBar.add(helpMenu); c.gridy = 0; c.gridx = 0; menuBar.setPreferredSize(new Dimension(1040,20)); add(menuBar,c); c.gridy = 1; lblCompanyBanner.setIcon(new ImageIcon(getClass().getResource("companyBannerLandscape.PNG"))); add(lblCompanyBanner,c); c.gridy = 2; c.insets = new Insets(0,30,0,0); add(HeaderPanel(),c); c.gridy = 3; add(ClaimDetailsPanel(),c); c.gridy = 4; c.insets = new Insets(5,30,5,0); add(RiskAddressPanel(),c); c.gridy = 5; c.insets = new Insets(0,30,10,0); add(SPPanel(),c); setBackground(Color.white); setBorder(border); } private JPanel HeaderPanel() { JPanel panel = new JPanel(); GridBagConstraints x = new GridBagConstraints(); panel.setLayout(layout); x.anchor = GridBagConstraints.LINE_START; x.insets = new Insets(0,0,0,440); x.anchor = GridBagConstraints.LINE_START; x.gridy = 0; x.gridx = 0; x.gridheight = 2; heading.setFont(new Font(null, Font.BOLD, 30)); panel.add(heading, x); x.gridheight = 1; x.insets = new Insets(0,0,0,0); x.gridx = 1; lblJobCode.setForeground(Color.red); panel.add(lblJobCode, x); x.gridx = 2; txtICCode.setEnabled(false); txtICCode.setColumns(4); panel.add(txtICCode, x); x.gridx = 3; txtCityCode.setColumns(4); txtCityCode.setEnabled(false); panel.add(txtCityCode, x); x.gridx = 4; txtJobCode.setColumns(5); panel.add(txtJobCode, x); x.gridy = 1; x.gridx = 1; panel.add(new JLabel("Date"), x); x.gridx = 2; x.gridwidth = 3; txtTimeIn.setEnabled(false); x.fill = GridBagConstraints.HORIZONTAL; panel.add(txtTimeIn, x); panel.setBackground(Color.white); return panel; } private JPanel ClaimDetailsPanel() { JPanel panel = new JPanel(); GridBagConstraints x = new GridBagConstraints(); panel.setLayout(layout); x.anchor = GridBagConstraints.LINE_START; x.insets = new Insets(0,5,0,10); x.gridx = 0; x.gridy = 0; lblClaimCause.setForeground(Color.red); panel.add(lblClaimCause,x); x.gridy = 1; lblLossDate.setForeground(Color.red); panel.add(lblLossDate,x); x.gridy = 2; lblPHName.setForeground(Color.red); panel.add(lblPHName, x); x.gridy = 3; panel.add(new JLabel("Policy Code"), x); x.insets = new Insets(0,0,0,88); x.fill = GridBagConstraints.HORIZONTAL; x.gridx = 1; x.gridy = 0; ddClaimCause.setEditable(true); panel.add(ddClaimCause,x); x.gridy = 1; panel.add(txtLossDate,x); x.gridy = 2; panel.add(ddPHName,x); x.gridy = 3; txtPolCode.setEnabled(false); panel.add(txtPolCode,x); x.fill = GridBagConstraints.NONE; x.insets = new Insets(0,0,0,10); x.gridx = 2; x.gridy = 0; panel.add(new JLabel("Excess"),x); x.gridy = 1; panel.add(new JLabel("Claim Code"),x); x.gridy = 2; panel.add(new JLabel("Consultant"),x); x.gridy = 3; panel.add(new JLabel("Insurance Co."),x); x.insets = new Insets(0,0,0,95); x.fill = GridBagConstraints.HORIZONTAL; x.gridx = 3; x.gridy = 0; panel.add(txtExcess,x); x.gridy = 1; panel.add(txtClaimCode,x); x.gridy = 2; txtCons.setEnabled(false); panel.add(txtCons,x); x.gridy = 3; x.gridwidth = 3; txtICName.setEnabled(false); panel.add(txtICName,x); x.gridwidth = 1; x.fill = GridBagConstraints.NONE; x.insets = new Insets(0,0,0,10); x.gridx = 4; x.gridy = 0; panel.add(new JLabel("Broker"), x); x.gridy = 1; panel.add(new JLabel("Agent"), x); x.insets = new Insets(0,0,0,5); x.fill = GridBagConstraints.HORIZONTAL; x.gridx = 5; x.gridy = 0; txtBro.setColumns(15); txtBro.setEnabled(false); panel.add(txtBro, x); x.gridy = 1; txtManAg.setEnabled(false); panel.add(txtManAg, x); x.fill = GridBagConstraints.NONE; panel.setBackground(Color.white); panel.setBorder(BorderFactory.createTitledBorder("Claim Details")); return panel; } private JPanel RiskAddressPanel() { JPanel panel = new JPanel(); GridBagConstraints x = new GridBagConstraints(); panel.setLayout(layout); x.anchor = GridBagConstraints.LINE_START; x.insets = new Insets(0,5,0,55); x.gridx = 0; x.gridy = 0; panel.add(new JLabel("Unit"),x); x.gridy = 1; panel.add(new JLabel("Street"),x); x.gridy = 2; panel.add(new JLabel("Suburb"),x); x.gridy = 3; panel.add(new JLabel("City"),x); x.insets = new Insets(0,0,0,86); x.fill = GridBagConstraints.HORIZONTAL; x.gridx = 1; x.gridy = 0; txtUnit.setColumns(17); panel.add(txtUnit,x); x.gridy = 1; txtStreet.setEnabled(false); panel.add(txtStreet,x); x.gridy = 2; txtSuburb.setEnabled(false); panel.add(txtSuburb,x); x.gridy = 3; txtCity.setEnabled(false); panel.add(txtCity,x); x.fill = GridBagConstraints.NONE; x.insets = new Insets(0,0,0,43); x.gridx = 2; x.gridy = 0; lblContact.setForeground(Color.red); panel.add(lblContact,x); x.gridy = 1; panel.add(new JLabel("Cell"),x); x.gridy = 2; panel.add(new JLabel("Home"),x); x.gridy = 3; panel.add(new JLabel("Work"),x); x.insets = new Insets(0,0,0,98); x.fill = GridBagConstraints.HORIZONTAL; x.gridx = 3; x.gridy = 0; txtContact.setColumns(15); panel.add(txtContact,x); x.gridy = 1; panel.add(txtHomeCell,x); x.gridy = 2; panel.add(txtHome,x); x.gridy = 3; panel.add(txtWork,x); x.fill = GridBagConstraints.NONE; x.insets = new Insets(0,0,0,17); x.gridx = 4; x.gridy = 1; panel.add(new JLabel("Other"),x); x.gridy = 2; panel.add(new JLabel("Fax"),x); x.gridy = 3; panel.add(new JLabel("Email"),x); x.insets = new Insets(0,0,0,5); x.fill = GridBagConstraints.HORIZONTAL; x.gridx = 5; x.gridy = 1; txtOther.setColumns(15); panel.add(txtOther,x); x.gridy = 2; panel.add(txtHomeFax,x); x.gridy = 3; panel.add(txtHomeEmail,x); x.fill = GridBagConstraints.NONE; panel.setBackground(Color.white); panel.setBorder(BorderFactory.createTitledBorder("Risk Address")); return panel; } private JPanel SPPanel() { JPanel panel = new JPanel(); GridBagConstraints x = new GridBagConstraints(); panel.setLayout(layout); x.anchor = GridBagConstraints.LINE_START; x.insets = new Insets(0,5,0,53); x.gridy = 0; x.gridx = 0; panel.add(new JLabel("Handler"),x); x.insets = new Insets(0,0,0,84); x.fill = GridBagConstraints.HORIZONTAL; x.gridy = 0; x.gridx = 1; txtHandler.setEnabled(false); txtHandler.setColumns(17); panel.add(txtHandler,x); x.fill = GridBagConstraints.NONE; x.insets = new Insets(0,0,0,33); x.gridx = 2; lblAssessor.setForeground(Color.red); panel.add(lblAssessor,x); x.insets = new Insets(0,0,0,77); x.gridx = 3; panel.add(ddAssessor,x); x.insets = new Insets(0,0,0,14); x.gridx = 4; lblApp.setForeground(Color.red); panel.add(lblApp,x); x.insets = new Insets(0,0,0,5); x.gridx = 5; txtApp.setColumns(15); panel.add(txtApp,x); x.fill = GridBagConstraints.NONE; x.insets = new Insets(0,0,0,0); panel.setBackground(Color.white); panel.setBorder(BorderFactory.createTitledBorder("Service Provider Details")); return panel; } private void PrepareForm(String func,String jobCode) throws SQLException, ClassNotFoundException { txtTimeIn.setEnabled(false); txtTimeIn.setText(dateFormat.format(new Date())); txtHandler.setEnabled(false); txtHandler.setText(Login.fullName); txtJobCode.setText(jobCode); SQL = "SELECT DISTINCT(claimCause) FROM claims ORDER BY claimCause"; rs = stmt.executeQuery(SQL); ddClaimCause.addItem(""); while(rs.next()) {ddClaimCause.addItem(rs.getString(1));} SQL = "SELECT CONCAT(firstName, ' ', lastName) as 'name' FROM address_book WHERE relationship LIKE '%Assessor%' ORDER BY name"; rs = stmt.executeQuery(SQL); ddAssessor.addItem(" 

它的代码很多,但它非常容易……只是一大堆简单的东西……我不是最先进的程序员……无论如何… lemme知道你是否需要帮助Boro