Example usage for com.itextpdf.text Document add

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

Introduction

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

Prototype


public boolean add(Element element) throws DocumentException 

Source Link

Document

Adds an Element to the Document.

Usage

From source file:com.tommontom.pdfsplitter.PdfMerge.java

public static void doMerge(java.util.List<InputStream> list, String[] imageList, String[] listWordExcels,
        OutputStream outputStream) throws DocumentException, IOException {
    Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    writer.setFullCompression();/*  w ww .j  a  va2 s .co  m*/
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    Image img;
    for (InputStream in : list) {
        PdfReader reader = new PdfReader(in);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {

            document.newPage();
            //import the page from source pdf
            PdfImportedPage page = writer.getImportedPage(reader, i);
            //add the page to the destination pdf
            cb.addTemplate(page, 0, 0);
        }

    }
    for (int i = 0; i < imageList.length; i++) {
        document.newPage();
        if (imageList[i] != null) {
            img = Image.getInstance(String.format("%s", imageList[i]));
            Rectangle one = new Rectangle(img.getPlainWidth(), img.getPlainHeight());
            document.setPageSize(one);
            if (img.getScaledWidth() > img.getScaledHeight()) {
                img.rotate();
            }
            if (img.getScaledWidth() > 792 || img.getScaledHeight() > 792) {
                img.scaleToFit(792, 792);

            }
            img.setDpi(150, 150);
            document.add(img);
        }
    }
    for (int i = 0; i < listWordExcels.length; i++) {
        if (imageList[i] != null) {
            File input = new File(listWordExcels[i]);
            File output = new File(listWordExcels[i] + ".pdf");
            String outputS = listWordExcels[i] + ".pdf";
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
            connection.connect();
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(input, output);
            PdfReader readerWord = new PdfReader(outputS);
            PdfImportedPage page = writer.getImportedPage(readerWord, readerWord.getNumberOfPages());
            cb.addTemplate(page, 0, 0);
        }
    }

    outputStream.flush();
    document.close();
    outputStream.close();
}

From source file:com.unicauca.coordinacionpis.managedbean.RegistroFormatoAController.java

public void agregarMetadatos() {
    // create document and writer
    Document document = new Document(PageSize.A4);
    PdfWriter writer;/*  w  w w  .  j  av  a  2 s . com*/
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\aguaabril2016.pdf"));
        // add meta-data to pdf
        document.addAuthor("Memorynotfound");
        document.addCreationDate();
        document.addCreator("Memorynotfound.com");
        document.addTitle("Add meta data to PDF");
        document.addSubject("how to add meta data to pdf using itext");
        document.addKeywords(metadatosAnteproyectos.getTitulo() + "," + metadatosAnteproyectos.getProfesor());
        document.addLanguage(Locale.ENGLISH.getLanguage());
        document.addHeader("type", "tutorial, example");

        // add xmp meta data
        writer.createXmpMetadata();

        document.open();
        document.add(new Paragraph("Add meta-data to PDF using iText"));
        document.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(RegistroOfertaAcademicaController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(RegistroOfertaAcademicaController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.util.Imprimir.java

public static void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);//from  w ww .  j  a v a  2 s  .  co m
    // Lets write a big header
    preface.add(new Paragraph("Titulo do documento", catFont));

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
    preface.add(new Paragraph("Relatrio criado por: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ", smallBold));

    addEmptyLine(preface, 8);

    preface.add(new Paragraph(
            "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
            redFont));

    document.add(preface);
    // Start a new page
    document.newPage();
}

From source file:com.util.Imprimir.java

public static void addContent(Document document) throws DocumentException {
    Anchor anchor = new Anchor("Capitulo Primeiro", catFont);
    anchor.setName("Capitulo Primeiro");

    // Second parameter is the number of the chapter
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategoria 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Hello"));

    subPara = new Paragraph("Subcategoria 2", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Paragrafo 1"));
    subCatPart.add(new Paragraph("Paragrafo 2"));
    subCatPart.add(new Paragraph("Paragrafo 3"));

    // add a list
    createList(subCatPart);/*from   ww  w.  ja v  a2s.  co  m*/
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    // add a table
    createTable(subCatPart);

    // now add all this to the document
    document.add(catPart);

    // Next section
    anchor = new Anchor("Capitulo Segundo", catFont);
    anchor.setName("Capitulo Segundo");

    // Second parameter is the number of the chapter
    catPart = new Chapter(new Paragraph(anchor), 1);

    subPara = new Paragraph("Subcategoria", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("This is a very important message"));

    // now add all this to the document
    document.add(catPart);

}

From source file:com.VanLesh.macsv10.macs.Models.Pdf.java

License:GNU General Public License

private static void addContent(Document doc, Calculation calc) {
    try {/*from w w w. j  ava  2  s.c om*/
        //special font so we can render greek characters
        BaseFont bfComic = BaseFont.createFont("/system/fonts/DroidSansFallback.ttf", BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED);
        Font font1 = new Font(bfComic, 12);
        Font tgfont, tipfont;

        if (calc.getRollover() > calc.getDrag()) {
            tipfont = redFont;
            tgfont = smallBold;
        } else {
            tipfont = smallBold;
            tgfont = redFont;
        }
        Anchor anchor = new Anchor("Site", subFont);
        anchor.setName("Site Parameters");
        Paragraph identifier = new Paragraph("Calculation ID:    " + calc.getTitle(), font1);
        Paragraph engineer = new Paragraph("Engineer:    " + calc.getEngineerName(), font1);
        Paragraph jobsite = new Paragraph("Jobsite:     " + calc.getJobSite(), font1);
        Paragraph latitude = new Paragraph("Latitude:     " + calc.getLatitude(), font1);
        Paragraph longitude = new Paragraph("Longitude:     " + calc.getLongitude(), font1);

        Anchor vehicleAnchor = new Anchor("Vehicle", subFont);
        vehicleAnchor.setName("Vehicle");
        Paragraph vehicleclass = new Paragraph("Vehicle Class:     " + calc.getVehicle().getVehicleClass(),
                font1);
        Paragraph vehicletype = new Paragraph("Vehicle Type:     " + calc.getVehicle().getVehicleType(), font1);
        Paragraph vehicleweight = new Paragraph("Wv:     " + calc.getVehicle().getWv(), font1);
        Paragraph vehicletracklength = new Paragraph("Tl:     " + calc.getVehicle().getTrackL(), font1);
        Paragraph vehicletrackwidth = new Paragraph("Tw:     " + calc.getVehicle().getTrackW(), font1);
        Paragraph vehiclebladewidth = new Paragraph("Wb:     " + calc.getVehicle().getBladeW(), font1);

        Anchor soilAnchor = new Anchor("Soil", subFont);
        soilAnchor.setName("Soil");
        Paragraph soiltype = new Paragraph("Soil Type:     " + calc.getSoil().getName(), font1);
        Paragraph soilunitweight = new Paragraph("\u03B3:     " + calc.getSoil().getunitW(), font1);
        Paragraph soilfrictionangle = new Paragraph("\u03A6:     " + calc.getSoil().getfrictA(), font1);
        Paragraph soilcohesion = new Paragraph("c:     " + calc.getSoil().getC(), font1);

        Anchor measurementsAnchor = new Anchor("Measurements", subFont);
        anchor.setName("Measurements");
        Paragraph beta = new Paragraph("\u03B2:     " + calc.getBeta(), font1);
        Paragraph theta = new Paragraph("\u03B8:     " + calc.getTheta(), font1);
        Paragraph Ha = new Paragraph("Ha:     " + calc.getHa(), font1);
        Paragraph La = new Paragraph("La:     " + calc.getLa(), font1);
        Paragraph Db = new Paragraph("Db:     " + calc.getD_b(), font1);

        Anchor resultsAnchor = new Anchor("Results", subFont);
        int drag = (int) calc.getDrag();
        int roll = (int) calc.getRollover();
        Paragraph Tg = new Paragraph("Anchor cap Sliding :     " + drag, tgfont);
        Paragraph tipover = new Paragraph("Anchor cap Overturning:     " + roll, tipfont);

        doc.add(anchor);
        doc.add(identifier);
        doc.add(engineer);
        doc.add(jobsite);
        doc.add(latitude);
        doc.add(longitude);

        doc.add(vehicleAnchor);
        doc.add(vehicleclass);
        doc.add(vehicletype);
        doc.add(vehicleweight);
        doc.add(vehicletracklength);
        doc.add(vehicletrackwidth);
        doc.add(vehiclebladewidth);

        doc.add(soilAnchor);
        doc.add(soiltype);
        doc.add(soilunitweight);
        doc.add(soilfrictionangle);
        doc.add(soilcohesion);

        doc.add(measurementsAnchor);
        doc.add(beta);
        doc.add(theta);
        doc.add(Ha);
        doc.add(La);
        doc.add(Db);

        doc.add(resultsAnchor);
        doc.add(Tg);
        doc.add(tipover);
        Log.e("addingcontent", "complete");

    } catch (Exception e) {
        Font font1 = new Font(Font.FontFamily.HELVETICA, 24, Font.NORMAL, BaseColor.BLACK);

    }

}

From source file:com.vectorprint.report.itext.debug.DebugHelper.java

License:Open Source License

/**
 * This method will append to the pdf a legend explaining the visual feedback in the pdf, an overview of the styles
 * used and an overview of the properties used.
 *
 * @throws DocumentException//from   w  w  w  .j  av a 2 s . c  om
 */
public static void appendDebugInfo(PdfWriter writer, Document document, EnhancedMap settings,
        StylerFactory stylerFactory) throws DocumentException, VectorPrintException {

    PdfContentByte canvas = writer.getDirectContent();
    canvas.setFontAndSize(FontFactory.getFont(FontFactory.COURIER).getBaseFont(), 8);
    canvas.setColorFill(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));
    canvas.setColorStroke(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    Font f = FontFactory.getFont(FontFactory.COURIER, 8);

    f.setColor(itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    float top = document.getPageSize().getTop();

    document.add(new Phrase(new Chunk("table: ", f).setLocalDestination(DEBUGPAGE)));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("cell: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("image: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("text: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("background color is shown in a small rectangle", f));
    document.add(Chunk.NEWLINE);
    canvas.setLineWidth(2);
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    float left = document.leftMargin();
    canvas.rectangle(left + 80, top - 25, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineWidth(0.3f);
    canvas.setLineDash(new float[] { 2, 2 }, 0);
    canvas.rectangle(left + 80, top - 37, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 1, 0 }, 0);
    canvas.rectangle(left + 80, top - 50, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    canvas.rectangle(left + 80, top - 63, left + 80, 8);
    canvas.closePathStroke();
    document.add(Chunk.NEWLINE);

    document.add(new Phrase("fonts available: " + FontFactory.getRegisteredFonts(), f));

    document.add(Chunk.NEWLINE);

    if (settings.getBooleanProperty(false, DEBUG)) {
        document.add(new Phrase("OVERVIEW OF STYLES FOR THIS REPORT", f));
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    Font b = new Font(f);
    b.setStyle("bold");
    Set<Map.Entry<String, String>> entrySet = stylerFactory.getStylerSetup().entrySet();
    for (Map.Entry<String, String> styleInfo : entrySet) {
        String key = styleInfo.getKey();
        document.add(new Chunk(key, b).setLocalDestination(key));
        document.add(new Chunk(": " + styleInfo.getValue(), f));
        document.add(Chunk.NEWLINE);
        document.add(new Phrase("   styling configured by " + key + ": ", f));
        for (BaseStyler st : (Collection<BaseStyler>) stylerFactory.getBaseStylersFromCache((key))) {
            document.add(Chunk.NEWLINE);
            document.add(new Chunk("      ", f));
            document.add(new Chunk(st.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                    .setLocalGoto(st.getClass().getSimpleName()));
            document.add(new Chunk(":", f));
            document.add(Chunk.NEWLINE);
            document.add(new Phrase("         non default parameters for " + st.getClass().getSimpleName()
                    + ": " + getParamInfo(st), f));
            document.add(Chunk.NEWLINE);
            if (!st.getConditions().isEmpty()) {
                document.add(new Phrase("      conditions for this styler: ", f));
            }
            for (StylingCondition sc : (Collection<StylingCondition>) st.getConditions()) {
                document.add(Chunk.NEWLINE);
                document.add(new Chunk("         ", f));
                document.add(
                        new Chunk(sc.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                                .setLocalGoto(sc.getClass().getSimpleName()));
                document.add(Chunk.NEWLINE);
                document.add(new Phrase("            non default parameters for "
                        + sc.getClass().getSimpleName() + ": " + getParamInfo(sc), f));
            }
        }
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    document.newPage();

    document.add(new Phrase("Properties used for the report", f));
    document.add(Chunk.NEWLINE);

    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bo);

    settings.listProperties(ps);
    ps.close();
    document.add(new Paragraph(bo.toString(), f));

    document.newPage();

    try {
        bo = new ByteArrayOutputStream();
        ps = new PrintStream(bo);
        Help.printHelpHeader(ps);
        ps.close();
        document.add(new Paragraph(bo.toString(), f));

        Help.printStylerHelp(document, f);

        Help.printConditionrHelp(document, f);

    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException
            | NoSuchMethodException | InvocationTargetException ex) {
        log.log(Level.SEVERE, null, ex);
    }
}

From source file:com.vectorprint.report.itext.Help.java

License:Open Source License

public static void printStylerHelp(Document document, com.itextpdf.text.Font f)
        throws IOException, FileNotFoundException, ClassNotFoundException, InstantiationException,
        IllegalAccessException, DocumentException, NoSuchMethodException, InvocationTargetException {
    for (BaseStyler s : getParameterizables(AbstractStyler.class.getPackage(), BaseStyler.class)) {
        document.add(Chunk.NEWLINE);
        document.add(//from  ww w.j ava2s  . c o  m
                new Chunk(s.getClass().getSimpleName(), f).setLocalDestination(s.getClass().getSimpleName()));
        document.add(new Phrase(":  " + s.getHelp() + "\n  parameters available:", f));

        ByteArrayOutputStream o = new ByteArrayOutputStream(400);
        PrintStream out = new PrintStream(o);
        printParamInfo(s, out);
        out.println("  able to style: " + s.getSupportedClasses());
        addStylerForClass(s);
        if (s.creates()) {
            out.println("  creates supported Class");
        }
        out.flush();
        document.add(new Paragraph(o.toString(), f));
    }
    printStylersPerClass(document, f);
}

From source file:com.vectorprint.report.itext.Help.java

License:Open Source License

public static void printConditionrHelp(Document document, com.itextpdf.text.Font f)
        throws IOException, FileNotFoundException, ClassNotFoundException, InstantiationException,
        IllegalAccessException, DocumentException, NoSuchMethodException, InvocationTargetException {
    document.add(new Paragraph(
            "\nAvailable conditions for stylers that can be configured in a properties file, together with parameters that may be used."
                    + "---------------------------------------------------------------------------------------------------------\n"
                    + "  All parameters mentioned below can take defaults through properties in the form <(Parent)Class.getSimpleName().parameterName>=<value>\n",
            f));//  w w w  .  ja  va  2s .c om
    for (StylingCondition s : getParameterizables(AbstractCondition.class.getPackage(),
            StylingCondition.class)) {
        document.add(Chunk.NEWLINE);
        document.add(
                new Chunk(s.getClass().getSimpleName(), f).setLocalDestination(s.getClass().getSimpleName()));
        document.add(new Phrase(":  " + s.getHelp() + "\n  parameters available:", f));

        ByteArrayOutputStream o = new ByteArrayOutputStream(400);
        PrintStream out = new PrintStream(o);
        printParamInfo(s, out);
        out.flush();
        document.add(new Paragraph(o.toString(), f));
    }
}

From source file:com.vectorprint.report.itext.Help.java

License:Open Source License

public static void printStylersPerClass(Document document, com.itextpdf.text.Font f) throws DocumentException {
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("Overview of stylers available per iText Class (and its subclasses)", f));
    document.add(Chunk.NEWLINE);/* w  w w .  ja v  a 2s. c o m*/
    for (Map.Entry<Class, Set<Class<? extends BaseStyler>>> e : stylersForClass.entrySet()) {
        document.add(Chunk.NEWLINE);
        document.add(new Chunk("stylers for " + e.getKey().getSimpleName() + ": ", f));
        for (Class bs : e.getValue()) {
            document.add(
                    new Chunk(bs.getSimpleName(), f).setLocalGoto(bs.getSimpleName()).setUnderline(0.3f, -1f));
            document.add(new Chunk(", ", f));
        }
    }
}