Search in sources :

Example 1 with Rectangle

use of com.lowagie.text.Rectangle in project adempiere by adempiere.

the class Document method writePDF.

private static void writePDF(Pageable pageable, OutputStream output) {
    try {
        final PageFormat pf = pageable.getPageFormat(0);
        final com.lowagie.text.Document document = new com.lowagie.text.Document(new Rectangle((int) pf.getWidth(), (int) pf.getHeight()));
        final PdfWriter writer = PdfWriter.getInstance(document, output);
        writer.setPdfVersion(PdfWriter.VERSION_1_2);
        document.open();
        final DefaultFontMapper mapper = new DefaultFontMapper();
        //Elaine 2009/02/17 - load additional font from directory set in PDF_FONT_DIR of System Configurator 
        String pdfFontDir = MSysConfig.getValue(PDF_FONT_DIR, "");
        if (pdfFontDir != null && pdfFontDir.trim().length() > 0) {
            pdfFontDir = pdfFontDir.trim();
            File dir = new File(pdfFontDir);
            if (dir.exists() && dir.isDirectory())
                mapper.insertDirectory(pdfFontDir);
        }
        //
        final float w = (float) pf.getWidth();
        final float h = (float) pf.getHeight();
        final PdfContentByte cb = writer.getDirectContent();
        for (int page = 0; page < pageable.getNumberOfPages(); page++) {
            if (page != 0) {
                document.newPage();
            }
            final PdfTemplate tp = cb.createTemplate(w, h);
            final Graphics2D g2 = tp.createGraphics(w, h, mapper);
            tp.setWidth(w);
            tp.setHeight(h);
            pageable.getPrintable(page).print(g2, pf, page);
            g2.dispose();
            cb.addTemplate(tp, 0, 0);
            writer.releaseTemplate(tp);
        }
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) Rectangle(com.lowagie.text.Rectangle) DefaultFontMapper(com.lowagie.text.pdf.DefaultFontMapper) PdfTemplate(com.lowagie.text.pdf.PdfTemplate) Graphics2D(java.awt.Graphics2D) PageFormat(java.awt.print.PageFormat) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) File(java.io.File)

Example 2 with Rectangle

use of com.lowagie.text.Rectangle in project adempiere by adempiere.

the class SmjPdfReport method onOpenDocument.

//simpleLine
/**
	 * predefine los valores del pie de pagina ** predefined values ​​footer
	 * 
	 * @param writer
	 * @param document
	 */
public void onOpenDocument(PdfWriter writer, Document document) {
    total = writer.getDirectContent().createTemplate(100, 100);
    total.setBoundingBox(new Rectangle(-20, -20, 100, 100));
    try {
        helv = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(com.lowagie.text.ExceptionConverter) Rectangle(com.lowagie.text.Rectangle) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with Rectangle

use of com.lowagie.text.Rectangle in project cytoscape-impl by cytoscape.

the class PDFWriterTest method itextLibTest.

@Test
public void itextLibTest() throws Exception {
    final OutputStream os = new BufferedOutputStream(new FileOutputStream("target/itextTest.pdf"));
    final Rectangle pageSize = PageSize.LETTER;
    final Document document = new Document(pageSize);
    logger.debug("Document created: " + document);
    final PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();
    final PdfContentByte canvas = writer.getDirectContent();
    logger.debug("CB0 created: " + canvas.getClass());
    final float pageWidth = pageSize.getWidth();
    final float pageHeight = pageSize.getHeight();
    logger.debug("Page W: " + pageWidth + " Page H: " + pageHeight);
    final DefaultFontMapper fontMapper = new DefaultFontMapper();
    logger.debug("FontMapper created = " + fontMapper);
    final Graphics2D g = canvas.createGraphics(pageWidth, pageHeight);
    logger.debug("! G2D created = " + g);
    assertNotNull(g);
    g.dispose();
    document.close();
    os.close();
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) Rectangle(com.lowagie.text.Rectangle) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) DefaultFontMapper(com.lowagie.text.pdf.DefaultFontMapper) Document(com.lowagie.text.Document) BufferedOutputStream(java.io.BufferedOutputStream) Graphics2D(java.awt.Graphics2D) Test(org.junit.Test)

Example 4 with Rectangle

use of com.lowagie.text.Rectangle in project dhis2-core by dhis2.

the class DefaultPdfDataEntryFormService method insertTable_OrgAndPeriod.

private void insertTable_OrgAndPeriod(PdfPTable mainTable, PdfWriter writer, List<Period> periods) throws IOException, DocumentException {
    boolean hasBorder = false;
    float width = 220.0f;
    // Input TextBox size
    Rectangle rectangle = new Rectangle(width, PdfDataEntryFormUtil.CONTENT_HEIGHT_DEFAULT);
    // Add Organization ID/Period textfield
    // Create A table to add for each group AT HERE
    // Code 1
    PdfPTable table = new PdfPTable(2);
    table.setWidths(new int[] { 1, 3 });
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    addCell_Text(table, PdfDataEntryFormUtil.getPdfPCell(hasBorder), "Organization unit identifier", Element.ALIGN_RIGHT);
    addCell_WithTextField(table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), PdfDataEntryFormUtil.LABELCODE_ORGID, PdfFieldCell.TYPE_TEXT_ORGUNIT);
    String[] periodsTitle = getPeriodTitles(periods, format);
    String[] periodsValue = getPeriodValues(periods);
    addCell_Text(table, PdfDataEntryFormUtil.getPdfPCell(hasBorder), "Period", Element.ALIGN_RIGHT);
    addCell_WithDropDownListField(table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), PdfDataEntryFormUtil.LABELCODE_PERIODID, periodsTitle, periodsValue);
    // Add to the main table
    PdfPCell cell_withInnerTable = new PdfPCell(table);
    // cell_withInnerTable.setPadding(0);
    cell_withInnerTable.setBorder(Rectangle.NO_BORDER);
    cell_withInnerTable.setHorizontalAlignment(Element.ALIGN_LEFT);
    mainTable.addCell(cell_withInnerTable);
}
Also used : PdfPCell(com.lowagie.text.pdf.PdfPCell) PdfPTable(com.lowagie.text.pdf.PdfPTable) Rectangle(com.lowagie.text.Rectangle)

