Buscar..


Observaciones

En iText 5, no puede usar el método add() para agregar un Paragraph a un Document si desea organizar el contenido en columnas. No podemos reutilizar el código del ejemplo Text2Pdf.java (iText 5) .

En lugar de eso, tenemos que crear un objeto ColumnText , debemos agregar todos los objetos de Paragraph a este objeto y, una vez que hayamos terminado de agregar todo el contenido, podemos comenzar a representar ese contenido utilizando el método go() . Mientras lo hacemos, tenemos que hacer un seguimiento de las columnas y crear nuevas páginas cuando sea necesario.

Lo que arreglamos en iText 7:

Con iText 7, podemos copiar y pegar el código del ejemplo de Text2Pdf.java (iText 7) . Podemos continuar usando el método add() la misma manera que lo hicimos antes. Si queremos representar el contenido en dos columnas en lugar de hacerlo en una, simplemente debemos cambiar el procesador de documentos:

Rectangle[] columns = {
    new Rectangle(36, 36, 254, 770),
    new Rectangle(305, 36, 254, 770)};
document.setRenderer(new ColumnDocumentRenderer(document, columns));

¿Quiere saber más?

Lea Trabajar con el elemento raíz, que es el capítulo 5 en el tutorial de iText 7: Building Blocks . ¡Consigue el ebook gratis!

Text2PdfColumns.java (iText 5)

Supongamos que tenemos el siguiente archivo de texto: jekyll_hyde.txt

¿Cómo lo convertimos a un PDF que se parece a esto?

introduzca la descripción de la imagen aquí

Cuando uses iText 5, necesitarías un código como este:

public void createPdf(String dest)
throws DocumentException, IOException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    ColumnText ct = new ColumnText(writer.getDirectContent());
    BufferedReader br = new BufferedReader(new FileReader(TEXT));
    String line;
    Paragraph p;
    Font normal = new Font(FontFamily.TIMES_ROMAN, 12);
    Font bold = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    boolean title = true;
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line, title ? bold : normal);
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        title = line.isEmpty();
        ct.addElement(p);
    }
    Rectangle[] columns = {
        new Rectangle(36, 36, 290, 806), new Rectangle(305, 36, 559, 806)
    };
    int c = 0;
    int status = ColumnText.START_COLUMN;
    while (ColumnText.hasMoreText(status)) {
        ct.setSimpleColumn(columns[c]);
        status = ct.go();
        if (++c == 2) {
            document.newPage();
            c = 0;
        }
    }
    document.close();
}

Fuente: developers.itextpdf.com

Text2PdfColumns.java (iText 7)

Supongamos que tiene el siguiente archivo de texto: jekyll_hyde.txt

¿Cómo lo convertimos a un PDF que se parece a esto?

introduzca la descripción de la imagen aquí

Cuando uses iText 7, necesitarías un código como este:

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf)
        .setTextAlignment(TextAlignment.JUSTIFIED);
    Rectangle[] columns = {
        new Rectangle(36, 36, 254, 770),
        new Rectangle(305, 36, 254, 770)};
    document.setRenderer(new ColumnDocumentRenderer(document, columns));
    BufferedReader br = new BufferedReader(new FileReader(TEXT));
    String line;
    PdfFont normal = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
    boolean title = true;
    while ((line = br.readLine()) != null) {
        document.add(new Paragraph(line).setFont(title ? bold : normal));
        title = line.isEmpty();
    }
    document.close();
}

Fuente: developers.itextpdf.com y el tutorial de iText 7: Building Blocks .



Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow