Java Code Examples for com.lowagie.text.Element#ALIGN_LEFT

The following examples show how to use com.lowagie.text.Element#ALIGN_LEFT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PdfLogicalPageDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private int mapAlignment( final RenderableComplexText node ) {
  ElementAlignment alignment;
  if ( node.getNext() == null ) {
    alignment = paragraphContext.getLastLineAlignment();
  } else {
    alignment = paragraphContext.getTextAlignment();
  }
  if ( ElementAlignment.LEFT.equals( alignment ) ) {
    return Element.ALIGN_LEFT;
  } else if ( ElementAlignment.RIGHT.equals( alignment ) ) {
    return Element.ALIGN_RIGHT;
  } else if ( ElementAlignment.CENTER.equals( alignment ) ) {
    return Element.ALIGN_CENTER;
  } else {
    return Element.ALIGN_JUSTIFIED;
  }
}
 
Example 2
Source File: PdfLine.java    From MesquiteCore with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the left indentation of the line taking the alignment of the line into account.
 *
 * @return	a value
 */

float indentLeft() {
    if (isRTL) {
        switch (alignment) {
            case Element.ALIGN_LEFT:
                return left + width;
            case Element.ALIGN_CENTER:
                return left + (width / 2f);
            default:
                return left;
        }
    }
    else {
        switch (alignment) {
            case Element.ALIGN_RIGHT:
                return left + width;
            case Element.ALIGN_CENTER:
                return left + (width / 2f);
            default:
                return left;
        }
    }
}
 
Example 3
Source File: PdfLine.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the left indentation of the line taking the alignment of the line into account.
 *
 * @return a value
 */

float indentLeft() {
	if (isRTL) {
		switch (alignment) {
			case Element.ALIGN_LEFT:
				return left + width;
			case Element.ALIGN_CENTER:
				return left + width / 2f;
			default:
				return left;
		}
	} else if (getSeparatorCount() == 0) {
		switch (alignment) {
			case Element.ALIGN_RIGHT:
				return left + width;
			case Element.ALIGN_CENTER:
				return left + width / 2f;
		}
	}
	return left;
}
 
Example 4
Source File: HtmlEncoder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
* Translates the alignment value.
*
* @param   alignment   the alignment value
* @return  the translated value
*/
   
   public static String getAlignment(int alignment) {
       switch(alignment) {
           case Element.ALIGN_LEFT:
               return HtmlTags.ALIGN_LEFT;
           case Element.ALIGN_CENTER:
               return HtmlTags.ALIGN_CENTER;
           case Element.ALIGN_RIGHT:
               return HtmlTags.ALIGN_RIGHT;
           case Element.ALIGN_JUSTIFIED:
           case Element.ALIGN_JUSTIFIED_ALL:
               return HtmlTags.ALIGN_JUSTIFIED;
           case Element.ALIGN_TOP:
               return HtmlTags.ALIGN_TOP;
           case Element.ALIGN_MIDDLE:
               return HtmlTags.ALIGN_MIDDLE;
           case Element.ALIGN_BOTTOM:
               return HtmlTags.ALIGN_BOTTOM;
           case Element.ALIGN_BASELINE:
               return HtmlTags.ALIGN_BASELINE;
               default:
                   return "";
       }
   }
 
Example 5
Source File: HtmlEncoder.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
* Translates the alignment value.
*
* @param   alignment   the alignment value
* @return  the translated value
*/
   
   public static String getAlignment(int alignment) {
       switch(alignment) {
           case Element.ALIGN_LEFT:
               return HtmlTags.ALIGN_LEFT;
           case Element.ALIGN_CENTER:
               return HtmlTags.ALIGN_CENTER;
           case Element.ALIGN_RIGHT:
               return HtmlTags.ALIGN_RIGHT;
           case Element.ALIGN_JUSTIFIED:
           case Element.ALIGN_JUSTIFIED_ALL:
               return HtmlTags.ALIGN_JUSTIFIED;
           case Element.ALIGN_TOP:
               return HtmlTags.ALIGN_TOP;
           case Element.ALIGN_MIDDLE:
               return HtmlTags.ALIGN_MIDDLE;
           case Element.ALIGN_BOTTOM:
               return HtmlTags.ALIGN_BOTTOM;
           case Element.ALIGN_BASELINE:
               return HtmlTags.ALIGN_BASELINE;
               default:
                   return "";
       }
   }
 
Example 6
Source File: PdfLogicalPageDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public ColumnText reconfigure( PdfContentByte cb, Phrase p ) {
  float filledWidth = ColumnText.getWidth( p, runDirection, 0 ) + 0.5f;
  ColumnText ct = new ColumnText( cb );
  ct.setRunDirection( runDirection );
  if ( alignment == Element.ALIGN_LEFT ) {
    ct.setSimpleColumn( llx, lly, llx + filledWidth, ury, leading, alignment );
  } else if ( alignment == Element.ALIGN_RIGHT ) {
    ct.setSimpleColumn( urx - filledWidth, lly, urx, ury, leading, alignment );
  } else if ( alignment == Element.ALIGN_CENTER ) {
    float delta = ( ( urx - llx ) - filledWidth ) / 2;
    ct.setSimpleColumn( urx + delta, lly, urx - delta, ury, leading, alignment );
  } else {
    ct.setSimpleColumn( llx, lly, urx, ury, leading, alignment );
  }
  return ct;
}
 
Example 7
Source File: PdfLine.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the left indentation of the line taking the alignment of the line into account.
 *
 * @return	a value
 */

float indentLeft() {
    if (isRTL) {
        switch (alignment) {
            case Element.ALIGN_LEFT:
                return left + width;
            case Element.ALIGN_CENTER:
                return left + (width / 2f);
            default:
                return left;
        }
    }
    else if (this.getSeparatorCount() == 0) {
        switch (alignment) {
            case Element.ALIGN_RIGHT:
                return left + width;
            case Element.ALIGN_CENTER:
                return left + (width / 2f);
        }
    }
    return left;
}
 
Example 8
Source File: RtfDestinationStylesheetTable.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void setToDefaults() {
	styleName = "";
	styleNr = 0;
	alignment = Element.ALIGN_LEFT;
	justificationPercentage = 0;
	firstLineIndent = 0;
	leftIndent = 0;
	rightIndent = 0;
	adustRightIndent = 0;
	mirrorIndent = 0;
	overrideWidowControl = -1;
	AutoSpaceBetweenDBCEnglish = 0;
	AutoSpaceBetweenDBCNumbers = 0;
	noCharacterWrapping = 0;
	noWordWrapping = 0;
	noOverflowPeriodComma = 0;
	
}
 
Example 9
Source File: PdfLine.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Resets the alignment of this line.
 * <P>
 * The alignment of the last line of for instance a <CODE>Paragraph</CODE>
 * that has to be justified, has to be reset to <VAR>ALIGN_LEFT</VAR>.
 */

public void resetAlignment() {
    if (alignment == Element.ALIGN_JUSTIFIED) {
        alignment = Element.ALIGN_LEFT;
    }
}
 
Example 10
Source File: MPdfWriter.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Effectue le rendu de la liste.
 *
 * @param table
 *           MBasicTable
 * @param datatable
 *           Table
 * @throws BadElementException
 *            e
 */
protected void renderList(final MBasicTable table, final Table datatable)
		throws BadElementException {
	final int columnCount = table.getColumnCount();
	final int rowCount = table.getRowCount();
	// data rows
	final Font font = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL);
	datatable.getDefaultCell().setBorderWidth(1);
	datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
	// datatable.setDefaultCellGrayFill(0);
	Object value;
	String text;
	int horizontalAlignment;
	for (int k = 0; k < rowCount; k++) {
		for (int i = 0; i < columnCount; i++) {
			value = getValueAt(table, k, i);
			if (value instanceof Number || value instanceof Date) {
				horizontalAlignment = Element.ALIGN_RIGHT;
			} else if (value instanceof Boolean) {
				horizontalAlignment = Element.ALIGN_CENTER;
			} else {
				horizontalAlignment = Element.ALIGN_LEFT;
			}
			datatable.getDefaultCell().setHorizontalAlignment(horizontalAlignment);
			text = getTextAt(table, k, i);
			datatable.addCell(new Phrase(8, text != null ? text : "", font));
		}
	}
}
 
Example 11
Source File: RtfListLevel.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Writes the initialization part of the RtfList
 * 
 * @param result The <code>OutputStream</code> to write to
 * @throws IOException On i/o errors.
 */
public void writeListBeginning(final OutputStream result) throws IOException {
    result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
    if(this.inTable) {
        result.write(RtfParagraph.IN_TABLE);
    }
    switch (this.alignment) {
        case Element.ALIGN_LEFT:
            result.write(RtfParagraphStyle.ALIGN_LEFT);
            break;
        case Element.ALIGN_RIGHT:
            result.write(RtfParagraphStyle.ALIGN_RIGHT);
            break;
        case Element.ALIGN_CENTER:
            result.write(RtfParagraphStyle.ALIGN_CENTER);
            break;
        case Element.ALIGN_JUSTIFIED:
        case Element.ALIGN_JUSTIFIED_ALL:
            result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
            break;
    }
    writeIndentation(result);
    result.write(RtfFont.FONT_SIZE);
    result.write(intToByteArray(fontNumber.getFontSize() * 2));
    if(this.symbolIndent > 0) {
        result.write(LIST_LEVEL_SYMBOL_INDENT);
        result.write(intToByteArray(this.leftIndent));
    }
}
 
Example 12
Source File: PdfLine.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Resets the alignment of this line.
 * <P>
 * The alignment of the last line of for instance a <CODE>Paragraph</CODE>
 * that has to be justified, has to be reset to <VAR>ALIGN_LEFT</VAR>.
 */

public void resetAlignment() {
    if (alignment == Element.ALIGN_JUSTIFIED) {
        alignment = Element.ALIGN_LEFT;
    }
}
 
Example 13
Source File: PdfLine.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Resets the alignment of this line.
 * <P>
 * The alignment of the last line of for instance a <CODE>Paragraph</CODE> that has to be
 * justified, has to be reset to <VAR>ALIGN_LEFT</VAR>.
 */

public void resetAlignment() {
	if (alignment == Element.ALIGN_JUSTIFIED) {
		alignment = Element.ALIGN_LEFT;
	}
}
 
Example 14
Source File: ColumnText.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Shows a line of text. Only the first line is written.
 * 
 * @param canvas where the text is to be written to
 * @param alignment the alignment. It is not influenced by the run direction
 * @param phrase the <CODE>Phrase</CODE> with the text
 * @param x the x reference position
 * @param y the y reference position
 * @param rotation the rotation to be applied in degrees counterclockwise
 * @param runDirection the run direction
 * @param arabicOptions the options for the arabic shaping
 */    
public static void showTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation, int runDirection, int arabicOptions) {
    if (alignment != Element.ALIGN_LEFT && alignment != Element.ALIGN_CENTER
        && alignment != Element.ALIGN_RIGHT)
        alignment = Element.ALIGN_LEFT;
    canvas.saveState();
    ColumnText ct = new ColumnText(canvas);
    float lly = -1;
    float ury = 2;
    float llx;
    float urx;
    switch (alignment) {
    	case Element.ALIGN_LEFT:
    		llx = 0;
    		urx = 20000;
    		break;
    	case Element.ALIGN_RIGHT:
    		llx = -20000;
    		urx = 0;
    		break;
    	default:
    		llx = -20000;
    		urx = 20000;
    		break;
    }
    if (rotation == 0) {
    	llx += x;
    	lly += y;
    	urx += x;
    	ury += y;
    }
    else {
        double alpha = rotation * Math.PI / 180.0;
        float cos = (float)Math.cos(alpha);
        float sin = (float)Math.sin(alpha);
        canvas.concatCTM(cos, sin, -sin, cos, x, y);
    }
    ct.setSimpleColumn(phrase, llx, lly, urx, ury, 2, alignment);
    if (runDirection == PdfWriter.RUN_DIRECTION_RTL) {
        if (alignment == Element.ALIGN_LEFT)
            alignment = Element.ALIGN_RIGHT;
        else if (alignment == Element.ALIGN_RIGHT)
            alignment = Element.ALIGN_LEFT;
    }
    ct.setAlignment(alignment);
    ct.setArabicOptions(arabicOptions);
    ct.setRunDirection(runDirection);
    try {
        ct.go();
    }
    catch (DocumentException e) {
        throw new ExceptionConverter(e);
    }
    canvas.restoreState();
}
 
