Example usage for com.itextpdf.text Element ALIGN_LEFT

List of usage examples for com.itextpdf.text Element ALIGN_LEFT

Introduction

In this page you can find the example usage for com.itextpdf.text Element ALIGN_LEFT.

Prototype

int ALIGN_LEFT

To view the source code for com.itextpdf.text Element ALIGN_LEFT.

Click Source Link

Document

A possible value for paragraph alignment.

Usage

From source file:org.cejug.yougi.web.report.EventAttendeeCertificate.java

License:Open Source License

public void generateCertificate(Attendee attendee) throws DocumentException {

    Font helvetica = new Font(Font.FontFamily.HELVETICA, 12);
    BaseFont baseFont = helvetica.getCalculatedBaseFont(false);
    canvas.saveState();/*from  w  w w. j  a v a2  s . co m*/
    canvas.beginText();
    canvas.setFontAndSize(baseFont, 12);
    canvas.showTextAligned(Element.ALIGN_LEFT, "Validation code: " + attendee.getCertificateCode()
            + " ( http://www.cejug.org/certificate_validation.xhtml) ", 30, 30, 0);
    canvas.endText();
    canvas.restoreState();

    String[] contentLine = new String[8];
    contentLine[0] = "Certificamos que";
    contentLine[1] = attendee.getCertificateFullname();
    contentLine[2] = "participou do evento";
    contentLine[3] = attendee.getCertificateEvent();
    contentLine[4] = "realizado na";
    contentLine[5] = attendee.getCertificateVenue();
    contentLine[6] = "no dia "
            + TextUtils.INSTANCE.getFormattedDate(attendee.getCertificateDate(), "dd.MM.yyyy");

    Font normalFont = new Font(Font.FontFamily.HELVETICA, 24);
    Font largeFont = new Font(Font.FontFamily.HELVETICA, 28, Font.FontStyle.BOLD.ordinal());
    Paragraph p;

    for (int i = 0; i < 5; i++) {
        p = new Paragraph(" ", normalFont);
        this.document.add(p);
    }

    Font currentFont = normalFont;
    for (int i = 0; i < contentLine.length; i++) {
        p = new Paragraph(contentLine[i], currentFont);
        p.setAlignment(Element.ALIGN_CENTER);
        this.document.add(p);

        currentFont = currentFont.equals(normalFont) ? largeFont : normalFont;
    }
}

From source file:org.cidte.sii.negocio.PDFWriter.java

public void writePDF(ArrayList<Writable> list, String directorio, String nombre, java.awt.Image image)
        throws DocumentException, FileNotFoundException, BadElementException, IOException {

    Document doc = new Document();
    PdfWriter docWriter;//  w  ww .j a  va 2 s . c o m

    // special font sizes
    Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
    Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12);

    // file path
    String path = directorio + nombre + ".pdf";
    docWriter = PdfWriter.getInstance(doc, new FileOutputStream(new File(path)));

    // document header attributes
    doc.addAuthor("sii");
    doc.addCreationDate();
    doc.addProducer();
    doc.addCreator("sii");
    doc.addTitle(nombre);
    doc.setPageSize(PageSize.LETTER);

    // open document
    doc.open();

    Image img = Image.getInstance(image, null);
    img.setAlignment(Element.ALIGN_LEFT);
    doc.add(img);

    // create a paragraph
    Paragraph paragraph = new Paragraph("iText  is a library that allows you to create and "
            + "manipulate PDF documents. It enables developers looking to enhance web and other "
            + "applications with dynamic PDF document generation and/or manipulation.");

    // create PDF table with the given widths
    PdfPTable table = new PdfPTable(list.get(0).getNames().length);
    // set table width a percentage of the page width
    table.setWidthPercentage(100);
    table.setSpacingBefore(10f); // Space before table
    table.setSpacingAfter(10f); // Space after table

    // insert column headings
    String[] headings = list.get(0).getNames();
    for (String heading : headings) {
        insertCell(table, heading, Element.ALIGN_CENTER, 1, bfBold12);
    }
    table.setHeaderRows(1);

    // insert the data
    for (int i = 0; i < list.size(); i++) {
        Writable w = list.get(i);
        Object[] arr = w.getAsArray();
        for (int j = 0; j < arr.length; j++) {
            // arr[j]
            insertCell(table, arr[j].toString(), Element.ALIGN_LEFT, 1, bf12);
        }
    }

    // insert an empty row
    // insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
    // add the PDF table to the paragraph
    paragraph.add(table);
    // add the paragraph to the document
    doc.add(paragraph);

    // close the document
    doc.close();

    // close the writer
    docWriter.close();

}

