Example usage for com.itextpdf.text Rectangle getRight

List of usage examples for com.itextpdf.text Rectangle getRight

Introduction

In this page you can find the example usage for com.itextpdf.text Rectangle getRight.

Prototype

public float getRight() 

Source Link

Document

Returns the upper right x-coordinate.

Usage

From source file:dbedit.actions.ExportPdfAction.java

License:Open Source License

@Override
protected void performThreaded(ActionEvent e) throws Exception {
    boolean selection = false;
    JTable table = ResultSetTable.getInstance();
    if (table.getSelectedRowCount() > 0 && table.getSelectedRowCount() != table.getRowCount()) {
        Object option = Dialog.show("PDF", "Export", Dialog.QUESTION_MESSAGE,
                new Object[] { "Everything", "Selection" }, "Everything");
        if (option == null || "-1".equals(option.toString())) {
            return;
        }//from   w  w  w.  j ava  2  s . c  om
        selection = "Selection".equals(option);
    }
    List list = ((DefaultTableModel) table.getModel()).getDataVector();
    int columnCount = table.getColumnCount();
    PdfPTable pdfPTable = new PdfPTable(columnCount);
    pdfPTable.setWidthPercentage(100);
    pdfPTable.getDefaultCell().setPaddingBottom(4);
    int[] widths = new int[columnCount];

    // Row Header
    pdfPTable.getDefaultCell().setBorderWidth(2);
    for (int i = 0; i < columnCount; i++) {
        String columnName = table.getColumnName(i);
        pdfPTable.addCell(new Phrase(columnName, ROW_HEADER_FONT));
        widths[i] = Math.min(50000, Math.max(widths[i], ROW_HEADER_BASE_FONT.getWidth(columnName + " ")));
    }
    pdfPTable.getDefaultCell().setBorderWidth(1);
    if (!list.isEmpty()) {
        pdfPTable.setHeaderRows(1);
    }

    // Body
    for (int i = 0; i < list.size(); i++) {
        if (!selection || table.isRowSelected(i)) {
            List record = (List) list.get(i);
            for (int j = 0; j < record.size(); j++) {
                Object o = record.get(j);
                if (o != null) {
                    if (ResultSetTable.isLob(j)) {
                        o = Context.getInstance().getColumnTypeNames()[j];
                    }
                } else {
                    o = "";
                }
                PdfPCell cell = new PdfPCell(new Phrase(o.toString()));
                cell.setPaddingBottom(4);
                if (o instanceof Number) {
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                }
                pdfPTable.addCell(cell);
                widths[j] = Math.min(50000, Math.max(widths[j], BASE_FONT.getWidth(o.toString())));
            }
        }
    }

    // Size
    pdfPTable.setWidths(widths);
    int totalWidth = 0;
    for (int width : widths) {
        totalWidth += width;
    }
    Rectangle pageSize = PageSize.A4.rotate();
    pageSize.setRight(pageSize.getRight() * Math.max(1f, totalWidth / 53000f));
    pageSize.setTop(pageSize.getTop() * Math.max(1f, totalWidth / 53000f));

    // Document
    Document document = new Document(pageSize);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);
    document.open();
    pdfTemplate = writer.getDirectContent().createTemplate(100, 100);
    pdfTemplate.setBoundingBox(new Rectangle(-20, -20, 100, 100));
    writer.setPageEvent(this);
    document.add(pdfPTable);
    document.close();
    FileIO.saveAndOpenFile("export.pdf", byteArrayOutputStream.toByteArray());
}

From source file:de.domjos.schooltools.core.utils.fileUtils.PDFBuilder.java

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    try {/*  ww w.  j a v  a2 s. c  o m*/
        Rectangle rect = writer.getBoxSize("art");
        Image img = Image.getInstance(Converter.convertDrawableToByteArray(this.context, R.drawable.icon));
        Phrase phrase = new Phrase();
        phrase.add(new Chunk(img, 0, 0));
        ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, phrase, rect.getLeft(),
                rect.getBottom(), 0);
        ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
                new Phrase(String
                        .valueOf(this.context.getString(R.string.api_page) + " " + document.getPageNumber())),
                rect.getRight(), rect.getBottom(), 0);
    } catch (Exception ex) {
        Helper.printException(context, ex);
    }
}

From source file:de.gbv.marginalia.Marginalia.java

License:Open Source License

/**
 * Inspect a PDF file and write the info to a writer
 * @param writer Writer to a text file//  w ww  .  ja va2 s . com
 * @param filename Path to the PDF file
 * @throws IOException
 */