Example 15
Source File: RtfParagraphStyle.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Writes the settings of this RtfParagraphStyle.
 * 
 * @param result The <code>OutputStream</code> to write to.
 * @throws IOException On i/o errors.
 */
private void writeParagraphSettings(final OutputStream result) throws IOException {
    if(this.keepTogether) {
        result.write(RtfParagraphStyle.KEEP_TOGETHER);
    }
    if(this.keepTogetherWithNext) {
        result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT);
    }
    switch (alignment) {
        case Element.ALIGN_LEFT:
            result.write(RtfParagraphStyle.ALIGN_LEFT);
            break;
        case Element.ALIGN_RIGHT:
            result.write(RtfParagraphStyle.ALIGN_RIGHT);
            break;
        case Element.ALIGN_CENTER:
            result.write(RtfParagraphStyle.ALIGN_CENTER);
            break;
        case Element.ALIGN_JUSTIFIED:
        case Element.ALIGN_JUSTIFIED_ALL:
            result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
            break;
    }
    result.write(FIRST_LINE_INDENT);
    result.write(intToByteArray(this.firstLineIndent));
    result.write(RtfParagraphStyle.INDENT_LEFT);
    result.write(intToByteArray(indentLeft));
    result.write(RtfParagraphStyle.INDENT_RIGHT);
    result.write(intToByteArray(indentRight));
    if(this.spacingBefore > 0) {
        result.write(RtfParagraphStyle.SPACING_BEFORE);
        result.write(intToByteArray(this.spacingBefore));
    }
    if(this.spacingAfter > 0) {
        result.write(RtfParagraphStyle.SPACING_AFTER);
        result.write(intToByteArray(this.spacingAfter));
    }
    if(this.lineLeading > 0) {
        result.write(RtfParagraph.LINE_SPACING);
        result.write(intToByteArray(this.lineLeading));
    }            
}
 
Example 16
Source File: RtfRow.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Writes the row definition/settings.
 *
 * @param result The <code>OutputStream</code> to write the definitions to.
 */
private void writeRowDefinition(final OutputStream result) throws IOException {
    result.write(ROW_BEGIN);
    this.document.outputDebugLinebreak(result);
    result.write(ROW_WIDTH_STYLE);
    result.write(ROW_WIDTH);
    result.write(intToByteArray(this.width));
    if(this.parentTable.getCellsFitToPage()) {
        result.write(ROW_KEEP_TOGETHER);
    }
    if(this.rowNumber <= this.parentTable.getHeaderRows()) {
        result.write(ROW_HEADER_ROW);
    }
    switch (this.parentTable.getAlignment()) {
        case Element.ALIGN_LEFT:
        	result.write(ROW_ALIGN_LEFT);
            break;
        case Element.ALIGN_RIGHT:
            result.write(ROW_ALIGN_RIGHT);
            break;
        case Element.ALIGN_CENTER:
            result.write(ROW_ALIGN_CENTER);
            break;
        case Element.ALIGN_JUSTIFIED:
        case Element.ALIGN_JUSTIFIED_ALL:
            result.write(ROW_ALIGN_JUSTIFIED);
            break;
    }
    result.write(ROW_GRAPH);
    RtfBorderGroup borders =this.parentTable.getBorders();
    if(borders != null) {
    	borders.writeContent(result);
    }
    
    if(this.parentTable.getCellSpacing() > 0) {
        result.write(ROW_CELL_SPACING_LEFT);
        result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
        result.write(ROW_CELL_SPACING_LEFT_STYLE);
        result.write(ROW_CELL_SPACING_TOP);
        result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
        result.write(ROW_CELL_SPACING_TOP_STYLE);
        result.write(ROW_CELL_SPACING_RIGHT);
        result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
        result.write(ROW_CELL_SPACING_RIGHT_STYLE);
        result.write(ROW_CELL_SPACING_BOTTOM);
        result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2)));
        result.write(ROW_CELL_SPACING_BOTTOM_STYLE);
    }
    
    result.write(ROW_CELL_PADDING_LEFT);
    result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2)));
    result.write(ROW_CELL_PADDING_RIGHT);
    result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2)));
    result.write(ROW_CELL_PADDING_LEFT_STYLE);
    result.write(ROW_CELL_PADDING_RIGHT_STYLE);
    
    this.document.outputDebugLinebreak(result);
    
    for(int i = 0; i < this.cells.size(); i++) {
        RtfCell rtfCell = (RtfCell) this.cells.get(i);
        rtfCell.writeDefinition(result);
    }    	
}
 
