package controller; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.File; import javax.swing.JFileChooser; import model.PdfFilter; import model.Watermark; public class Controller.java { public void genPdfButtonActionPerformed(java.awt.event.ActionEvent evt) { String filePath = fileChoose(); if(filePath != "") { genPdf(filePath); } } private void genPdf(String filePath) { try { tryGenPdf(filePath); }catch(FileNotFoundException ex) { System.err.println("A fájl nem található!"); }catch(DocumentException ex){ System.err.println("Hiba a dokumentum készítése során!"); }catch(IOException ex){ System.err.println("Hiba a kiírás során!"); } } private void tryGenPdf(String filePath) throws FileNotFoundException, DocumentException, IOException { Document document = new Document(); FileOutputStream os = new FileOutputStream(filePath); PdfWriter pdfWriter = PdfWriter.getInstance(document, os); pdfWriter.setPageEvent(new Watermark()); //vízjel hozzáadása document.open(); BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED); Font font = new Font(baseFont, 12, Font.NORMAL); Chunk chunk = new Chunk("Árvíztűrő tükörfúrógép.", font); document.add(chunk); document.close(); } private String fileChoose() { JFileChooser fileChooser = new JFileChooser(); String filePath = ""; //Itt adjuk hozzá a filtert (2 sor): fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.addChoosableFileFilter(new PdfFilter()); if( fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION ) { filePath = fileChooser.getSelectedFile().getPath(); } return filePath; } }