public static void inspect(PrintWriter writer, String filename) throws IOException, SAXException {
    //        writer.println(filename);
    writer.flush();

    PdfReader reader = new PdfReader(filename);

    ContentHandler xmlhandler = new SimpleXMLWriter(writer);
    xmlhandler.startDocument();

    SimpleXMLCreator xml = new SimpleXMLCreator(xmlhandler, Annotation.namespaces, true);

    /*
            writer.println("Number of pages: "+reader.getNumberOfPages());
            Rectangle mediabox = reader.getPageSize(1);
            writer.print("Size of page 1: [");
            writer.print(mediabox.getLeft());
            writer.print(',');
            writer.print(mediabox.getBottom());
            writer.print(',');
            writer.print(mediabox.getRight());
            writer.print(',');
            writer.print(mediabox.getTop());
            writer.println("]");
            writer.print("Rotation of page 1: ");
            writer.println(reader.getPageRotation(1));
            writer.print("Page size with rotation of page 1: ");
            writer.println(reader.getPageSizeWithRotation(1));
            writer.println();
            writer.flush();
    */
    List<Annotation> annots = new LinkedList<Annotation>();
    xml.startElement("annots");

    // TODO: The following elements may be added:
    // - optionally write <f href="Document.pdf"/>
    // - optionally write <ids original="ID" modified="ID" />

    xml.startElement("m", "pages");
    for (int pageNum = 1; pageNum <= reader.getNumberOfPages(); pageNum++) {
        PdfDictionary pageDic = reader.getPageN(pageNum);

        Map<String, String> attr = new HashMap<String, String>();
        attr.put("number", "" + pageNum);
        attr.put("rotate", "" + reader.getPageRotation(pageNum));

        Rectangle mediabox = reader.getPageSize(pageNum);
        attr.put("left", "" + mediabox.getLeft());
        attr.put("bottom", "" + mediabox.getBottom());
        attr.put("right", "" + mediabox.getRight());
        attr.put("top", "" + mediabox.getTop());

        xml.contentElement("m", "page", "", attr);

        PdfArray rawannots = pageDic.getAsArray(PdfName.ANNOTS);
        if (rawannots == null || rawannots.isEmpty()) {
            // writer.println("page "+pageNum+" contains no annotations");
            continue;
        }

        // writer.println("page "+pageNum+" has "+rawannots.size()+" annotations");

        for (int i = 0; i < rawannots.size(); i++) {
            PdfObject obj = rawannots.getDirectObject(i);
            if (!obj.isDictionary())
                continue;
            Annotation a = new Annotation((PdfDictionary) obj, pageNum);
            annots.add(a);
        }

        /**
        // Now we have all highlight and similar annotations, we need
        // to find out what words are actually highlighted! PDF in fact
        // is a dump format to express documents.
        // For some hints see
        // http://stackoverflow.com/questions/4028240/extract-each-column-of-a-pdf-file
                
        // We could reuse code from LocationTextExtractionStrategy (TODO)
        // LocationTextExtractionStrategy extr = new LocationTextExtractionStrategy();
        String fulltext = PdfTextExtractor.getTextFromPage(reader,pageNum);//,extr
        writer.println(fulltext);
        */
    }
    xml.endElement();

    for (Annotation a : annots) {
        a.serializeXML(xmlhandler);
    }
    // TODO: add page information (page size and orientation)

    xml.endAll();
}

From source file:de.jost_net.JVerein.io.HeaderFooter.java

License:Open Source License

/**
 * Adds the header and the footer.// ww  w. j  av a  2 s.  c  o  m
 * 
 */
@Override
public void onEndPage(PdfWriter writer, Document document) {
    Rectangle rect = document.getPageSize();
    switch (writer.getPageNumber() % 2) {
    case 0:
        // ColumnText.showTextAligned(writer.getDirectContent(),
        // Element.ALIGN_RIGHT, header[0], rect.getRight(), rect.getTop(), 0);
        break;
    case 1:
        // ColumnText.showTextAligned(writer.getDirectContent(),
        // Element.ALIGN_LEFT,
        // header[1], rect.getLeft(), rect.getTop(), 0);
        break;
    }
    float left = rect.getLeft() + document.leftMargin();
    float right = rect.getRight() - document.rightMargin();
    float bottom = rect.getBottom() + document.bottomMargin();
    PdfContentByte pc = writer.getDirectContent();
    pc.setColorStroke(BaseColor.BLACK);
    pc.setLineWidth(0.5f);
    pc.moveTo(left, bottom - 5);
    pc.lineTo(right, bottom - 5);
    pc.stroke();
    pc.moveTo(left, bottom - 25);
    pc.lineTo(right, bottom - 25);
    pc.stroke();

    ColumnText.showTextAligned(pc, Element.ALIGN_CENTER,
            new Phrase(footer + " " + pagenumber, Reporter.getFreeSans(7)), (left + right) / 2, bottom - 18, 0);
}