Example 17
Source File: RtfImage.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Writes the RtfImage content
 */ 
public void writeContent(final OutputStream result) throws IOException
{
	
    if(this.topLevelElement) {
        result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
        switch(alignment) {
            case Element.ALIGN_LEFT:
                result.write(RtfParagraphStyle.ALIGN_LEFT);
                break;
            case Element.ALIGN_RIGHT:
                result.write(RtfParagraphStyle.ALIGN_RIGHT);
                break;
            case Element.ALIGN_CENTER:
                result.write(RtfParagraphStyle.ALIGN_CENTER);
                break;
            case Element.ALIGN_JUSTIFIED:
                result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
                break;
        }
    }
    result.write(OPEN_GROUP);
    result.write(PICTURE_GROUP);
    result.write(OPEN_GROUP);
    result.write(PICTURE);
    switch(imageType) {
    	case Image.ORIGINAL_JPEG:
    	    result.write(PICTURE_JPEG);
    		break;
    	case Image.ORIGINAL_PNG:
        case Image.ORIGINAL_GIF:
    	    result.write(PICTURE_PNG);
    		break;
    	case Image.ORIGINAL_WMF:
    	case Image.ORIGINAL_BMP:
    	    result.write(PICTURE_WMF);
    		break;
    }
    result.write(PICTURE_WIDTH);
    result.write(intToByteArray((int) width));
    result.write(PICTURE_HEIGHT);
    result.write(intToByteArray((int) height));
    if(this.document.getDocumentSettings().isWriteImageScalingInformation()) {
        result.write(PICTURE_SCALE_X);
        result.write(intToByteArray((int)(100 * plainWidth / width)));
        result.write(PICTURE_SCALE_Y);
        result.write(intToByteArray((int)(100 * plainHeight / height)));
    }
    if(this.document.getDocumentSettings().isImagePDFConformance()) {
        result.write(PICTURE_SCALED_WIDTH);
        result.write(intToByteArray((int) (plainWidth * RtfElement.TWIPS_FACTOR)));
        result.write(PICTURE_SCALED_HEIGHT);
        result.write(intToByteArray((int) (plainHeight * RtfElement.TWIPS_FACTOR)));
    } else {
        if(this.width != this.plainWidth || this.imageType == Image.ORIGINAL_BMP) {
            result.write(PICTURE_SCALED_WIDTH);
            result.write(intToByteArray((int) (plainWidth * PIXEL_TWIPS_FACTOR)));
        }
        if(this.height != this.plainHeight || this.imageType == Image.ORIGINAL_BMP) {
            result.write(PICTURE_SCALED_HEIGHT);
            result.write(intToByteArray((int) (plainHeight * PIXEL_TWIPS_FACTOR)));
        }
    }

    if(this.document.getDocumentSettings().isImageWrittenAsBinary()) {
    	//binary
    	result.write('\n');
    	result.write(PICTURE_BINARY_DATA);
    	result.write(intToByteArray(imageDataSize()));
        result.write(DELIMITER);
        if(result instanceof RtfByteArrayBuffer) {
        	((RtfByteArrayBuffer)result).append(imageData);
        } else {
        	for(int k = 0; k < imageData.length; k++) {
	result.write(imageData[k]);
}
        }
    } else {
    	//hex encoded
        result.write(DELIMITER);
    	result.write('\n');
    	writeImageDataHexEncoded(result);
    }
    
    result.write(CLOSE_GROUP);
    result.write(CLOSE_GROUP);
    if(this.topLevelElement) {
        result.write(RtfParagraph.PARAGRAPH);
        result.write(RtfParagraph.PARAGRAPH);
    }
    result.write('\n');    	
}
 
