Search in sources :

Example 21 with Document

use of com.itextpdf.layout.Document in project i7js-highlevel by itext.

the class C02E11_JekyllHydeV7 method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf);
    // Set column parameters
    float offSet = 36;
    float gutter = 23;
    float columnWidth = (PageSize.A4.getWidth() - offSet * 2) / 2 - gutter;
    float columnHeight = PageSize.A4.getHeight() - offSet * 2;
    // Define column areas
    Rectangle[] columns = { new Rectangle(offSet, offSet, columnWidth, columnHeight), new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight) };
    DocumentRenderer renderer = new MyColumnRenderer(document, columns);
    document.setRenderer(renderer);
    PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
    document.setTextAlignment(TextAlignment.JUSTIFIED).setFont(font).setHyphenation(new HyphenationConfig("en", null, 3, 3));
    BufferedReader br = new BufferedReader(new FileReader(SRC));
    String line;
    Paragraph p;
    boolean title = true;
    AreaBreak nextPage = new AreaBreak(AreaBreakType.NEXT_PAGE);
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line);
        if (title) {
            p.setFont(bold).setFontSize(12);
            title = false;
        } else {
            p.setFirstLineIndent(36);
        }
        if (line.isEmpty()) {
            document.add(nextPage);
            title = true;
        }
        document.add(p);
    }
    br.close();
    renderer.flush();
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) Rectangle(com.itextpdf.kernel.geom.Rectangle) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) AreaBreak(com.itextpdf.layout.element.AreaBreak) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) DocumentRenderer(com.itextpdf.layout.renderer.DocumentRenderer) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph) PdfFont(com.itextpdf.kernel.font.PdfFont) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 22 with Document

use of com.itextpdf.layout.Document in project i7js-highlevel by itext.

the class C02E13_JekyllHydeV9 method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf, PageSize.A4, false);
    PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
    document.setTextAlignment(TextAlignment.JUSTIFIED).setHyphenation(new HyphenationConfig("en", null, 3, 3)).setFont(font).setFontSize(11);
    BufferedReader br = new BufferedReader(new FileReader(SRC));
    String line;
    Paragraph p;
    boolean title = true;
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line);
        p.setKeepTogether(true);
        if (title) {
            p.setFont(bold).setFontSize(12);
            title = false;
        } else {
            p.setFirstLineIndent(36);
        }
        if (line.isEmpty()) {
            p.setMarginBottom(12);
            title = true;
        } else {
            p.setMarginBottom(0);
        }
        document.add(p);
    }
    br.close();
    int n = pdf.getNumberOfPages();
    Paragraph footer;
    for (int page = 1; page <= n; page++) {
        footer = new Paragraph(String.format("Page %s of %s", page, n));
        document.showTextAligned(footer, 297.5f, 20, page, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
    }
    // Close document
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfFont(com.itextpdf.kernel.font.PdfFont) BufferedReader(java.io.BufferedReader) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) FileReader(java.io.FileReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph)

Example 23 with Document

use of com.itextpdf.layout.Document in project i7js-highlevel by itext.

the class C03E04_JekyllHydeTabsV4 method createPdf.

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf, PageSize.A4.rotate());
    float[] stops = new float[] { 80, 120, 580, 590, 720 };
    List<TabStop> tabstops = new ArrayList();
    tabstops.add(new TabStop(stops[0], TabAlignment.CENTER, new DottedLine()));
    tabstops.add(new TabStop(stops[1], TabAlignment.LEFT));
    tabstops.add(new TabStop(stops[2], TabAlignment.RIGHT, new SolidLine(0.5f)));
    tabstops.add(new TabStop(stops[3], TabAlignment.LEFT));
    TabStop anchor = new TabStop(stops[4], TabAlignment.ANCHOR, new DashedLine());
    anchor.setTabAnchor(' ');
    tabstops.add(anchor);
    PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
    for (int i = 0; i < stops.length; i++) {
        pdfCanvas.moveTo(document.getLeftMargin() + stops[i], 0);
        pdfCanvas.lineTo(document.getLeftMargin() + stops[i], 595);
    }
    pdfCanvas.stroke();
    List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
    for (List<String> record : resultSet) {
        Paragraph p = new Paragraph();
        p.addTabStops(tabstops);
        p.add(record.get(0).trim()).add(new Tab()).add(record.get(1).trim()).add(new Tab()).add(record.get(2).trim()).add(new Tab()).add(record.get(3).trim()).add(new Tab()).add(record.get(4).trim()).add(new Tab()).add(record.get(5).trim() + " \'");
        document.add(p);
    }
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ArrayList(java.util.ArrayList) TabStop(com.itextpdf.layout.element.TabStop) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph) Tab(com.itextpdf.layout.element.Tab) DottedLine(com.itextpdf.kernel.pdf.canvas.draw.DottedLine) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) SolidLine(com.itextpdf.kernel.pdf.canvas.draw.SolidLine) CsvTo2DList(com.itextpdf.highlevel.util.CsvTo2DList) ArrayList(java.util.ArrayList) List(java.util.List) DashedLine(com.itextpdf.kernel.pdf.canvas.draw.DashedLine)

Example 24 with Document

use of com.itextpdf.layout.Document in project i7js-highlevel by itext.

the class C05E01_MyFirstTable method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf);
    Table table = new Table(new float[] { 1, 1, 1 });
    table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
    table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");
    document.add(table);
    document.close();
}
Also used : Table(com.itextpdf.layout.element.Table) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) Cell(com.itextpdf.layout.element.Cell) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph)

Example 25 with Document

use of com.itextpdf.layout.Document in project i7js-highlevel by itext.

the class C05E02_ColumnWidths4 method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf);
    Table table = new Table(UnitValue.createPercentArray(new float[] { 2, 1, 1 }));
    table.setWidth(UnitValue.createPercentValue(80));
    table.setHorizontalAlignment(HorizontalAlignment.CENTER);
    table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
    table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");
    document.add(table);
    document.close();
}
Also used : Table(com.itextpdf.layout.element.Table) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) Cell(com.itextpdf.layout.element.Cell) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph)

Aggregations

PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)132 Document (com.itextpdf.layout.Document)132 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)130 Paragraph (com.itextpdf.layout.element.Paragraph)104 PdfFont (com.itextpdf.kernel.font.PdfFont)39 HyphenationConfig (com.itextpdf.layout.hyphenation.HyphenationConfig)31 BufferedReader (java.io.BufferedReader)26 FileReader (java.io.FileReader)26 AreaBreak (com.itextpdf.layout.element.AreaBreak)25 Image (com.itextpdf.layout.element.Image)25 Table (com.itextpdf.layout.element.Table)25 Cell (com.itextpdf.layout.element.Cell)21 List (java.util.List)20 CsvTo2DList (com.itextpdf.highlevel.util.CsvTo2DList)19 File (java.io.File)16 SolidBorder (com.itextpdf.layout.borders.SolidBorder)14 ArrayList (java.util.ArrayList)14 Tab (com.itextpdf.layout.element.Tab)13 Text (com.itextpdf.layout.element.Text)13 Rectangle (com.itextpdf.kernel.geom.Rectangle)12