Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:java:java_pdf

Különbségek

A kiválasztott változat és az aktuális verzió közötti különbségek a következők.

Összehasonlító nézet linkje

Előző változat mindkét oldalonElőző változat
oktatas:programozas:java:java_pdf [2024/12/08 09:05] – [Helló Világ ékezetes] adminoktatas:programozas:java:java_pdf [2024/12/08 12:08] (aktuális) admin
Sor 3: Sor 3:
 ====== Java PDF generálás ====== ====== Java PDF generálás ======
  
-  * **Szerző:** Sallai András +  * [[oktatas:programozás:java:java_pdf:Itextpdf5]] 
-  * Copyright (c) 2014, Sallai András +  * [[oktatas:programozás:java:java_pdf:Itextpdf9]]
-  * Szerkesztve: 2014, 2015, 2016, 2024 +
-  * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] +
-  * Webhttps://szit.hu +
- +
-===== Bevezetés ===== +
- +
-Ez a leírás a iTextPdf használatát mutatja be, egy egyszerű példán +
-keresztül. Cél, hogy az ékezetes "ő" és "ű" betűk is problémamentesen +
-megjelenjenek. +
- +
- +
- +
- +
-===== Maven függőség ===== +
- +
-<code xml> +
-<dependency> +
-    <groupId>com.itextpdf</groupId> +
-    <artifactId>itextpdf</artifactId> +
-    <version>5.5.13.4</version> +
-</dependency> +
-</code> +
- +
-===== Szükséges jar fájl ===== +
- +
-A PDF generálásához a iTextPdfet használjuk. A kívánt jar fájl letöltéséhez +
-látogassuk meg a következő weboldalt: +
-  * http://itextpdf.com/ +
- +
- +
-A itextpdf-x.x.x.jar fájl szükséges. A dokumentum készítésekor: +
-  itextpdf-5.5.4.jar +
- +
- +
-API dokumentáció: +
-  * http://api.itextpdf.com/itext/ +
- +
- +
-===== Használható osztályok ===== +
- +
-A com.itextpdf.text csomag: +
-  * PdfReader - dokumentum beolvasása +
-  * PdfWriter - dokumentum elkészítése +
-A com.itextpdf.text.pdf csomag: +
-  * PdfStamper - dokumentum időbélyeg +
-===== Szükséges osztályok ===== +
-<code java+
-import com.itextpdf.text.Chunk; +
-import com.itextpdf.text.Document; +
-import com.itextpdf.text.Font; +
-import com.itextpdf.text.pdf.BaseFont; +
-import com.itextpdf.text.pdf.PdfWriter; +
-import java.io.FileOutputStream; +
-</code> +
- +
- +
-===== Helló Világ ===== +
- +
-<code java> +
-Document document = new Document(); +
-PdfWriter.getInstance(document, new FileOutputStream(dest)); +
-Rectangle one = new Rectangle(70,140); +
-Rectangle two = new Rectangle(700,400); +
-document.setPageSize(one); +
-document.setMargins(2, 2, 2, 2); +
-document.open(); +
-Paragraph p = new Paragraph("Hi"); +
-document.add(p); +
-document.setPageSize(two); +
-document.setMargins(20, 20, 20, 20); +
-document.newPage(); +
-document.add(p); +
-document.close(); +
-</code> +
- +
-===== Helló Világ ékezetes ===== +
- +
-<code java> +
-Document document = new Document(); +
-try { +
-    FileOutputStream os = new FileOutputStream("hellovilag.pdf");             +
-    PdfWriter.getInstance(document, os); +
-    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); +
-}catch(Exception ex) { +
-    //kivételkezelés             +
-+
-document.close(); +
-</code> +
- +
-A "ő" és az "ű" betűk megfelelő megjelenítéséhez a **BaseFont.CP1250** beállításra van szükség. Nélküle a betűk helyén semmi nem jelenik meg. +
- +
-A következő példában bekezdést teszünk a PDF fájlba. +
- +
-<code java Main.java> +
-package lan.zold; +
- +
-import java.io.FileOutputStream; +
- +
-import com.itextpdf.text.Document; +
-import com.itextpdf.text.Font; +
-import com.itextpdf.text.Paragraph; +
-import com.itextpdf.text.pdf.BaseFont; +
-import com.itextpdf.text.pdf.PdfWriter; +
- +
-public class Main { +
-    public static void main(String[] args) { +
-         +
-        try { +
-            Document document = new Document(); +
-            FileOutputStream fos = new FileOutputStream("adat.pdf"); +
-            PdfWriter.getInstance(document, fos); +
-            document.open();             +
- +
-            BaseFont baseFont = BaseFont.createFont( +
-                BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED +
-            ); +
-            Font font = new Font(baseFont);             +
-            document.add(new Paragraph("árvíztűrő tükörfúrógép", font)); +
-            document.addAuthor("Nagy János"); +
-            document.close(); +
-        }catch(Exception e) { +
-            e.printStackTrace(); +
-        } +
-    } +
-+
-</code> +
- +
-===== Vízjel, filter és fájlválasztó ===== +
-A vízjeles példa az ékezetes hosszú "ű" és hosszú "ő" karaktereket is kezeli. +
- +
-<code java Watermark.java> +
- +
-package model; +
- +
-import com.itextpdf.text.BaseColor; +
-import com.itextpdf.text.Document; +
-import com.itextpdf.text.DocumentException; +
-import com.itextpdf.text.Element; +
-import com.itextpdf.text.Font; +
-import com.itextpdf.text.Font.FontFamily; +
-import com.itextpdf.text.Phrase; +
-import com.itextpdf.text.pdf.BaseFont; +
-import com.itextpdf.text.pdf.ColumnText; +
-import com.itextpdf.text.pdf.PdfContentByte; +
-import com.itextpdf.text.pdf.PdfPageEventHelper; +
-import com.itextpdf.text.pdf.PdfWriter; +
-import java.io.IOException; +
- +
- +
-public class Watermark extends PdfPageEventHelper { +
-        Font font = null; +
-        Phrase watermark = null; +
-        public Watermark() { +
-            font = new Font(createBaseFont(), 70, Font.NORMAL, BaseColor.LIGHT_GRAY); +
-            watermark = new Phrase("VÍZJEL ÁRVÍZTŰRŐ", font); +
-        } +
-         +
-        private BaseFont createBaseFont() { +
-            BaseFont baseFont = null; +
-            try { +
-                baseFont =  tryCreateBaseFont(); +
-            }catch(DocumentException ex) { +
-                System.err.println("Hiba a dokumentum készítése közben!"); +
-            }catch(IOException ex) { +
-                System.err.println("Hiba a dokuementum kiírása közben!"); +
-            } +
-            return baseFont; +
-        } +
-        private BaseFont tryCreateBaseFont() throws DocumentException, +
-                IOException { +
-            BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, +
-                BaseFont.CP1250, BaseFont.EMBEDDED); +
-            return baseFont; +
-        } +
-         +
-        @Override +
-        public void onEndPage(PdfWriter writer, Document document) { +
-            PdfContentByte canvas = writer.getDirectContentUnder(); +
-            ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, watermark, 280, 420, 45); +
-        }     +
-     +
-+
-</code> +
- +
- +
-<code java Controller.java> +
- +
-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; +
-    } +
-+
- +
-</code> +
- +
- +
-<code java PdfFilter.java> +
-public class PdfFilter extends FileFilter { +
- +
- private final String[] okFileExtensions = +
-     new String[] {"pdf"}; +
-  +
- public boolean accept(File file) { +
- for (String extension : okFileExtensions) +
-+
- if (file.getName().toLowerCase().endsWith(extension)) +
-+
- return true; +
-+
-+
- return false; +
-+
- public String getDescription() +
-+
- return "PDF fájl"; +
- }     +
-+
- +
-</code> +
- +
-===== Linkek ===== +
- +
-==== PDF generátorok ==== +
- +
-  * [[http://itextpdf.com/|iTextPdf]] +
-    * [[http://itextpdf.com/book/examples.php|iTextPdf példa]] +
-  * [[http://pdfbox.apache.org/|PDFBox]] +
-  * [[http://pdfclown.org/|PDFClown]] +
-  * [[https://java.net/projects/pdf-renderer/|PDF-renderer]] +
-  * [[http://opensource.intarsys.de/home/en/index.php?n=OpenSource.JPod|jPod]] +
-  * [[http://pdfjet.com/os/edition.html|PDFjet]] +
-  * [[https://code.google.com/p/flying-saucer/|Flying Saucer]] +
- +
  
oktatas/programozas/java/java_pdf.1733645157.txt.gz · Utolsó módosítás: 2024/12/08 09:05 szerkesztette: admin