Search in sources :

Example 76 with Document

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

the class C02E12_JekyllHydeV8 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);
    Text totalPages = new Text("This document has {totalpages} pages.");
    IRenderer renderer = new TextRenderer(totalPages);
    totalPages.setNextRenderer(renderer);
    document.add(new Paragraph(totalPages));
    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();
    String total = renderer.toString().replace("{totalpages}", String.valueOf(pdf.getNumberOfPages()));
    ((TextRenderer) renderer).setText(total);
    ((Text) renderer.getModelElement()).setNextRenderer(renderer);
    document.relayout();
    // Close document
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) Text(com.itextpdf.layout.element.Text) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph) IRenderer(com.itextpdf.layout.renderer.IRenderer) PdfFont(com.itextpdf.kernel.font.PdfFont) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) TextRenderer(com.itextpdf.layout.renderer.TextRenderer)

Example 77 with Document

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

the class C03E01_JekyllHydeTabsV1 method createPdf.

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf, PageSize.A4.rotate());
    PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
    for (int i = 1; i <= 10; i++) {
        pdfCanvas.moveTo(document.getLeftMargin() + i * 50, 0);
        pdfCanvas.lineTo(document.getLeftMargin() + i * 50, 595);
    }
    pdfCanvas.stroke();
    List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
    for (List<String> record : resultSet) {
        Paragraph p = new Paragraph();
        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) Tab(com.itextpdf.layout.element.Tab) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) CsvTo2DList(com.itextpdf.highlevel.util.CsvTo2DList) List(java.util.List) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph)

Example 78 with Document

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

the class C03E05_JekyllHydeTabsV5 method createPdf.

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf, PageSize.A4);
    float[] stops = new float[] { 70, 90, 420, 430, 520 };
    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], 842);
    }
    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 79 with Document

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

the class C03E10_MaryReillyV3 method createPdf.

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf);
    Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
    document.add(p);
    Image img1 = new Image(ImageDataFactory.create(MARY));
    document.add(img1);
    Image img2 = new Image(ImageDataFactory.create(MARY));
    document.add(img2);
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) Image(com.itextpdf.layout.element.Image) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph)

Example 80 with Document

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

the class C03E18_ImageTypes method createPdf.

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf);
    // raw
    byte[] data = new byte[256 * 3];
    for (int i = 0; i < 256; i++) {
        data[i * 3] = (byte) (255 - i);
        data[i * 3 + 1] = (byte) (255 - i);
        data[i * 3 + 2] = (byte) i;
    }
    ImageData raw = ImageDataFactory.create(256, 1, 3, 8, data, null);
    Image img = new Image(raw);
    img.scaleAbsolute(256, 10);
    document.add(img);
    // JPEG2000
    Image img1 = new Image(ImageDataFactory.create(TEST1));
    document.add(img1);
    document.add(new AreaBreak());
    // BMP
    Image img2 = new Image(ImageDataFactory.create(TEST2));
    img2.setMarginBottom(10);
    document.add(img2);
    // PNG
    Image img3 = new Image(ImageDataFactory.create(TEST3));
    img3.setMarginBottom(10);
    document.add(img3);
    // Transparent PNG
    Image img4 = new Image(ImageDataFactory.create(TEST4));
    img4.setBorderLeft(new SolidBorder(6));
    document.add(img4);
    // GIF
    Image img5 = new Image(ImageDataFactory.create(TEST5));
    img5.setBackgroundColor(ColorConstants.LIGHT_GRAY);
    document.add(img5);
    // AWT
    java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(TEST5);
    Image awt = new Image(ImageDataFactory.create(awtImage, java.awt.Color.yellow));
    awt.setMarginTop(10);
    document.add(awt);
    // JBIG2
    Image img6 = new Image(ImageDataFactory.create(TEST6));
    document.add(img6);
    // TIFF
    Image img7 = new Image(ImageDataFactory.create(TEST7));
    document.add(img7);
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ImageData(com.itextpdf.io.image.ImageData) AreaBreak(com.itextpdf.layout.element.AreaBreak) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) Image(com.itextpdf.layout.element.Image) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) SolidBorder(com.itextpdf.layout.borders.SolidBorder) java.awt(java.awt)

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