From source file:org.displaytag.sample.decorators.ItextTotalWrapper.java

License:Open Source License

/**
 * Obtain a cell with the given value./*from ww w . ja  v  a  2s.c o m*/
 * @param value Value to display in the cell.
 * @return A cell with the given value.
 */
private PdfPCell getCell(String value) {
    PdfPCell cell = new PdfPCell(new Phrase(new Chunk(value, this.font)));
    cell.setLeading(8, 0);
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    return cell;
}

From source file:org.fossa.rolp.util.LebPageHelper.java

License:Open Source License

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfPTable table = new PdfPTable(2);
    table.setTotalWidth(527);//from  ww w.  ja va 2  s.c  om
    table.setWidthPercentage(100);
    table.setLockedWidth(true);
    table.getDefaultCell().setFixedHeight(105f);
    table.getDefaultCell().setBorderWidth(0);
    table.addCell("");
    table.addCell(csmLogoImage);
    table.writeSelectedRows(0, -1, 100, 840, writer.getDirectContent());
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT,
            new Phrase(
                    lebData.getSchuelername() + " " + lebData.getSchuljahr() + " "
                            + lebData.getSchulhalbjahr().getId() + " Seite " + document.getPageNumber(),
                    fusszeilenFont),
            100, 75, 0);
}

From source file:org.fossa.rolp.util.PdfFormatHelper.java

License:Open Source License

private static PdfPTable prebuildHeaderTable() throws DocumentException {
    PdfPTable table = new PdfPTable(6);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.setWidthPercentage(100f);/*  w w  w.  j  a v a2 s . co m*/
    table.setWidths(new float[] { 0.12f, 0.2f, 0.15f, 0.2f, 0.2f, 0.08f });
    return table;
}

From source file:org.fossa.rolp.util.PdfFormatHelper.java

License:Open Source License

public static PdfPTable buildFooterVersetzungsvermerkLine(String versetzungsvermerk, Font footerFont)
        throws DocumentException {
    PdfPCell labelCell = new PdfPCell(new Phrase("Versetzungsvermerk", footerFont));
    labelCell.setBorder(Rectangle.BOTTOM);
    labelCell.setBorderWidth(1f);//from w  w w  .  j a  v a  2  s. c o  m

    PdfPCell nameCell = new PdfPCell(new Phrase(versetzungsvermerk, footerFont));
    nameCell.setBorder(Rectangle.BOTTOM);
    nameCell.setBorderWidth(1f);

    PdfPTable table = new PdfPTable(2);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.setWidthPercentage(100f);
    table.addCell(labelCell);
    table.addCell(nameCell);
    table.setWidths(new float[] { 0.3f, 0.7f });
    return table;
}

From source file:org.fossa.rolp.util.PdfFormatHelper.java

License:Open Source License

public static PdfPTable buildFooterDatumLine(String datumString, Font footerFont) throws DocumentException {
    PdfPCell labelCell = new PdfPCell(new Phrase("Datum", footerFont));
    labelCell.setBorder(Rectangle.BOTTOM);
    labelCell.setBorderWidth(1f);/*  w  ww  .  j  av a 2s  . c  om*/

    PdfPCell nameCell = new PdfPCell(new Phrase(datumString, footerFont));
    nameCell.setBorder(Rectangle.BOTTOM);
    nameCell.setBorderWidth(1f);

    PdfPTable table = new PdfPTable(2);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.setWidthPercentage(100f);
    table.addCell(labelCell);
    table.addCell(nameCell);
    table.setWidths(new float[] { 0.3f, 0.7f });
    return table;
}

From source file:org.ganttproject.impex.htmlpdf.itext.ThemeImpl.java

License:GNU General Public License

