Java Code Examples for com.itextpdf.text.Font#setColor()

The following examples show how to use com.itextpdf.text.Font#setColor() . 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: PageHeaderFooterEvent.java    From ureport with Apache License 2.0 7 votes vote down vote up
private PdfPCell buildPdfPCell(HeaderFooter phf,String text,int type){
	PdfPCell cell=new PdfPCell();
	cell.setPadding(0);
	cell.setBorder(Rectangle.NO_BORDER);
	Font font=FontBuilder.getFont(phf.getFontFamily(), phf.getFontSize(), phf.isBold(), phf.isItalic(),phf.isUnderline());
	String fontColor=phf.getForecolor();
	if(StringUtils.isNotEmpty(fontColor)){
		String[] color=fontColor.split(",");
		font.setColor(Integer.valueOf(color[0]), Integer.valueOf(color[1]), Integer.valueOf(color[2]));			
	}
	Paragraph graph=new Paragraph(text,font);
	cell.setPhrase(graph);
	switch(type){
	case 1:
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		break;
	case 2:
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		break;
	case 3:
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		break;
	}
	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	return cell;
}
 
Example 2
Source File: PdfTableExcel.java    From excel2pdf with Apache License 2.0 6 votes vote down vote up
protected Font getFontByExcel(CellStyle style) {
    Font result = new Font(Resource.BASE_FONT_CHINESE , 8 , Font.NORMAL);
    Workbook wb = excel.getWorkbook();

    short index = style.getFontIndex();
    org.apache.poi.ss.usermodel.Font font = wb.getFontAt(index);

    if(font.getBoldweight() == org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD){
        result.setStyle(Font.BOLD);
    }

    HSSFColor color = HSSFColor.getIndexHash().get(font.getColor());

    if(color != null){
        int rbg = POIUtil.getRGB(color);
        result.setColor(new BaseColor(rbg));
    }

    FontUnderline underline = FontUnderline.valueOf(font.getUnderline());
    if(underline == FontUnderline.SINGLE){
        String ulString = Font.FontStyle.UNDERLINE.getValue();
        result.setStyle(ulString);
    }
    return result;
}
 
Example 3
Source File: PdfReportPageNumber.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a header to every page
 * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
 *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
	PdfPTable table = new PdfPTable(3);
	try {
		table.setWidths(new int[]{40,5,10});
		table.setTotalWidth(100);
		table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
		Font font=new Font(chineseFont,8);
		font.setColor(new BaseColor(55,55,55));
		Paragraph paragraph=new Paragraph("第   "+writer.getPageNumber()+" 页   共",font);
		paragraph.setAlignment(Element.ALIGN_RIGHT);
		table.addCell(paragraph);
		Image img=Image.getInstance(total);
		img.scaleAbsolute(28, 28);
		PdfPCell cell = new PdfPCell(img);
		cell.setBorder(Rectangle.NO_BORDER);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
		PdfPCell c = new PdfPCell(new Paragraph("页",font));
		c.setHorizontalAlignment(Element.ALIGN_LEFT);
		c.setBorder(Rectangle.NO_BORDER);
		table.addCell(c);
		float center=(document.getPageSize().getWidth())/2-120/2;
		table.writeSelectedRows(0, -1,center,30, writer.getDirectContent());
	}
	catch(DocumentException de) {
		throw new ExceptionConverter(de);
	}
}
 
Example 4
Source File: AbstractPdfReportBuilder.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
protected Font createFont(TextChunk textChunk) {
	Font font = new Font(chineseFont);
	if (textChunk.getFontColor() != null) {
		int[] colors = textChunk.getFontColor();
		font.setColor(new BaseColor(colors[0], colors[1], colors[2]));
	}
	if (textChunk.getFontSize() > 0) {
		font.setSize(textChunk.getFontSize());
	}
	font.setStyle(textChunk.getFontStyle());
	return font;
}
 