Example 18
Source File: BarcodeInter25.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** Places the barcode in a <CODE>PdfContentByte</CODE>. The
 * barcode is always placed at coordinates (0, 0). Use the
 * translation matrix to move it elsewhere.<p>
 * The bars and text are written in the following colors:<p>
 * <P><TABLE BORDER=1>
 * <TR>
 *    <TH><P><CODE>barColor</CODE></TH>
 *    <TH><P><CODE>textColor</CODE></TH>
 *    <TH><P>Result</TH>
 *    </TR>
 * <TR>
 *    <TD><P><CODE>null</CODE></TD>
 *    <TD><P><CODE>null</CODE></TD>
 *    <TD><P>bars and text painted with current fill color</TD>
 *    </TR>
 * <TR>
 *    <TD><P><CODE>barColor</CODE></TD>
 *    <TD><P><CODE>null</CODE></TD>
 *    <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
 *    </TR>
 * <TR>
 *    <TD><P><CODE>null</CODE></TD>
 *    <TD><P><CODE>textColor</CODE></TD>
 *    <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>
 *    </TR>
 * <TR>
 *    <TD><P><CODE>barColor</CODE></TD>
 *    <TD><P><CODE>textColor</CODE></TD>
 *    <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>
 *    </TR>
 * </TABLE>
 * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed
 * @param barColor the color of the bars. It can be <CODE>null</CODE>
 * @param textColor the color of the text. It can be <CODE>null</CODE>
 * @return the dimensions the barcode occupies
 */    
public Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor) {
    String fullCode = code;
    float fontX = 0;
    if (font != null) {
        if (generateChecksum && checksumText)
            fullCode += getChecksum(fullCode);
        fontX = font.getWidthPoint(fullCode = altText != null ? altText : fullCode, size);
    }
    String bCode = keepNumbers(code);
    if (generateChecksum)
        bCode += getChecksum(bCode);
    int len = bCode.length();
    float fullWidth = len * (3 * x + 2 * x * n) + (6 + n ) * x;
    float barStartX = 0;
    float textStartX = 0;
    switch (textAlignment) {
        case Element.ALIGN_LEFT:
            break;
        case Element.ALIGN_RIGHT:
            if (fontX > fullWidth)
                barStartX = fontX - fullWidth;
            else
                textStartX = fullWidth - fontX;
            break;
        default:
            if (fontX > fullWidth)
                barStartX = (fontX - fullWidth) / 2;
            else
                textStartX = (fullWidth - fontX) / 2;
            break;
    }
    float barStartY = 0;
    float textStartY = 0;
    if (font != null) {
        if (baseline <= 0)
            textStartY = barHeight - baseline;
        else {
            textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size);
            barStartY = textStartY + baseline;
        }
    }
    byte bars[] = getBarsInter25(bCode);
    boolean print = true;
    if (barColor != null)
        cb.setColorFill(barColor);
    for (int k = 0; k < bars.length; ++k) {
        float w = (bars[k] == 0 ? x : x * n);
        if (print)
            cb.rectangle(barStartX, barStartY, w - inkSpreading, barHeight);
        print = !print;
        barStartX += w;
    }
    cb.fill();
    if (font != null) {
        if (textColor != null)
            cb.setColorFill(textColor);
        cb.beginText();
        cb.setFontAndSize(font, size);
        cb.setTextMatrix(textStartX, textStartY);
        cb.showText(fullCode);
        cb.endText();
    }
    return getBarcodeSize();
}
 
Example 19
Source File: AbstractPdfTextRenderer.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 
 */
public void initialize(
	JRPdfExporter pdfExporter, 
	PdfContentByte pdfContentByte,
	JRPrintText text, 
	JRStyledText styledText, 
	int offsetX,
	int offsetY
	)
{
	this.pdfExporter = pdfExporter;
	this.pdfContentByte = pdfContentByte;
	
	horizontalAlignment = Element.ALIGN_LEFT;
	leftOffsetFactor = 0f;
	rightOffsetFactor = 0f;
	
	//FIXMETAB 0.2f was a fair approximation
	switch (text.getHorizontalTextAlign())
	{
		case JUSTIFIED :
		{
			horizontalAlignment = Element.ALIGN_JUSTIFIED;
			leftOffsetFactor = 0f;
			rightOffsetFactor = 0f;
			break;
		}
		case RIGHT :
		{
			if (text.getRunDirectionValue() == RunDirectionEnum.LTR)
			{
				horizontalAlignment = Element.ALIGN_RIGHT;
			}
			else
			{
				horizontalAlignment = Element.ALIGN_LEFT;
			}
			leftOffsetFactor = -0.2f;
			rightOffsetFactor = 0f;
			break;
		}
		case CENTER :
		{
			horizontalAlignment = Element.ALIGN_CENTER;
			leftOffsetFactor = -0.1f;
			rightOffsetFactor = 0.1f;
			break;
		}
		case LEFT :
		default :
		{
			if (text.getRunDirectionValue() == RunDirectionEnum.LTR)
			{
				horizontalAlignment = Element.ALIGN_LEFT;
			}
			else
			{
				horizontalAlignment = Element.ALIGN_RIGHT;
			}
			leftOffsetFactor = 0f;
			rightOffsetFactor = 0.2f;
			break;
		}
	}

	super.initialize(text, styledText, offsetX, offsetY);
}
 
Example 20
Source File: ColumnText.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Shows a line of text. Only the first line is written.
 * 
 * @param canvas where the text is to be written to
 * @param alignment the alignment. It is not influenced by the run direction
 * @param phrase the <CODE>Phrase</CODE> with the text
 * @param x the x reference position
 * @param y the y reference position
 * @param rotation the rotation to be applied in degrees counterclockwise
 * @param runDirection the run direction
 * @param arabicOptions the options for the arabic shaping
 */
public static void showTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation, int runDirection, int arabicOptions) {
	if (alignment != Element.ALIGN_LEFT && alignment != Element.ALIGN_CENTER && alignment != Element.ALIGN_RIGHT) {
		alignment = Element.ALIGN_LEFT;
	}
	canvas.saveState();
	ColumnText ct = new ColumnText(canvas);
	float lly = -1;
	float ury = 2;
	float llx;
	float urx;
	switch (alignment) {
		case Element.ALIGN_LEFT:
			llx = 0;
			urx = 20000;
			break;
		case Element.ALIGN_RIGHT:
			llx = -20000;
			urx = 0;
			break;
		default:
			llx = -20000;
			urx = 20000;
			break;
	}
	if (rotation == 0) {
		llx += x;
		lly += y;
		urx += x;
		ury += y;
	} else {
		double alpha = rotation * Math.PI / 180.0;
		float cos = (float) Math.cos(alpha);
		float sin = (float) Math.sin(alpha);
		canvas.concatCTM(cos, sin, -sin, cos, x, y);
	}
	ct.setSimpleColumn(phrase, llx, lly, urx, ury, 2, alignment);
	if (runDirection == PdfWriter.RUN_DIRECTION_RTL) {
		if (alignment == Element.ALIGN_LEFT) {
			alignment = Element.ALIGN_RIGHT;
		} else if (alignment == Element.ALIGN_RIGHT) {
			alignment = Element.ALIGN_LEFT;
		}
	}
	ct.setAlignment(alignment);
	ct.setArabicOptions(arabicOptions);
	ct.setRunDirection(runDirection);
	try {
		ct.go();
	} catch (DocumentException e) {
		throw new ExceptionConverter(e);
	}
	canvas.restoreState();
}