Example usage for com.lowagie.text Document topMargin

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

Introduction

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

Prototype


public float topMargin() 

Source Link

Document

Returns the top margin.

Usage

From source file:biblivre3.administration.reports.BaseBiblivreReport.java

License:Open Source License

@Override
public void onEndPage(PdfWriter writer, Document document) {
    try {/*from w  w w  .j  a v  a 2  s.  c o m*/
        Rectangle page = document.getPageSize();

        PdfPTable head = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Paragraph(this.getText("REPORTS_HEADER")));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(Rectangle.BOTTOM);
        head.addCell(cell);
        head.setTotalWidth((page.width() / 2) - document.leftMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.height() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

        PdfPTable date = new PdfPTable(1);
        PdfPCell dateCell = new PdfPCell(new Paragraph(dateFormat.format(generationDate)));
        dateCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
        dateCell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
        dateCell.setBorder(Rectangle.BOTTOM);
        date.addCell(dateCell);
        date.setTotalWidth((page.width() / 2) - document.rightMargin());
        date.writeSelectedRows(0, -1, (page.width() / 2),
                page.height() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

        PdfPTable foot = new PdfPTable(1);
        Chunk pageNumber = new Chunk(String.valueOf(document.getPageNumber()));
        pageNumber.setFont(footerFont);
        cell = new PdfPCell(new Paragraph(pageNumber));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
        cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(Rectangle.TOP);
        foot.addCell(cell);
        foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
        foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:br.org.acessobrasil.silvinha.util.HeaderAndFooter.java

License:Open Source License

/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 *///www.j a v a 2s  . co  m
public void onEndPage(PdfWriter writer, Document document) {

    try {
        Rectangle page = document.getPageSize();
        PdfPTable head = new PdfPTable(1);
        PdfPCell cell = new PdfPCell(new Paragraph(GERAL.RELATORIOS_ASES));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
        cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(Rectangle.BOTTOM);
        head.addCell(cell);
        head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());
        PdfPTable foot = new PdfPTable(1);
        cell = new PdfPCell(new Paragraph(String.valueOf(document.getPageNumber())));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorder(Rectangle.TOP);
        foot.addCell(cell);
        foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:com.aryjr.nheengatu.pdf.PDFPageBreak.java

License:Open Source License

public PDFPageBreak(final PdfWriter writer, final Document document, final Tag headFirstPageTag,
        final Tag footFirstPageTag, final Tag headTag, final Tag footTag) throws DocumentException {
    this.headFirstPageTag = headFirstPageTag;
    this.footFirstPageTag = footFirstPageTag;
    this.headTag = headTag;
    this.footTag = footTag;

    final Rectangle page = document.getPageSize();
    if (headTag != null) {
        final PDFTable pdft = createTable(writer, document, headTag);
        pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        headHeight = pdft.getTotalHeight();
    }//from w w  w  . j  a  va 2 s  .  c om
    if (footTag != null) {
        final PDFTable pdft = createTable(writer, document, footTag);
        pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        footHeight = pdft.getTotalHeight();
    }
    if (headFirstPageTag != null) {
        final PDFTable pdft = createTable(writer, document, headFirstPageTag);
        pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        headFirstPageHeight = pdft.getTotalHeight();
    }
    if (footFirstPageTag != null) {
        final PDFTable pdft = createTable(writer, document, footFirstPageTag);
        pdft.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        footFirstPageHeight = pdft.getTotalHeight();
    }

    topMargin = document.topMargin();
    bottomMargin = document.bottomMargin();
    document.setMargins(document.leftMargin(), document.rightMargin(), topMargin + headFirstPageHeight,
            bottomMargin + footFirstPageHeight);

}

From source file:com.moss.pdf.template.core.Renderer.java

License:Open Source License

public void render(InputStream in, List<? extends PropertyMapping> fields, OutputStream out) throws Exception {

    PdfReader reader = new PdfReader(in);

    Document document = new Document(reader.getPageSizeWithRotation(1));

    PdfWriter writer = PdfWriter.getInstance(document, out);

    document.open();/* ww w.  ja v a 2  s .  c o  m*/

    for (int i = 1; i <= reader.getNumberOfPages(); i++) {

        PdfContentByte cb = writer.getDirectContent();

        PdfImportedPage customPage = writer.getImportedPage(reader, i);

        /*
         * add the page to our new document, turning this page to its 
         * original rotation
         */
        int pageRotation = reader.getPageRotation(i);

        if (pageRotation > 0) {

            System.out.println("page rotation found: " + pageRotation);

            double angle = -((2 * Math.PI) * pageRotation / 360);
            //         double angle = -(Math.PI / 2);

            cb.addTemplate(customPage, (float) Math.cos(angle), (float) Math.sin(angle),
                    (float) -Math.sin(angle), (float) Math.cos(angle), 0f, // x
                    document.top() + document.topMargin() // y
            );
        } else {
            cb.addTemplate(customPage, 0f, 0f);
        }

        Map<FontName, BaseFont> fonts = new HashMap<FontName, BaseFont>();

        for (PropertyMapping field : fields) {

            if (field.getPageNumber() != i) {
                continue;
            }

            /*
             * Only builtin fonts are supported at the moment
             */
            BaseFont font;
            int fontSize;
            int alignment;
            String text;
            float x, y;
            float rotation;

            {
                font = fonts.get(field.getFontName());

                if (font == null) {

                    FontName e = field.getFontName();
                    String name = null;

                    if (FontName.COURIER == e) {
                        name = BaseFont.COURIER;
                    } else if (FontName.COURIER_BOLD == e) {
                        name = BaseFont.COURIER_BOLD;
                    } else if (FontName.COURIER_BOLD_OBLIQUE == e) {
                        name = BaseFont.COURIER_BOLDOBLIQUE;
                    } else if (FontName.COURIER_OBLIQUE == e) {
                        name = BaseFont.COURIER_OBLIQUE;
                    } else if (FontName.HELVETICA == e) {
                        name = BaseFont.HELVETICA;
                    } else if (FontName.HELVETICA_BOLD == e) {
                        name = BaseFont.HELVETICA_BOLD;
                    } else if (FontName.HELVETICA_BOLD_OBLIQUE == e) {
                        name = BaseFont.HELVETICA_BOLDOBLIQUE;
                    } else if (FontName.HELVETICA_OBLIQUE == e) {
                        name = BaseFont.HELVETICA_OBLIQUE;
                    } else if (FontName.TIMES_BOLD == e) {
                        name = BaseFont.TIMES_BOLD;
                    } else if (FontName.TIMES_BOLD_ITALIC == e) {
                        name = BaseFont.TIMES_BOLDITALIC;
                    } else if (FontName.TIMES_ITALIC == e) {
                        name = BaseFont.TIMES_ITALIC;
                    } else if (FontName.TIMES_ROMAN == e) {
                        name = BaseFont.TIMES_ROMAN;
                    }

                    if (name == null) {
                        throw new RuntimeException("Unknown font type: " + e);
                    }

                    font = BaseFont.createFont(name, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                    fonts.put(field.getFontName(), font);
                }

                fontSize = field.getFontSize();

                if (TextAlignment.LEFT == field.getAlignment()) {
                    alignment = PdfContentByte.ALIGN_LEFT;
                } else if (TextAlignment.CENTER == field.getAlignment()) {
                    alignment = PdfContentByte.ALIGN_CENTER;
                } else if (TextAlignment.RIGHT == field.getAlignment()) {
                    alignment = PdfContentByte.ALIGN_RIGHT;
                } else {
                    alignment = PdfContentByte.ALIGN_LEFT;
                }

                Object value = p.eval(field.getExpr());

                if (value == null) {
                    text = "";
                } else {
                    text = value.toString();
                }

                x = field.getX() * POINTS_IN_A_CM;
                y = field.getY() * POINTS_IN_A_CM;

                rotation = 0;
            }

            cb.beginText();

            cb.setFontAndSize(font, fontSize);

            cb.showTextAligned(alignment, text, x, y, rotation);

            cb.endText();
        }

        document.newPage();
    }

    reader.close();
    document.close();
}

From source file:CPS.Core.TODOLists.PDFExporter.java

License:Open Source License

private Document prepareDocument(String filename, final String title, final String author, final String creator,
        final Rectangle pageSize) {

    System.out.println("DEBUG(PDFExporter): Creating document: " + filename);

    Document d = new Document();

    d.setPageSize(pageSize);/*from  www  .  j  a  va  2s  . c  om*/
    // TODO alter page orientation?  maybe useful for seed order worksheet

    d.addTitle(title);
    d.addAuthor(author);
    //        d.addSubject( );
    //        d.addKeywords( );
    d.addCreator(creator);

    // left, right, top, bottom - scale in points (~72 points/inch)
    d.setMargins(35, 35, 35, 44);

    try {
        PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(filename));
        // add header and footer
        writer.setPageEvent(new PdfPageEventHelper() {
            public void onEndPage(PdfWriter writer, Document document) {
                try {
                    Rectangle page = document.getPageSize();

                    PdfPTable head = new PdfPTable(3);
                    head.getDefaultCell().setBorderWidth(0);
                    head.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
                    head.addCell(new Phrase(author, fontHeadFootItal));

                    head.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                    head.addCell(new Phrase(title, fontHeadFootReg));

                    head.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
                    head.addCell("");

                    head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
                    head.writeSelectedRows(0, -1, document.leftMargin(),
                            page.getHeight() - document.topMargin() + head.getTotalHeight(),
                            writer.getDirectContent());

                    PdfPTable foot = new PdfPTable(3);

                    foot.getDefaultCell().setBorderWidth(0);
                    foot.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
                    foot.addCell(new Phrase(creator, fontHeadFootItal));

                    foot.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                    foot.addCell("");

                    foot.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
                    foot.addCell(new Phrase("Page " + document.getPageNumber(), fontHeadFootReg));

                    foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
                    foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                            writer.getDirectContent());
                } catch (Exception e) {
                    throw new ExceptionConverter(e);
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }

    return d;
}

From source file:cz.incad.kramerius.rest.api.k5.client.pdf.PDFResource.java

License:Open Source License

private static StreamingOutput streamingOutput(final File file, final String format) {
    return new StreamingOutput() {
        public void write(OutputStream output) throws IOException, WebApplicationException {
            try {
                Rectangle formatRect = formatRect(format);
                Document document = new Document(formatRect);
                PdfWriter.getInstance(document, output);
                document.open();/*from www  .j a  v a  2  s .com*/

                Image image = Image.getInstance(file.toURI().toURL());

                image.scaleToFit(
                        document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(),
                        document.getPageSize().getHeight() - document.topMargin() - document.bottomMargin());
                document.add(image);

                document.close();

            } catch (Exception e) {
                throw new WebApplicationException(e);
            } finally {
                if (file != null)
                    file.delete();
            }
        }
    };
}

From source file:jdbreport.model.io.pdf.itext2.PdfWriter.java

License:Apache License

private int saveSheet(Document document, int listCount, ReportModel model)
        throws DocumentException, IOException, SaveReportException {

    model.updatePages(0);/* w w w .  j a v  a  2 s. c  om*/

    ReportPage pageFormat = model.getReportPage();
    Paper paper = pageFormat.getPaper();
    Rectangle pageSize = new Rectangle(Math.round((float) paper.getWidth()),
            Math.round((float) paper.getHeight()));

    if (pageFormat.getOrientation() == ReportPage.LANDSCAPE) {
        pageSize = pageSize.rotate();
    }
    document.setPageSize(pageSize);
    document.setMargins(Math.round((float) pageFormat.getLeftMargin(Units.PT)),
            Math.round((float) pageFormat.getRightMargin(Units.PT)),
            Math.round((float) pageFormat.getTopMargin(Units.PT)),
            Math.round((float) pageFormat.getBottomMargin(Units.PT)));
    Rectangle viewPageSize = new Rectangle(
            document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(),
            document.getPageSize().getHeight() - document.topMargin() - document.bottomMargin());

    if (listCount > 0) {
        document.newPage();
    }
    listCount++;

    //float columnMargin = (float) Units.PT.setXPixels(model.getColumnModel().getColumnMargin());

    ReportPrintable printable = new ReportPrintable(model);
    int pageCount = printable.calcCountPage(model.getReportPage());
    Map<Integer, PageClip> clips = printable.getPageClips();

    int leftCol;
    int topRow;
    int rightCol;
    int bottomRow;

    for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
        PageClip pageClip = clips.get(pageIndex);
        if (pageClip == null)
            break;

        leftCol = pageClip.getLeftCol();
        rightCol = pageClip.getRightCol();

        topRow = pageClip.getTopRow();
        bottomRow = pageClip.getBottomRow();

        int columnCount = rightCol - leftCol;

        float[] widths = new float[columnCount + 1];
        widths[0] = 1;
        for (int c = 0; c < columnCount; c++) {
            ReportColumn column = (ReportColumn) model.getColumnModel().getColumn(leftCol + c);
            widths[c + 1] = (float) Math.round(column.getNativeWidth());
        }

        PdfPTable table = createPdfTable(columnCount + 1, widths, viewPageSize);

        fillTable(model, leftCol, topRow, rightCol, bottomRow, table);

        document.add(table);

        if (pageIndex < clips.size()) {
            document.newPage();
        }

    }
    return listCount;
}

From source file:org.activityinfo.server.report.renderer.itext.ItextChartRenderer.java

License:Open Source License

@Override
public void render(DocWriter writer, Document doc, PivotChartReportElement element) throws DocumentException {

    doc.add(ThemeHelper.elementTitle(element.getTitle()));
    ItextRendererHelper.addFilterDescription(doc, element.getContent().getFilterDescriptions());
    ItextRendererHelper.addDateFilterDescription(doc, element.getFilter().getDateRange());

    if (element.getContent().getData().isEmpty()) {
        Paragraph para = new Paragraph(I18N.CONSTANTS.noData());
        para.setFont(new Font(Font.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0)));
        doc.add(para);//  w  w  w  . j a va  2 s.  c  om

    } else {
        float width = doc.getPageSize().getWidth() - doc.rightMargin() - doc.leftMargin();
        float height = (doc.getPageSize().getHeight() - doc.topMargin() - doc.bottomMargin()) / 3f;

        renderImage(writer, doc, element, width, height);
    }
}

From source file:org.cyberoam.iview.charts.Chart.java

License:Open Source License

/**
 * This Event handler Method adds Header and Footer in PDF File
 *//*from   ww  w. j a v a 2 s  .  co m*/
public void onEndPage(PdfWriter writer, Document document) {
    try {
        if (document.getPageNumber() > 1) {
            String seperator = System.getProperty("file.separator");
            //String path=System.getProperty("catalina.home") +seperator+"webapps" +seperator+"ROOT" + seperator + "images" + seperator;
            String path = InitServlet.contextPath + seperator + "images" + seperator;
            Image imgHead = Image.getInstance(path + "iViewPDFHeader.JPG");
            Image imgFoot = Image.getInstance(path + "iViewPDFFooter.JPG");
            Rectangle page = document.getPageSize();

            PdfPTable head = new PdfPTable(1);
            head.addCell(imgHead);
            head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            head.writeSelectedRows(0, -1, document.leftMargin() - 10,
                    page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

            PdfPTable foot = new PdfPTable(1);
            foot.addCell(imgFoot);
            foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            foot.writeSelectedRows(0, -1, document.leftMargin() - 10, document.bottomMargin() + 24,
                    writer.getDirectContent());
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:org.drools.verifier.doc.DroolsDocsComponentFactory.java

License:Apache License

public void onEndPage(PdfWriter writer, Document document) {

    try {/*from  ww  w  . j ava2s  .c  o m*/
        Image image = Image.getInstance(DroolsDocsBuilder.class.getResource("guvnor-webapp.png")); // TODO this image never existed
        image.setAlignment(Image.RIGHT);
        image.scaleAbsolute(100, 30);
        Rectangle page = document.getPageSize();
        PdfPTable head = new PdfPTable(2);

        PdfPCell cell1 = new PdfPCell(image);
        cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell1.setBorder(0);

        head.addCell(cell1);

        PdfPCell cell2 = new PdfPCell(new Phrase(currentDate, DroolsDocsComponentFactory.HEADER_FOOTER_TEXT));
        cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell2.setBorder(0);

        head.addCell(cell2);

        head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

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