Example usage for com.lowagie.text Document top

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

Introduction

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

Prototype


public float top() 

Source Link

Document

Returns the upper right y-coordinate.

Usage

From source file:org.posterita.businesslogic.performanceanalysis.POSReportManager.java

License:Open Source License

public static String getShipmentPDFReport(Properties ctx, int minoutId, String trxName)
        throws OperationException {
    String docStatus = null;/*  w  w w .j  a  v  a2s. c om*/
    String dateOrdered = null;
    String docType = null;
    String orgName = null;
    String orgAddress = null;
    String salesRep = null;
    String phone = "      ";
    String fax = "       ";

    String customerName = null;
    String customerAddress = null;
    String documentNo = null;

    MInOut minout = MinOutManager.loadMInOut(ctx, minoutId, trxName);

    // getting orgInfo
    MOrg org = new MOrg(ctx, minout.getAD_Org_ID(), trxName);
    int location_id = org.getInfo().getC_Location_ID();
    MLocation location = new MLocation(ctx, location_id, trxName);
    MBPartner orgPartner = new MBPartner(ctx, org.getLinkedC_BPartner_ID(trxName), trxName);
    MBPartnerLocation meLocation[] = MBPartnerLocation.getForBPartner(ctx, orgPartner.get_ID());

    if (meLocation.length != 1)
        throw new OperationException("Should have only 1 location for organisation business partner!!");

    MBPartnerLocation orgLocation = meLocation[0];

    if (orgLocation.getPhone() != null)
        phone = orgLocation.getPhone();

    if (orgLocation.getFax() != null)
        fax = orgLocation.getFax();
    ;

    orgName = org.getName();

    String address1 = (location.getAddress1() == null) ? " " : location.getAddress1();
    String address2 = (location.getAddress2() == null) ? " " : location.getAddress2();
    orgAddress = (address1 + " " + address2).trim();

    // getting order type
    MDocType doctype = MDocType.get(ctx, minout.getC_DocType_ID());
    docType = doctype.getName();

    // getting orderInfo
    docStatus = minout.getDocStatusName();
    documentNo = minout.getDocumentNo();

    Date d = new Date(minout.getCreated().getTime());
    SimpleDateFormat s = new SimpleDateFormat(TimestampConvertor.DEFAULT_DATE_PATTERN1);
    dateOrdered = s.format(d);

    // getting salesrep
    int saleRep_id = minout.getSalesRep_ID();
    MUser user = new MUser(ctx, saleRep_id, trxName);
    salesRep = user.getName();

    // getting customer info
    int bpartner_id = minout.getC_BPartner_ID();
    BPartnerBean bean = BPartnerManager.getBpartner(ctx, bpartner_id, trxName);

    String name1 = (bean.getPartnerName() == null) ? " " : bean.getPartnerName();
    String name2 = (bean.getName2() == null) ? " " : bean.getName2();
    customerName = (name1 + " " + name2).trim();

    address1 = (bean.getAddress1() == null) ? " " : bean.getAddress1();
    address2 = (bean.getAddress2() == null) ? " " : bean.getAddress2();
    customerAddress = (address1 + " " + address2).trim();

    ArrayList<WebMinOutLineBean> orderLineList = MinOutManager.getWebMinOutLines(ctx, minout);

    // ----------------------------------- generating pdf
    // --------------------------------------
    String reportName = RandomStringGenerator.randomstring() + ".pdf";
    String reportPath = ReportManager.getReportPath(reportName);

    Font titleFont = new Font(Font.TIMES_ROMAN, 18, Font.BOLD);
    Font subtitleFont = new Font(Font.TIMES_ROMAN, 14, Font.BOLD);

    Font headerFont = new Font(Font.TIMES_ROMAN, 11, Font.BOLD);
    Font simpleFont = new Font(Font.TIMES_ROMAN, 10);

    float cellBorderWidth = 0.0f;

    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 30, 30, 20, 40);// l,r,t,b
    // document.getPageSize().set;

    System.out.println(document.leftMargin());

    try {
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
        PdfWriter.getInstance(document, new FileOutputStream(reportPath));

        // step 3: we open the document
        document.open();
        // step 4: we add a paragraph to the document

        Image logo = null;

        // TODO: make this part dynamic <------------------------------
        // IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!
        String imageURI = PathInfo.PROJECT_HOME + "images/pos/openBLUE_POS_Logo.gif";
        logo = Image.getInstance(imageURI);

        // MAttachment attachment = new
        // MAttachment(ctx,MOrg.Table_ID,org.getID(),null);
        // logo = Image.getInstance(attachment.getEntries()[0].getData());

        try {
            byte logoData[] = OrganisationManager.getLogo(ctx, null);
            logo = Image.getInstance(logoData);
        } catch (LogoException ex) {
            logo = Image.getInstance(imageURI);
        }

        logo.setAbsolutePosition(document.left(), document.top() - logo.getHeight());
        document.add(logo);

        PdfPTable table = new PdfPTable(2);
        PdfPCell cell = null;

        //
        table.getDefaultCell().setPadding(5.0f);
        table.setWidthPercentage(100.0f);

        // header cell
        Paragraph title = new Paragraph();
        title.add(new Chunk(orgName, subtitleFont));
        title.add(new Chunk("\n"));
        title.add(new Chunk(orgAddress, subtitleFont));
        title.add(new Chunk("\n"));
        title.add(new Chunk("Phone: " + phone, subtitleFont));
        title.add(new Chunk("\n"));
        title.add(new Chunk("Fax: " + fax, subtitleFont));

        // cell = new PdfPCell(new Paragraph(new
        // Chunk("Title1",titleFont)));
        cell = new PdfPCell(title);

        cell.setColspan(2);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        cell.setFixedHeight(logo.getHeight());
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph(""));
        cell.setBorderWidth(cellBorderWidth);
        cell.setFixedHeight(10);
        cell.setColspan(2);
        table.addCell(cell);

        // doc type
        cell = new PdfPCell(new Paragraph(new Chunk(docType, titleFont)));

        cell.setColspan(2);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        // spacing
        cell = new PdfPCell(new Paragraph(""));
        cell.setBorderWidth(cellBorderWidth);
        cell.setFixedHeight(10);
        cell.setColspan(2);
        table.addCell(cell);

        // row 1
        cell = new PdfPCell(new Paragraph(new Chunk(customerName, headerFont)));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph(new Chunk("Sales Rep: " + salesRep, headerFont)));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        // row 2
        cell = new PdfPCell(new Paragraph(new Chunk(customerAddress, headerFont)));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        // spacing
        cell = new PdfPCell(new Paragraph(""));
        cell.setBorderWidth(cellBorderWidth);
        cell.setFixedHeight(10);
        cell.setColspan(2);
        table.addCell(cell);

        // row 3
        cell = new PdfPCell(new Paragraph(new Chunk("No: " + documentNo, headerFont)));
        cell.setColspan(2);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        // row 4
        cell = new PdfPCell(new Paragraph(new Chunk("Doc Status: " + docStatus, headerFont)));
        cell.setColspan(2);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        /*
         * //row 5 cell = new PdfPCell(new Paragraph(new Chunk("Payment By:
         * "+paymentBy,headerFont))); cell.setColspan(2);
         * cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
         * cell.setBorderWidth(cellBorderWidth); table.addCell(cell);
         */

        // row 6
        cell = new PdfPCell(new Paragraph(new Chunk("Date: " + dateOrdered, headerFont)));
        cell.setColspan(2);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        // spacing
        cell = new PdfPCell(new Paragraph(""));
        cell.setColspan(2);
        cell.setFixedHeight(10);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        // spacing
        cell = new PdfPCell(new Paragraph(""));
        cell.setColspan(2);
        cell.setFixedHeight(10);
        cell.setBorderWidth(cellBorderWidth);
        table.addCell(cell);

        // ------------------------------------------------------
        cell = new PdfPCell();
        cell.setColspan(2);
        cell.setBorderWidth(cellBorderWidth);

        PdfPTable t = new PdfPTable(3);
        t.getDefaultCell().setPadding(3.0f);
        t.setWidthPercentage(100.0f);

        int[] widths = { 1, 10, 1 };
        t.setWidths(widths);

        // setting headers
        t.addCell(new Paragraph(new Chunk("SerNo", headerFont)));
        t.addCell(new Paragraph(new Chunk("Name", headerFont)));
        t.addCell(new Paragraph(new Chunk("Qty", headerFont)));

        // setting table data
        // --------------------------------writing table
        // data------------------------------
        int serNo = 0;

        BigDecimal qty = null;

        for (WebMinOutLineBean orderlineBean : orderLineList) {
            serNo++;
            qty = orderlineBean.getQtyOrdered();

            t.addCell(new Paragraph(new Chunk(serNo + "", simpleFont)));
            t.addCell(new Paragraph(new Chunk(orderlineBean.getProductName(), simpleFont)));
            t.addCell(new Paragraph(new Chunk(qty.intValue() + "", simpleFont)));
        }
        // -----------------------------------------------------------------------------------

        // table.addCell(cell);
        table.setSplitRows(true);

        document.add(table);
        document.add(t);

    } catch (Exception e) {
        throw new OperationException(e);
    }

    // step 5: we close the document
    document.close();

    return reportName;
}