From source file:EplanPrinter.PDFPrint.java

License:Open Source License

private MediaBox[] getDocumentMediaBox(PdfReader reader, int pages) {
    MediaBox mbox[] = new MediaBox[pages];
    Rectangle rect;

    for (int x = 0; x < totalPages; x++) {
        mbox[x] = new MediaBox();
        rect = reader.getPageSize(x + 1);
        mbox[x].top = rect.getTop();/*w w w  . j av  a 2  s .c  om*/
        mbox[x].bottom = rect.getBottom();
        mbox[x].left = rect.getLeft();
        mbox[x].right = rect.getRight();
    }
    return mbox;
}

From source file:fenix.planner.pdf.PDFGenerator.java

License:Open Source License

public void generate(OutputStream output) {
    final Rectangle pageSize = PageSize.A4;
    final ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();

    document = new Document(pageSize, LEFT_MARGIN, RIGHT_MARGIN, TOP_MARGIN, BOTTOM_MARGIN);
    try {//  w  w  w  .j  a va  2  s  .co m
        final PdfWriter writer = PdfWriter.getInstance(document, outputBuffer);
        writer.setBoxSize("art", new Rectangle(ART_BOX_LEFT_MARGIN, ART_BOX_BOTTOM_MARGIN,
                pageSize.getRight() - ART_BOX_RIGHT_MARGIN, pageSize.getTop() - ART_BOX_TOP_MARGIN));
        writer.setPageEvent(new HeaderFooter());
        document.open();
        createAndAddForeword();
        createAndAddEventTable();
        createAndAddAfterword();
        createAndAddOrganizerTable();
        document.close();
        writer.close();

        // Loop through the document again to add the missing page numbers
        document = new Document();
        final PdfCopy copy = new PdfCopy(document, output);
        document.open();
        final PdfReader reader = new PdfReader(outputBuffer.toByteArray());
        addPageNumbers(reader, copy);
        document.close();
        reader.close();
    } catch (DocumentException | IOException ex) {
        throw new PDFGenerationException("Error generating PDF", ex);
    }
}

From source file:fenix.planner.pdf.PDFGenerator.java

License:Open Source License

private void addPageNumbers(PdfReader reader, PdfCopy copy) {
    int pageCount = reader.getNumberOfPages();
    PdfImportedPage page;//from w w  w  .j a v a2s  .c om
    PdfCopy.PageStamp stamp;

    for (int i = 1; i <= pageCount; ++i) {
        Rectangle rect = reader.getBoxSize(i, "art");
        page = copy.getImportedPage(reader, i);
        stamp = copy.createPageStamp(page);
        // add page numbers
        ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_RIGHT,
                new Phrase(String.format("%d / %d", i, pageCount), footerFont), rect.getRight(),
                rect.getBottom() + 5, 0);
        try {
            stamp.alterContents();
            copy.addPage(page);
        } catch (BadPdfFormatException | IOException ex) {
            throw new PDFGenerationException("Error adding page number to page " + i, ex);
        }
    }
}

From source file:fr.ybonnel.breizhcamppdf.PdfRenderer.java

License:Apache License

public void render() throws DocumentException, IOException {

    // Footer//www  . j  av  a 2s .c o m
    pdfWriter.setPageEvent(new PdfPageEventHelper() {
        @Override
        public void onEndPage(PdfWriter writer, Document document) {

            if (writer.getPageNumber() > 1) {
                Rectangle rect = document.getPageSize();
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
                        new Phrase("BreizhCamp 2014"), (rect.getLeft() + rect.getRight()) / 2,
                        rect.getBottom() + 18, 0);
            }
        }
    });

    createFirstPage();
    List<Talk> talksToExplain = createProgrammePages();
    createTalksPages(talksToExplain);
}

From source file:helpers.PDFHeaderFooter.java

@Override
public void onStartPage(PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("art");
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(""), rect.getLeft(),
            rect.getTop(), 0);/*from ww  w. jav a2s . com*/
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
            new Phrase("Page " + document.getPageNumber(),
                    FontFactory.getFont(FontFactory.TIMES_ITALIC, 5, Font.ITALIC, BaseColor.BLACK)),
            rect.getRight(), rect.getTop(), 0);
}

From source file:helpers.PDFHeaderFooter.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("art");
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(""), rect.getLeft(),
            rect.getBottom(), 0);//from   ww  w.j a va 2  s  . c o m
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
            new Phrase("Copyright @" + new SetDateCreated().getYear() + " (Wellington Mapiku)",
                    FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.ITALIC, BaseColor.DARK_GRAY)),
            rect.getRight(), rect.getBottom(), 0);
}