📄 tr2.java
字号:
import javax.swing.JFrame;import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.PdfWriter;import com.lowagie.text.pdf.*;/** * This code was generated using CloudGarden's Jigloo SWT/Swing GUI Builder, * which is free for non-commercial use. If Jigloo is being used commercially * (ie, by a corporation, company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. Please visit * www.cloudgarden.com for details. Use of Jigloo implies acceptance of these * licensing terms. ************************************* A COMMERCIAL LICENSE * HAS NOT BEEN PURCHASED for this machine, so Jigloo or this code cannot be * used legally for any corporate or commercial purpose. * ************************************* */import javax.swing.JPanel;import javax.swing.JButton;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.BorderLayout;import javax.swing.JTextField;import javax.swing.JLabel;public class tr2 extends javax.swing.JDialog { private JPanel jPanel1; private JButton op; private JLabel wat; private JTextField wa; private JTextField im; private JLabel opat; private JTextField opa; private JLabel pat; private JTextField pa; private JLabel tex; private JLabel hea; private JTextField he; private JButton qt; String sFile; String temp = "\n"; String[] temps = new String[10]; public String head; // 增加一个水印 public String outpath; public String[] imgpath = new String[10]; public String waterpath; int i = 0; int k = 0; int flag = 0; /** * Auto-generated main method to display this JDialog */ public static void main(String[] args) { JFrame frame = new JFrame(); tr2 inst = new tr2(frame); inst.setVisible(true); } public tr2(JFrame frame) { super(frame); initGUI(); } private void initGUI() { try { setSize(400, 300); { jPanel1 = new JPanel(); this.getContentPane().add(jPanel1, BorderLayout.CENTER); jPanel1.setLayout(null); jPanel1.setPreferredSize(new java.awt.Dimension(392, 273)); { op = new JButton(); jPanel1.add(op); op.setText("execute"); op.setBounds(57, 238, 97, 30); op.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { flag++; try { head = he.getText(); outpath = opa.getText(); if (outpath.equals("")) outpath = "res.pdf"; waterpath = wa.getText(); imgpath[i++] = im.getText(); Document document = new Document(PageSize.A4, 50, 50, 100, 50); Rectangle pageRect = document.getPageSize(); FileOutputStream fo = new FileOutputStream( outpath); PdfWriter.getInstance(document, fo); //创建汉字字体 BaseFont bfSong = BaseFont.createFont( "STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontSong = new Font(bfSong, 10, Font.NORMAL); HeaderFooter header = new HeaderFooter( new Phrase(head, fontSong), false); header.setBorder(2); header.setAlignment(Element.ALIGN_RIGHT); document.setHeader(header); if (!waterpath.equals("")) { try { Watermark watermark = new Watermark( Image.getInstance(waterpath), pageRect.left() + 50, pageRect .top() - 85); watermark.scalePercent(50); document.add(watermark); } catch (Exception e) { System.err.println("请查看水印是否在正确的位置?"); } // 为页增加页脚信息 } HeaderFooter footer = new HeaderFooter( new Phrase("第 ", fontSong), new Phrase( " 页", fontSong)); footer.setAlignment(Element.ALIGN_CENTER); footer.setBorder(1); document.setFooter(footer); // 打开文档 document.open(); sFile = pa.getText(); if (!sFile.equals("")) { File inputFile = new File(sFile);//定义读取源文件 FileReader in = new FileReader(inputFile); //将文件输入流构造到缓存 BufferedReader bin = new BufferedReader(in); String c = ""; //循环读取文件和写入文件 while ((c = bin.readLine()) != null) { temp = temp + c + "\n"; } temps[k++] = temp; temp = "\n"; bin.close(); } for (int r = 0; r < flag && !(sFile.equals("") && im.getText() .equals("")); r++) { document .add(new Phrase(temps[r], fontSong)); if (!imgpath[r].equals("")) document.add(Image .getInstance(imgpath[r])); } document.close(); } catch (Exception e) { //System.out.println(e.getStackTrace()); e.printStackTrace(); } } }); } { qt = new JButton(); jPanel1.add(qt); qt.setText("quit"); qt.setBounds(183, 238, 101, 30); qt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { dispose(); System.exit(0); //TODO add your code for qt.actionPerformed } }); } { he = new JTextField(); jPanel1.add(he); he.setBounds(90, 146, 238, 26); he.setFont(new java.awt.Font("Dialog", 1, 18)); } { hea = new JLabel(); jPanel1.add(hea); hea.setText("页眉"); hea.setBounds(1, 145, 60, 30); } { tex = new JLabel(); jPanel1.add(tex); tex.setText("图片路径"); tex.setBounds(1, 192, 83, 30); } { pa = new JTextField(); jPanel1.add(pa); pa.setBounds(90, 54, 238, 27); pa.setFont(new java.awt.Font("Dialog", 1, 18)); } { pat = new JLabel(); jPanel1.add(pat); pat.setText("输入路径"); pat.setBounds(2, 43, 60, 30); } { opa = new JTextField(); jPanel1.add(opa); opa.setBounds(91, 95, 235, 29); opa.setFont(new java.awt.Font("Dialog", 1, 18)); } { opat = new JLabel(); jPanel1.add(opat); opat.setText("输出路径"); opat.setBounds(0, 91, 60, 30); } { im = new JTextField(); jPanel1.add(im); im.setBounds(90, 194, 237, 26); im.setFont(new java.awt.Font("Dialog", 1, 18)); } { wa = new JTextField(); jPanel1.add(wa); wa.setBounds(91, 14, 235, 27); wa.setFont(new java.awt.Font("Dialog", 1, 18)); } { wat = new JLabel(); jPanel1.add(wat); wat.setText("水印路径"); wat.setBounds(3, 15, 60, 30); } } } catch (Exception e) { //e.printStackTrace(); System.out.print("error"); } } /** * Auto-generated method for setting the popup menu for a component */ private void setComponentPopupMenu(final java.awt.Component parent, final javax.swing.JPopupMenu menu) { parent.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { if (e.isPopupTrigger()) menu.show(parent, e.getX(), e.getY()); } public void mouseReleased(java.awt.event.MouseEvent e) { if (e.isPopupTrigger()) menu.show(parent, e.getX(), e.getY()); } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -