Example usage for com.lowagie.text Document add

List of usage examples for com.lowagie.text Document add

Introduction

In this page you can find the example usage for com.lowagie.text Document add.

Prototype


public boolean add(Element element) throws DocumentException 

Source Link

Document

Adds an Element to the Document.

Usage

From source file:at.htlpinkafeld.beans.AlleAbwesenheitenBean.java

/**
 * pdf post processing//  w ww  .java 2s. co  m
 *
 * @param document pdf document
 * @throws DocumentException
 */
public void postProcessPDF(Object document) throws DocumentException {
    Document pdf = (Document) document;

    pdf.add(new Paragraph("\n\nStand: " + LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))));
    pdf.close();
}

From source file:at.htlpinkafeld.beans.JahresuebersichtBean.java

/**
 * pre processes the PDF for creating/*from w w  w .jav  a 2  s.  c o m*/
 *
 * @param document pdf-doc
 * @throws DocumentException may be thrown
 */
public void preProcessPDF(Object document) throws DocumentException {
    Document pdf = (Document) document;
    pdf.open();
    pdf.setPageSize(PageSize.A4);

    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(100);
    table.addCell(getCell("Jahresbersicht - " + selectedYear.getYear(), PdfPCell.ALIGN_LEFT));
    table.addCell(getCell("", PdfPCell.ALIGN_CENTER));
    table.addCell(getCell("von " + selectedUser.getPersName(), PdfPCell.ALIGN_RIGHT));
    pdf.add(table);

    pdf.add(new Paragraph("\n"));
}

From source file:at.htlpinkafeld.beans.JahresuebersichtBean.java

/**
 * post processes the PDF for creating//from   w  ww  . j  a va  2  s  . c om
 *
 * @param document pdf-doc
 * @throws DocumentException may be thrown
 */
public void postProcessPDF(Object document) throws DocumentException {
    Document pdf = (Document) document;
    pdf.add(new Paragraph("\nStand: " + LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))));
    pdf.close();
}

From source file:at.htlpinkafeld.beans.UserDetailsBean.java

public void preProcessPDF(Object document) throws DocumentException {
        Document pdf = (Document) document;
        pdf.setPageSize(PageSize.A4.rotate());
        pdf.open();/*  w ww .  j  a  v a  2s. co  m*/

        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100);
        table.addCell(getCell("Monatsbersicht - " + selectedDate.format(DateTimeFormatter.ofPattern("MM.yyyy")),
                PdfPCell.ALIGN_LEFT));
        table.addCell(getCell("", PdfPCell.ALIGN_CENTER));
        table.addCell(getCell("von " + selectedUser, PdfPCell.ALIGN_RIGHT));
        pdf.add(table);

        pdf.add(new Paragraph("\n"));
    }

From source file:at.htlpinkafeld.beans.UserDetailsBean.java

public void postProcessPDF(Object document) throws DocumentException {
        Document pdf = (Document) document;

        pdf.add(new Paragraph("\nStand: " + LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))));

        User curr = BenutzerverwaltungService.getUserByUsername(selectedUser);

        UserHistoryService.getUserHistoryEntriesForUserBetweenDates(curr, selectedDate, selectedDate.plusDays(1))
                .forEach((uhe) -> {/*from  ww  w  .j a v  a2 s .  c  om*/
                    monthOvertime = uhe.getOvertime();
                });

        pdf.add(new Paragraph("\nberstunden gesamt: " + monthOvertime));
        pdf.close();
    }

From source file:at.htlpinkafeld.beans.UserDetailsBean.java

public void createPDF() {
        try {/*w  w  w .ja  va2 s. c o m*/
            FacesContext context = FacesContext.getCurrentInstance();
            Document document = new Document();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);

            if (!document.isOpen()) {
                document.open();
            }

            preProcessPDF(document);

            PdfPTable pdfTable = exportPDFTable();
            document.add(pdfTable);

            postProcessPDF(document);

            //Keep modifying your pdf file (add pages and more)
            document.close();
            String fileName = "Monatszeiten";

            writePDFToResponse(context.getExternalContext(), baos, fileName);

            context.responseComplete();
        } catch (DocumentException ex) {
            Logger.getLogger(UserDetailsBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

From source file:at.tugraz.sss.serv.SSFileU.java

License:Apache License

public static void writePDFFromText(final String pdfFilePath, final String textFilePath) throws Exception {

    OutputStream out = null;// w  w  w.j  a  va 2  s.  com
    BufferedReader br = null;

    try {

        out = openOrCreateFileWithPathForWrite(pdfFilePath);

        final Document document = new Document();
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        String line;

        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        br = new BufferedReader(new FileReader(new File(textFilePath)));

        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line));
        }

        document.close();

    } catch (Exception error) {

        if (out != null) {
            out.close();
        }

        if (br != null) {
            br.close();
        }
    }
}

From source file:at.tugraz.sss.serv.SSFileU.java

License:Apache License

public static void writePDFFromDoc(final String docFilePath, final String pdfFilePath) throws Exception {

    final Document document = new Document();
    final POIFSFileSystem fs = new POIFSFileSystem(openFileForRead(docFilePath));
    final HWPFDocument word = new HWPFDocument(fs);
    final WordExtractor we = new WordExtractor(word);
    final OutputStream out = openOrCreateFileWithPathForWrite(pdfFilePath);
    final PdfWriter writer = PdfWriter.getInstance(document, out);
    final Range range = word.getRange();

    document.open();//from  w w w .  j  a va 2 s. com
    writer.setPageEmpty(true);
    document.newPage();
    writer.setPageEmpty(true);

    String[] paragraphs = we.getParagraphText();

    for (int i = 0; i < paragraphs.length; i++) {

        org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
        // CharacterRun run = pr.getCharacterRun(i);
        // run.setBold(true);
        // run.setCapitalized(true);
        // run.setItalic(true);
        paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
        System.out.println("Length:" + paragraphs[i].length());
        System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());

        // add the paragraph to the document
        document.add(new Paragraph(paragraphs[i]));
    }

    document.close();
}

From source file:at.tugraz.sss.serv.util.SSFileU.java

License:Apache License

public static void writePDFFromText(final String pdfFilePath, final String textFilePath) throws SSErr {

    OutputStream out = null;/*from w ww  . ja va  2s .c o m*/
    BufferedReader br = null;

    try {

        out = openOrCreateFileWithPathForWrite(pdfFilePath);

        final Document document = new Document();
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        String line;

        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        br = new BufferedReader(new FileReader(new File(textFilePath)));

        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line));
        }

        document.close();

    } catch (Exception error) {
        SSServErrReg.regErrThrow(error);
    } finally {

        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }

        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }
    }
}

From source file:at.tugraz.sss.serv.util.SSFileU.java

License:Apache License

public static void writePDFFromDoc(final String docFilePath, final String pdfFilePath) throws SSErr {

    try {//from   w w  w  .j  a v a 2s  .c  om
        final Document document = new Document();
        final POIFSFileSystem fs = new POIFSFileSystem(openFileForRead(docFilePath));
        final HWPFDocument word = new HWPFDocument(fs);
        final WordExtractor we = new WordExtractor(word);
        final OutputStream out = openOrCreateFileWithPathForWrite(pdfFilePath);
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        final Range range = word.getRange();

        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        String[] paragraphs = we.getParagraphText();

        for (int i = 0; i < paragraphs.length; i++) {

            org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
            // CharacterRun run = pr.getCharacterRun(i);
            // run.setBold(true);
            // run.setCapitalized(true);
            // run.setItalic(true);
            paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
            System.out.println("Length:" + paragraphs[i].length());
            System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());

            // add the paragraph to the document
            document.add(new Paragraph(paragraphs[i]));
        }

        document.close();
    } catch (Exception error) {
        SSServErrReg.regErrThrow(error);
    }
}