Example 5
Source File: PersonalReportPdfCommand.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
private Font getFont(String color, boolean bold) throws Exception {
	Font font = FontFactory.getFont(BscConstants.PDF_ITEXT_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
	int rgb[] = SimpleUtils.getColorRGB2(color);
	BaseColor baseColor = new BaseColor(rgb[0], rgb[1], rgb[2]);
	font.setSize(9);
	font.setColor(baseColor);
	return font;
}
 
Example 6
Source File: KpiReportPdfCommand.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
private Font getFont(String color, boolean bold) throws Exception {
	Font font = FontFactory.getFont(BscConstants.PDF_ITEXT_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
	int rgb[] = SimpleUtils.getColorRGB2(color);
	BaseColor baseColor = new BaseColor(rgb[0], rgb[1], rgb[2]);
	font.setSize(9);
	if (bold) {
		font.setSize(14);
		font.setStyle(Font.BOLD);
	}		
	font.setColor(baseColor);
	return font;
}
 
Example 7
Source File: OrganizationReportPdfCommand.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
private Font getFont(String color, boolean bold) throws Exception {
	Font font = FontFactory.getFont(BscConstants.PDF_ITEXT_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
	int rgb[] = SimpleUtils.getColorRGB2(color);
	BaseColor baseColor = new BaseColor(rgb[0], rgb[1], rgb[2]);
	font.setSize(9);
	font.setColor(baseColor);
	return font;
}
 
Example 8
Source File: CellPhrase.java    From ureport with Apache License 2.0 4 votes vote down vote up
private Font buildPdfFont(Cell cell){
	CellStyle style=cell.getCellStyle();
	CellStyle customStyle=cell.getCustomCellStyle();
	CellStyle rowStyle=cell.getRow().getCustomCellStyle();
	CellStyle colStyle=cell.getColumn().getCustomCellStyle();
	String fontName=style.getFontFamily();
	if(customStyle!=null && StringUtils.isNotBlank(customStyle.getFontFamily())){
		fontName=customStyle.getFontFamily();
	}
	if(rowStyle!=null && StringUtils.isNotBlank(rowStyle.getFontFamily())){
		fontName=rowStyle.getFontFamily();
	}
	if(colStyle!=null && StringUtils.isNotBlank(colStyle.getFontFamily())){
		fontName=colStyle.getFontFamily();
	}
	int fontSize=style.getFontSize();
	Boolean bold=style.getBold(),italic=style.getItalic(),underline=style.getUnderline();
	if(customStyle!=null){
		if(customStyle.getBold()!=null){
			bold=customStyle.getBold();
		}
		if(customStyle.getItalic()!=null){
			italic=customStyle.getItalic();
		}
		if(customStyle.getUnderline()!=null){
			underline=customStyle.getUnderline();
		}
		if(customStyle.getFontSize()>0){
			fontSize=customStyle.getFontSize();
		}
	}
	if(rowStyle!=null){
		if(rowStyle.getBold()!=null){
			bold=rowStyle.getBold();
		}
		if(rowStyle.getItalic()!=null){
			italic=rowStyle.getItalic();
		}
		if(rowStyle.getUnderline()!=null){
			underline=rowStyle.getUnderline();
		}
		if(rowStyle.getFontSize()>0){
			fontSize=rowStyle.getFontSize();
		}
	}
	if(colStyle!=null){
		if(colStyle.getBold()!=null){
			bold=colStyle.getBold();
		}
		if(colStyle.getItalic()!=null){
			italic=colStyle.getItalic();
		}
		if(colStyle.getUnderline()!=null){
			underline=colStyle.getUnderline();
		}
		if(colStyle.getFontSize()>0){
			fontSize=colStyle.getFontSize();
		}
	}
	if(bold==null)bold=false;
	if(italic==null)italic=false;
	if(underline==null)underline=false;
	if(StringUtils.isBlank(fontName)){
		fontName="宋体";
	}
	Font font=FontBuilder.getFont(fontName, fontSize,bold,italic,underline);
	String fontColor=style.getForecolor();
	if(customStyle!=null && StringUtils.isNotBlank(customStyle.getForecolor())){
		fontColor=customStyle.getForecolor();
	}
	if(rowStyle!=null && StringUtils.isNotBlank(rowStyle.getForecolor())){
		fontColor=rowStyle.getForecolor();
	}
	if(colStyle!=null && StringUtils.isNotBlank(colStyle.getForecolor())){
		fontColor=colStyle.getForecolor();
	}
	if(StringUtils.isNotEmpty(fontColor)){
		String[] color=fontColor.split(",");
		font.setColor(Integer.valueOf(color[0]), Integer.valueOf(color[1]), Integer.valueOf(color[2]));			
	}
	return font;
}
 
Example 9
Source File: HRPDFBuilderImpl.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected void buildPdfDocument(Map<String, Object> model, Document doc,
        PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    // get data model which is passed by the Spring container
    List<HrmsLogin> users = (List<HrmsLogin>) model.get("allUsers");
     
    doc.add(new Paragraph("Master List of Users"));
     
    PdfPTable table = new PdfPTable(4);
    table.setWidthPercentage(100.0f);
    table.setWidths(new float[] {3.0f, 2.0f, 2.0f, 2.0f});
    table.setSpacingBefore(10);
     
    // define font for table header row
    Font font = FontFactory.getFont(FontFactory.HELVETICA);
    font.setColor(BaseColor.WHITE);
     
    // define table header cell
    PdfPCell cell = new PdfPCell();
    cell.setBackgroundColor(BaseColor.BLUE);
    cell.setPadding(5);
     
    // write table header
    cell.setPhrase(new Phrase("Employee ID", font));
    table.addCell(cell);
     
    cell.setPhrase(new Phrase("Username", font));
    table.addCell(cell);
	 
    cell.setPhrase(new Phrase("Password", font));
    table.addCell(cell);
     
    cell.setPhrase(new Phrase("Role", font));
    table.addCell(cell);
     
   
     
    // write table row data
    for (HrmsLogin user : users) {
        table.addCell(user.getHrmsEmployeeDetails().getEmpId()+"");
        table.addCell(user.getUsername());
        table.addCell(user.getPassword());
        table.addCell(user.getRole());
        
    }
     
    doc.add(table);
     
}