Example 5 with Rectangle

use of com.lowagie.text.Rectangle in project dhis2-core by dhis2.

the class DefaultPdfDataEntryFormService method setProgramStage_DocumentContent.

private void setProgramStage_DocumentContent(Document document, PdfWriter writer, String programStageUid) throws Exception {
    ProgramStage programStage = programStageService.getProgramStage(programStageUid);
    if (programStage == null) {
        throw new RuntimeException("Error - ProgramStage not found for UID " + programStageUid);
    } else {
        // Get Rectangle with TextBox Width to be used
        Rectangle rectangle = new Rectangle(0, 0, TEXTBOXWIDTH, PdfDataEntryFormUtil.CONTENT_HEIGHT_DEFAULT);
        // Create Main Layout table and set the properties
        PdfPTable mainTable = getProgramStageMainTable();
        // Generate Period List for ProgramStage
        List<Period> periods = getProgramStagePeriodList();
        // Add Org Unit, Period, Hidden ProgramStageID Field
        insertTable_OrgAndPeriod(mainTable, writer, periods);
        insertTable_TextRow(writer, mainTable, TEXT_BLANK);
        // Add ProgramStage Field - programStage.getId();
        insertTable_HiddenValue(mainTable, rectangle, writer, PdfDataEntryFormUtil.LABELCODE_PROGRAMSTAGEIDTEXTBOX, String.valueOf(programStage.getId()));
        // Add ProgramStage Content to PDF - [The Main Section]
        insertTable_ProgramStage(mainTable, writer, programStage);
        // Add the mainTable to document
        document.add(mainTable);
    }
}
Also used : PdfPTable(com.lowagie.text.pdf.PdfPTable) Rectangle(com.lowagie.text.Rectangle) Period(org.hisp.dhis.period.Period) ProgramStage(org.hisp.dhis.program.ProgramStage)

Aggregations

Rectangle (com.lowagie.text.Rectangle)20 Document (com.lowagie.text.Document)10 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)8 PdfWriter (com.lowagie.text.pdf.PdfWriter)6 IOException (java.io.IOException)6 DocumentException (com.lowagie.text.DocumentException)5 PdfPTable (com.lowagie.text.pdf.PdfPTable)5 FileOutputStream (java.io.FileOutputStream)5 DefaultFontMapper (com.lowagie.text.pdf.DefaultFontMapper)4 PdfPCell (com.lowagie.text.pdf.PdfPCell)4 Graphics2D (java.awt.Graphics2D)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4 BadElementException (com.lowagie.text.BadElementException)3 DocWriter (com.lowagie.text.DocWriter)3 PdfImportedPage (com.lowagie.text.pdf.PdfImportedPage)3 HTMLDocument (javax.swing.text.html.HTMLDocument)3 Chapter (com.lowagie.text.Chapter)2 ExceptionConverter (com.lowagie.text.ExceptionConverter)2 Image (com.lowagie.text.Image)2