From source file:questions.directcontent.InterpretOCR.java

public static void main(String[] args) throws IOException, DocumentException {
    Document document = new Document(PageSize.LETTER);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    document.open();//from   w w w .j av a  2 s  .  co  m
    PdfContentByte cb = writer.getDirectContent();
    BaseFont font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    cb.beginText();
    cb.setFontAndSize(font, 12);
    String line;
    String word;
    @SuppressWarnings("unused")
    float llx, lly, urx, ury;
    StringTokenizer tokenizer;
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RESOURCE)));
    while ((line = reader.readLine()) != null) {
        tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
            tokenizer.nextToken();
            word = tokenizer.nextToken();
            llx = Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            lly = document.top() - Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            urx = Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            ury = document.top() - Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            cb.showTextAligned(Element.ALIGN_LEFT, word, (llx + urx) / 2, lly, 0);
        }
    }
    cb.endText();
    document.close();
}

From source file:questions.objects.DifferentLeadings.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A7);
    try {//from w w  w .  j  ava2s . co m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        Chunk space = new Chunk(' ');
        String text = "Quick brown fox jumps over the lazy dog.";
        Phrase phrase1 = new Phrase(text, new Font(Font.HELVETICA, 12));
        Phrase phrase2 = new Phrase(new Chunk(text, new Font(Font.TIMES_ROMAN, 24)));
        Phrase phrase3 = new Phrase(text, new Font(Font.COURIER, 8));
        Phrase phrase4 = new Phrase(text, new Font(Font.HELVETICA, 4));
        Paragraph paragraph = new Paragraph();
        paragraph.add(phrase1);
        paragraph.add(space);
        paragraph.add(phrase2);
        paragraph.add(space);
        paragraph.add(phrase3);
        paragraph.add(space);
        paragraph.add(phrase4);
        paragraph.setMultipliedLeading(1.5f);
        paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(document.left(), document.bottom(), document.right(), document.top());
        column.addElement(paragraph);
        column.go();
        document.newPage();
        document.add(paragraph);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:questions.separators.DottedGlue.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from ww w.j a v a2s .  c  om
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        PdfContentByte canvas = writer.getDirectContent();
        canvas.moveTo(document.left(), document.top());
        canvas.lineTo(document.left(), 550);
        canvas.moveTo(document.right(), document.top());
        canvas.lineTo(document.right(), 550);
        canvas.stroke();

        Chunk separator = new Chunk(new DottedLineSeparator());

        // two parts
        Paragraph p1 = new Paragraph();
        p1.add(new Chunk("Chapter 1"));
        p1.add(separator);
        p1.add(new Chunk("p 10"));

        // 3 parts
        Paragraph p2 = new Paragraph();
        p2.add(new Chunk("Chapter 2"));
        p2.add(separator);
        p2.add(new Chunk("x"));
        p2.add(separator);
        p2.add(new Chunk("y"));

        // line ends with separator
        Paragraph p3 = new Paragraph();
        p3.add(new Chunk("Chapter 3"));
        p3.add(separator);

        // line starts with a separator
        Paragraph p4 = new Paragraph();
        p4.add(separator);
        p4.add(new Chunk("chapter 4"));

        // line IS a separator
        Paragraph p5 = new Paragraph();
        p5.add(separator);

        // line IS a separator + leading
        Paragraph p6 = new Paragraph(40);
        p6.add(separator);

        // separator galore
        Paragraph p7 = new Paragraph();
        p7.setAlignment(Element.ALIGN_JUSTIFIED);
        for (int i = 0; i < 10; i++) {
            p7.add(new Chunk("This is a test hippopotamus hippopotamus"));
            if (i % 4 == 0)
                p7.add(" hippopotamus hippopotamus hippopotamus hippopotamus");
            p7.add(separator);
        }

        document.add(p1);
        document.add(p1);
        document.add(p2);
        document.add(p3);
        document.add(p4);
        document.add(p5);
        document.add(p6);
        document.add(p7);

        ColumnText ct = new ColumnText(writer.getDirectContent());
        ct.addElement(p1);
        ct.addElement(p1);
        ct.addElement(p2);
        ct.addElement(p3);
        ct.addElement(p4);
        ct.addElement(p5);
        ct.addElement(p6);
        ct.addElement(p7);
        ct.setSimpleColumn(36, 36, 436, 536);
        ct.go();
        canvas.rectangle(36, 36, 400, 500);
        canvas.stroke();
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:questions.separators.TabbedWords2.java

public static void main(String[] args) {
    Document document = new Document();
    try {//w  ww. j ava2  s.co  m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        Paragraph p;
        Chunk tab1 = new Chunk(new VerticalPositionMark(), 100, true);
        Chunk tab2 = new Chunk(new LineSeparator(), 120, true);
        Chunk tab3 = new Chunk(new DottedLineSeparator(), 200, true);
        Chunk tab4 = new Chunk(new VerticalPositionMark(), 240, true);
        Chunk tab5 = new Chunk(new VerticalPositionMark(), 300, true);

        PdfContentByte canvas = writer.getDirectContent();
        ColumnText column = new ColumnText(canvas);
        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            column.addElement(p);
        }
        column.setSimpleColumn(60, 36, 400, 806);
        canvas.moveTo(60, 36);
        canvas.lineTo(60, 806);
        canvas.moveTo(160, 36);
        canvas.lineTo(160, 806);
        canvas.moveTo(180, 36);
        canvas.lineTo(180, 806);
        canvas.moveTo(260, 36);
        canvas.lineTo(260, 806);
        canvas.moveTo(300, 36);
        canvas.lineTo(300, 806);
        canvas.moveTo(360, 36);
        canvas.lineTo(360, 806);
        canvas.moveTo(400, 36);
        canvas.lineTo(400, 806);
        canvas.stroke();
        column.go();

        document.setMargins(60, 195, 36, 36);
        document.newPage();

        canvas.moveTo(document.left(), document.bottom());
        canvas.lineTo(document.left(), document.top());
        canvas.moveTo(document.right(), document.bottom());
        canvas.lineTo(document.right(), document.top());
        canvas.stroke();
        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            document.add(p);
        }
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:questions.tables.AddTableAsHeaderFooter.java

public void onEndPage(PdfWriter writer, Document document) {
    try {/*from   w  w w  .j a  v a2s .c  om*/
        // Header
        headerTable.writeSelectedRows(0, -1, document.leftMargin(),
                document.top() + headerTable.getTotalHeight(), writer.getDirectContent());
        // Footer
        PdfPTable footerTable = new PdfPTable(2);
        PdfPCell cell1 = new PdfPCell(new Phrase("page " + writer.getPageNumber()));
        footerTable.addCell(cell1);
        PdfPCell cell2 = new PdfPCell(Image.getInstance(tpl));
        footerTable.addCell(cell2);
        footerTable.setTotalWidth(document.right() - document.left());
        footerTable.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:questions.tables.TableColumns.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate());
    try {//w  w  w  .  j ava 2s  .  c o m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));

        document.open();

        // the content of the columns
        ColumnText content = new ColumnText(writer.getDirectContent());
        PdfPTable items = new PdfPTable(2);
        items.setWidthPercentage(100);
        for (int i = 0; i < 100; ++i) {
            items.addCell("item " + i);
            items.addCell("some item");
        }
        content.addElement(items);

        // adding the stuff to the document
        int column = 0;
        float height = 0;
        float[][] x = { { document.left(), document.left() + 380 },
                { document.right() - 380, document.right() } };
        int status = ColumnText.START_COLUMN;
        while (ColumnText.hasMoreText(status)) {
            if (column == 0) {
                PdfPTable table = new PdfPTable(1);
                table.setWidthPercentage(100);
                table.addCell("EmployeeSheets");
                table.addCell("Page " + writer.getPageNumber());
                document.add(table);
                height = table.getTotalHeight();
            }
            content.setSimpleColumn(x[column][0], document.bottom(), x[column][1],
                    document.top() - height - 10);
            status = content.go();
            if (++column >= x.length) {
                column = 0;
                document.newPage();
            }
        }
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:questions.tables.TablesWriteSelected.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate());
    try {/*  w  w  w.  j  a va  2s.co m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));

        document.open();

        // the content of the columns
        PdfPTable items = new PdfPTable(2);
        items.setTotalWidth(TOTAL_WIDTH);
        for (int i = 0; i < 100; ++i) {
            items.addCell("item " + i);
            items.addCell("some item");
        }
        int rows = items.size();

        // adding the stuff to the document
        int column = 0;
        int start;
        int end = 0;
        int row = 0;
        float available = 0;
        float[][] x = { { document.left(), document.left() + TOTAL_WIDTH },
                { document.right() - TOTAL_WIDTH, document.right() } };
        while (row < rows) {
            start = row;
            if (column == 0) {
                PdfPTable table = new PdfPTable(1);
                table.setWidthPercentage(100);
                table.addCell("EmployeeSheets");
                table.addCell("Page " + writer.getPageNumber());
                document.add(table);
                available = document.top() - table.getTotalHeight() - 10 - document.bottom();
            }
            float needed = items.getRowHeight(start);
            while (needed < available && row < rows) {
                needed += items.getRowHeight(++row);
                end = row;
            }
            items.writeSelectedRows(start, end, x[column][0], document.bottom() + available,
                    writer.getDirectContent());
            if (++column >= x.length) {
                column = 0;
                document.newPage();
            }
        }
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}