private PdfPTable createColontitleTable(String topLeft, String topRight, String bottomLeft,
        String bottomRight) {//from  w  w w  .  j  a v a 2s. c  o  m
    PdfPTable head = new PdfPTable(2);
    {
        PdfPCell cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        Paragraph p = new Paragraph(topLeft, getSansRegularBold(18));
        p.setAlignment(Paragraph.ALIGN_LEFT);
        // colontitle.setLeading(0);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        // cell.setPaddingLeft(2);
        cell.setPaddingBottom(6);
        cell.setPhrase(p);
        head.addCell(cell);
    }
    {
        PdfPCell cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        Paragraph p = new Paragraph(topRight, getSansRegularBold(10));
        p.setAlignment(Paragraph.ALIGN_RIGHT);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setPaddingBottom(6);
        cell.setPhrase(p);
        head.addCell(cell);
    }
    {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setPaddingLeft(3);
        cell.setPaddingTop(2);
        cell.setPaddingBottom(6);
        cell.setBorder(Rectangle.TOP);
        cell.setBorderWidthTop(2);
        cell.setBorderColor(SORTAVALA_GREEN);
        Paragraph p = new Paragraph(bottomLeft, getSansRegularBold(18));
        p.setAlignment(Paragraph.ALIGN_LEFT);
        p.setExtraParagraphSpace(0);
        p.setIndentationLeft(0);
        p.setSpacingBefore(0);
        cell.setPhrase(p);
        // cell.addElement(p);
        head.addCell(cell);
    }
    {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setPaddingTop(2);
        cell.setPaddingBottom(6);
        cell.setBorder(Rectangle.TOP);
        cell.setBorderWidthTop(2);
        cell.setBorderColor(SORTAVALA_GREEN);
        Paragraph p = new Paragraph(bottomRight, getSansRegularBold(10));
        p.setAlignment(Paragraph.ALIGN_RIGHT);
        cell.setPhrase(p);
        head.addCell(cell);
    }
    final Document document = myDoc;
    Rectangle page = document.getPageSize();
    head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
    return head;
}

From source file:org.larz.dom4.editor.ReportGenerator.java

License:Open Source License

public static void generateReport(XtextEditor sourcePage, final Shell shell) {
    final IXtextDocument myDocument = ((XtextEditor) sourcePage).getDocument();
    myDocument.modify(new IUnitOfWork.Void<XtextResource>() {
        @Override/*from   w  w w .j a  va 2  s  .  c o  m*/
        public void process(XtextResource resource) throws Exception {
            Map<String, Map<String, ModObject>> cellMap = new HashMap<String, Map<String, ModObject>>();

            Dom4Mod dom4Mod = (Dom4Mod) resource.getContents().get(0);
            EList<AbstractElement> elements = dom4Mod.getElements();
            for (AbstractElement element : elements) {
                if (element instanceof SelectArmorById || element instanceof SelectArmorByName) {
                    String name = getSelectArmorname((Armor) element);
                    if (name == null)
                        continue;
                    String id = getArmorid((Armor) element);

                    Map<String, ModObject> map = cellMap.get(ARMOR);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(ARMOR, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Armor) element, modObject.propertyMap);
                } else if (element instanceof NewArmor) {
                    String name = getArmorname((Armor) element);
                    String id = getArmorid((Armor) element);

                    Map<String, ModObject> map = cellMap.get(ARMOR);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(ARMOR, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Armor) element, modObject.propertyMap);
                } else if (element instanceof SelectWeaponById || element instanceof SelectWeaponByName) {
                    String name = getSelectWeaponname((Weapon) element);
                    String id = getWeaponid((Weapon) element);

                    Map<String, ModObject> map = cellMap.get(WEAPONS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(WEAPONS, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Weapon) element, modObject.propertyMap);
                } else if (element instanceof NewWeapon) {
                    String name = getWeaponname((Weapon) element);
                    String id = getWeaponid((Weapon) element);

                    Map<String, ModObject> map = cellMap.get(WEAPONS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(WEAPONS, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Weapon) element, modObject.propertyMap);
                } else if (element instanceof SelectMonsterById || element instanceof SelectMonsterByName) {
                    String name = getSelectMonstername((Monster) element);
                    String id = getMonsterid((Monster) element);

                    Map<String, ModObject> map = cellMap.get(MONSTERS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(MONSTERS, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Monster) element, modObject.propertyMap);
                } else if (element instanceof NewMonster) {
                    String name = getMonstername((Monster) element);
                    String id = getMonsterid((Monster) element);

                    Map<String, ModObject> map = cellMap.get(MONSTERS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(MONSTERS, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Monster) element, modObject.propertyMap);
                } else if (element instanceof SelectSpellById || element instanceof SelectSpellByName) {
                    String name = getSelectSpellname((Spell) element);
                    String id = getSpellid((Spell) element);

                    Map<String, ModObject> map = cellMap.get(SPELLS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(SPELLS, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Spell) element, modObject.propertyMap);
                } else if (element instanceof NewSpell) {
                    String name = getSpellname((Spell) element);
                    //String id = getSpellid((Spell)element);

                    Map<String, ModObject> map = cellMap.get(SPELLS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(SPELLS, map);
                    }
                    ModObject modObject = map.get(name);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = "" + name;
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(name, modObject);
                    }
                    setPropertyValues((Spell) element, modObject.propertyMap);
                } else if (element instanceof SelectItemById || element instanceof SelectItemByName) {
                    String name = getSelectItemname((Item) element);
                    String id = getItemid((Item) element);

                    Map<String, ModObject> map = cellMap.get(ITEMS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(ITEMS, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Item) element, modObject.propertyMap);
                } else if (element instanceof NewItem) {
                    String name = getItemname((Item) element);
                    //String id = getItemid((Item)element);

                    Map<String, ModObject> map = cellMap.get(ITEMS);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(ITEMS, map);
                    }
                    ModObject modObject = map.get(name);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name;
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(name, modObject);
                    }
                    setPropertyValues((Item) element, modObject.propertyMap);
                } else if (element instanceof SelectSiteById || element instanceof SelectSiteByName) {
                    String name = getSelectSitename((Site) element);
                    String id = getSiteid((Site) element);

                    Map<String, ModObject> map = cellMap.get(SITES);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(SITES, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Site) element, modObject.propertyMap);
                } else if (element instanceof NewSite) {
                    String name = getSitename((Site) element);
                    String id = getSiteid((Site) element);

                    Map<String, ModObject> map = cellMap.get(SITES);
                    if (map == null) {
                        map = new HashMap<String, ModObject>();
                        cellMap.put(SITES, map);
                    }
                    ModObject modObject = map.get(id);
                    if (modObject == null) {
                        modObject = new ModObject();
                        modObject.title = name + " (" + id + ")";
                        modObject.propertyMap = new HashMap<String, PropertyValues>();
                        map.put(id, modObject);
                    }
                    setPropertyValues((Site) element, modObject.propertyMap);
                } else if (element instanceof SelectNation) {
                    //                  String name = getSelectNationname((Nation)element);
                    //                  String id = getNationid((Nation)element);
                    //
                    //                  Map<String, ModObject> map = cellMap.get(NATIONS);
                    //                  if (map == null) {
                    //                     map = new HashMap<String, ModObject>();
                    //                     cellMap.put(NATIONS, map);
                    //                  }
                    //                  ModObject modObject = map.get(id);
                    //                  if (modObject == null) {
                    //                     modObject = new ModObject();
                    //                     modObject.title = name + " (" + id + ")";
                    //                     modObject.propertyMap = new HashMap<String, PropertyValues>();
                    //                     map.put(id, modObject);
                    //                  }
                    //                  setPropertyValues((SelectNation)element, modObject.propertyMap, resource);
                }

            }

            try {
                // step 1
                Document document = new Document(PageSize.LETTER.rotate());
                // step 2
                File tempFile = File.createTempFile("dom4editor", ".pdf");
                tempFile.deleteOnExit();
                FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);

                PdfWriter.getInstance(document, tempFileOutputStream);
                // step 3
                document.open();

                List<Map.Entry<String, Map<String, ModObject>>> cellList = new ArrayList<Map.Entry<String, Map<String, ModObject>>>();
                for (Map.Entry<String, Map<String, ModObject>> innerEntry : cellMap.entrySet()) {
                    cellList.add(innerEntry);
                }
                Collections.sort(cellList, new Comparator<Map.Entry<String, Map<String, ModObject>>>() {
                    @Override
                    public int compare(Map.Entry<String, Map<String, ModObject>> o1,
                            Map.Entry<String, Map<String, ModObject>> o2) {
                        return o1.getKey().compareTo(o2.getKey());
                    }
                });

                for (Map.Entry<String, Map<String, ModObject>> entry : cellList) {
                    PdfPTable table = new PdfPTable(1);
                    table.setWidthPercentage(100f);

                    PdfPCell cell = new PdfPCell(new Phrase(entry.getKey(), TITLE));
                    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    cell.setFixedHeight(26f);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
                    table.addCell(cell);
                    table.setHeaderRows(1);

                    List<Map.Entry<String, ModObject>> list = new ArrayList<Map.Entry<String, ModObject>>();
                    for (Map.Entry<String, ModObject> innerEntry : entry.getValue().entrySet()) {
                        list.add(innerEntry);
                    }

                    Collections.sort(list, new Comparator<Map.Entry<String, ModObject>>() {
                        @Override
                        public int compare(Map.Entry<String, ModObject> o1, Map.Entry<String, ModObject> o2) {
                            return o1.getValue().title.compareTo(o2.getValue().title);
                        }
                    });

                    PdfPTable propertyTable = null;
                    if (entry.getKey().equals(ARMOR)) {
                        propertyTable = getTable(new String[] { "name", "type", "prot", "def", "enc", "rcost" },
                                new String[] { "Name", "Type", "Prot", "Def", "Enc", "Rcost" },
                                new ValueTranslator[] { null, new ValueTranslator() {
                                    @Override
                                    public String translate(String value) {
                                        if (value == null)
                                            return null;
                                        if (value.equals("4"))
                                            return "Shield";
                                        if (value.equals("5"))
                                            return "Body Armor";
                                        if (value.equals("6"))
                                            return "Helmet";
                                        return "Unknown";
                                    }
                                }, null, null, null, null }, null, list);
                        propertyTable.setWidths(new float[] { 5, 1, 1, 1, 1, 1 });
                    }
                    if (entry.getKey().equals(WEAPONS)) {
                        propertyTable = getTable(
                                new String[] { "name", "dmg", "att", "nratt", "def", "len", "range", "ammo",
                                        "rcost" },
                                new String[] { "Name", "Dmg", "Att", "Nratt", "Def", "Len", "Range", "Ammo",
                                        "Rcost" },
                                null, null, list);
                        propertyTable.setWidths(new float[] { 5, 1, 1, 1, 1, 1, 1, 1, 1 });
                    }
                    if (entry.getKey().equals(MONSTERS)) {
                        propertyTable = getTable(
                                new String[] { "name", "hp", "prot", "MOVE", "size", "ressize", "str", "enc",
                                        "att", "def", "prec", "mr", "mor", "gcost", "rcost" },
                                new String[] { "Name", "HP", "Prot", "Move", "Size", "Rsize", "Str", "Enc",
                                        "Att", "Def", "Prec", "MR", "Mor", "Gcost", "Rcost" },
                                null, new ValueCombiner[] { null, null, null, new ValueCombiner() {
                                    @Override
                                    public String translate(String[] value) {
                                        if (value[0] == null && value[1] == null)
                                            return null;
                                        return value[0] + "/" + value[1];
                                    }

                                    @Override
                                    public String[] getNeededColumns() {
                                        return new String[] { "mapmove", "ap" };
                                    }
                                }, null, null, null, null, null, null, null, null, null, null, null }, list);
                        propertyTable.setWidths(new float[] { 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
                    }
                    if (entry.getKey().equals(ITEMS)) {
                        propertyTable = getTable(
                                new String[] { "name", "constlevel", "PATH", "type", "weapon", "armor" },
                                new String[] { "Name", "Constlevel", "Path Req", "Type", "Weapon", "Armor" },
                                new ValueTranslator[] { null, null, null, new ValueTranslator() {
                                    @Override
                                    public String translate(String value) {
                                        if (value == null)
                                            return null;
                                        if (value.equals("1"))
                                            return "1-h Weapon";
                                        if (value.equals("2"))
                                            return "2-h Weapon";
                                        if (value.equals("3"))
                                            return "Missile Weapon";
                                        if (value.equals("4"))
                                            return "Shield";
                                        if (value.equals("5"))
                                            return "Body Armor";
                                        if (value.equals("6"))
                                            return "Helmet";
                                        if (value.equals("7"))
                                            return "Boots";
                                        if (value.equals("8"))
                                            return "Misc";
                                        return "Unknown";
                                    }
                                }, null, null }, new ValueCombiner[] { null, null, new ValueCombiner() {
                                    @Override
                                    public String translate(String[] value) {
                                        if (value[0] == null && value[1] == null && value[2] == null
                                                && value[3] == null)
                                            return null;
                                        StringBuffer buf = new StringBuffer();
                                        if (value[0] != null && !value[0].equals("null")) {
                                            buf.append(getPathName(Integer.parseInt(value[0])) + value[1]);
                                        }
                                        if (value[2] != null && !value[2].equals("null")
                                                && !value[2].equals("-1")) {
                                            buf.append(getPathName(Integer.parseInt(value[2])) + value[3]);
                                        }
                                        return buf.toString();
                                    }

                                    @Override
                                    public String[] getNeededColumns() {
                                        return new String[] { "mainpath", "mainlevel", "secondarypath",
                                                "secondarylevel" };
                                    }
                                }, null, null, null }, list);
                        propertyTable.setWidths(new float[] { 2.5f, 1, 1, 1, 2.5f, 2.5f });
                    }
                    if (entry.getKey().equals(SPELLS)) {
                        propertyTable = getTable(
                                new String[] { "name", "school", "researchlevel", "aoe", "damage", "effect",
                                        "fatiguecost", "nreff", "range", "precision", "spec", "nextspell" },
                                new String[] { "Name", "School", "Research", "AOE", "Damage", "Effect",
                                        "Fatigue", "Nreff", "Range", "Precision", "Spec", "Nextspell" },
                                null, null, list);
                        propertyTable.setWidths(new float[] { 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
                    }
                    if (entry.getKey().equals(NATIONS)) {
                        propertyTable = getTable(new String[] { "name", "startsite1", "startsite2",
                                "startsite3", "startsite4", "era", "startfort" }, list);
                        propertyTable.setWidths(new float[] { 5, 1, 1, 1, 1, 1, 1 });
                    }
                    if (entry.getKey().equals(SITES)) {
                        propertyTable = getTable(
                                new String[] { "name", "path", "level", "rarity", "loc", "homemon", "homecom",
                                        "gold", "res" },
                                new String[] { "Name", "Path", "Level", "Rarity", "Loc", "Homemon", "Homecom",
                                        "Gold", "Res" },
                                new ValueTranslator[] { null, new ValueTranslator() {
                                    @Override
                                    public String translate(String value) {
                                        if (value == null)
                                            return null;
                                        if (value.equals("0"))
                                            return "Fire";
                                        if (value.equals("1"))
                                            return "Air";
                                        if (value.equals("2"))
                                            return "Water";
                                        if (value.equals("3"))
                                            return "Earth";
                                        if (value.equals("4"))
                                            return "Astral";
                                        if (value.equals("5"))
                                            return "Death";
                                        if (value.equals("6"))
                                            return "Nature";
                                        if (value.equals("7"))
                                            return "Blood";
                                        return "Unknown";
                                    }
                                }, null, null, null, null, null, null, null }, null, list);
                        propertyTable.setWidths(new float[] { 5, 1, 1, 1, 1, 1, 1, 1, 1 });
                    }
                    PdfPCell innerCell = new PdfPCell();
                    innerCell.addElement(propertyTable);
                    innerCell.setBorder(PdfPCell.NO_BORDER);
                    innerCell.setHorizontalAlignment(Element.ALIGN_LEFT);

                    table.addCell(innerCell);
                    document.add(table);
                    document.newPage();
                }
                document.close();

                tempFileOutputStream.flush();
                tempFileOutputStream.close();

                Program pdfViewer = Program.findProgram("pdf");
                if (pdfViewer != null) {
                    pdfViewer.execute(tempFile.getAbsolutePath());
                } else {
                    FileDialog dialog = new FileDialog(shell, SWT.SAVE);
                    dialog.setFilterExtensions(new String[] { "*.pdf" });
                    if (dialog.open() != null) {
                        FileInputStream from = null;
                        FileOutputStream to = null;
                        try {
                            String filterPath = dialog.getFilterPath();
                            String name = dialog.getFileName();

                            from = new FileInputStream(new File(tempFile.getAbsolutePath()));
                            to = new FileOutputStream(new File(filterPath + File.separator + name));
                            byte[] buffer = new byte[4096];
                            int bytesRead;

                            while ((bytesRead = from.read(buffer)) != -1) {
                                to.write(buffer, 0, bytesRead); // write
                            }
                        } finally {
                            if (from != null) {
                                from.close();
                            }
                            if (to != null) {
                                to.close();
                            }
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:org.larz.dom4.editor.ReportGenerator.java

License:Open Source License

private static PdfPTable getTable(String[] columns, String[] columnNames, ValueTranslator[] trans,
        ValueCombiner[] combine, List<Map.Entry<String, ModObject>> list) {
    PdfPTable table = new PdfPTable(columns.length);
    table.setWidthPercentage(100f);//  w w  w .j a  v  a2  s  .c o  m
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    for (String col : columnNames) {
        PdfPCell c = new PdfPCell(new Phrase(col, SUBTITLE));
        c.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(c);
    }
    table.setHeaderRows(1);

    for (Map.Entry<String, ModObject> innerEntry : list) {
        String name = innerEntry.getValue().title;
        Map<String, PropertyValues> map = innerEntry.getValue().propertyMap;

        List<Map.Entry<String, PropertyValues>> list2 = new ArrayList<Map.Entry<String, PropertyValues>>();
        for (Map.Entry<String, PropertyValues> innerEntry2 : map.entrySet()) {
            list2.add(innerEntry2);
        }
        Collections.sort(list2, new Comparator<Map.Entry<String, PropertyValues>>() {
            @Override
            public int compare(Map.Entry<String, PropertyValues> o1, Map.Entry<String, PropertyValues> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });

        if (list2.size() == 0)
            continue;

        PdfPCell[] cells = new PdfPCell[columns.length];
        cells[0] = new PdfPCell();
        cells[0].addElement(new Phrase(name, BOLD_TEXT));

        for (int i = 1; i < cells.length; i++) {
            cells[i] = new PdfPCell();
            if (combine != null && combine[i] != null) {
                String[] neededCols = combine[i].getNeededColumns();
                String[] oldValues = new String[neededCols.length];
                String[] newValues = new String[neededCols.length];
                for (int j = 0; j < neededCols.length; j++) {
                    for (Map.Entry<String, PropertyValues> entry : list2) {
                        if (entry.getKey().equals(neededCols[j])) {
                            oldValues[j] = entry.getValue().oldValue;
                            newValues[j] = entry.getValue().newValue;
                            break;
                        }
                    }
                }
                // Put old values into null new values
                boolean hasNew = false;
                for (int k = 0; k < newValues.length; k++) {
                    if (newValues[k] != null) {
                        hasNew = true;
                        break;
                    }
                }
                if (hasNew) {
                    for (int k = 0; k < newValues.length; k++) {
                        if (newValues[k] == null) {
                            newValues[k] = oldValues[k];
                        }
                    }
                }
                String newValue = combine[i].translate(newValues);
                String oldValue = combine[i].translate(oldValues);
                if (newValue != null) {
                    Phrase phrase = new Phrase();
                    phrase.add(new Chunk(newValue, BOLD_TEXT));
                    if (oldValue != null) {
                        phrase.add(new Chunk(" (" + oldValue + ")", TEXT));
                    }
                    cells[i].addElement(phrase);
                } else if (oldValue != null) {
                    cells[i].addElement(new Phrase(oldValue, TEXT));
                }
            } else {
                for (Map.Entry<String, PropertyValues> entry : list2) {
                    if (entry.getKey().equals(columns[i])) {
                        String oldValue = entry.getValue().oldValue;
                        String newValue = entry.getValue().newValue;
                        if (trans != null && trans.length > i && trans[i] != null) {
                            oldValue = trans[i].translate(oldValue);
                            newValue = trans[i].translate(newValue);
                        }
                        if (newValue != null) {
                            Phrase phrase = new Phrase();
                            phrase.add(new Chunk(newValue, BOLD_TEXT));
                            if (oldValue != null && !oldValue.equals("null")) {
                                phrase.add(new Chunk(" (" + oldValue + ")", TEXT));
                            }
                            cells[i].addElement(phrase);
                        } else if (oldValue != null && !oldValue.equals("null")) {
                            cells[i].addElement(new Phrase(oldValue, TEXT));
                        }
                        break;
                    }
                }
            }
        }

        for (PdfPCell cell : cells) {
            table.addCell(cell);
        }
    }
    return table;
}