Java Code Examples for com.itextpdf.text.pdf.PdfPCell#setHorizontalAlignment()

The following examples show how to use com.itextpdf.text.pdf.PdfPCell#setHorizontalAlignment() . 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: PDFSampleMain.java    From tutorials with MIT License 7 votes vote down vote up
private static void addCustomRows(PdfPTable table) throws URISyntaxException, BadElementException, IOException {
    Path path = Paths.get(ClassLoader.getSystemResource("Java_logo.png").toURI());
    Image img = Image.getInstance(path.toAbsolutePath().toString());
    img.scalePercent(10);

    PdfPCell imageCell = new PdfPCell(img);
    table.addCell(imageCell);

    PdfPCell horizontalAlignCell = new PdfPCell(new Phrase("row 2, col 2"));
    horizontalAlignCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(horizontalAlignCell);

    PdfPCell verticalAlignCell = new PdfPCell(new Phrase("row 2, col 3"));
    verticalAlignCell.setVerticalAlignment(Element.ALIGN_BOTTOM);
    table.addCell(verticalAlignCell);
}
 
Example 3
Source File: UseRowspan.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/44005834/changing-rowspans">
 * Changing rowspans
 * </a>
 * <p>
 * Helper method of the OP.
 * </p>
 * @see #testUseRowspanLikeUser7968180()
 * @see #testUseRowspanLikeUser7968180Fixed()
 * @see #createPdf(String)
 * @see #createPdfFixed(String)
 */
private static void addCellToTableCzech(PdfPTable table, int horizontalAlignment,
        int verticalAlignment, String value, int colspan, int rowspan,
        String fontType, float fontSize) {
    BaseFont base = null;
    try {
        base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Font font = new Font(base, fontSize);
    PdfPCell cell = new PdfPCell(new Phrase(value, font));
    cell.setColspan(colspan);
    cell.setRowspan(rowspan);
    cell.setHorizontalAlignment(horizontalAlignment);
    cell.setVerticalAlignment(verticalAlignment);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);
}
 
Example 4
Source File: PdfTableExcel.java    From excel2pdf with Apache License 2.0 6 votes vote down vote up
protected void addImageByPOICell(PdfPCell pdfpCell , Cell cell , float cellWidth) throws BadElementException, MalformedURLException, IOException{
        POIImage poiImage = new POIImage().getCellImage(cell);
        byte[] bytes = poiImage.getBytes();
        if(bytes != null){
//           double cw = cellWidth;
//           double ch = pdfpCell.getFixedHeight();
//
//           double iw = poiImage.getDimension().getWidth();
//           double ih = poiImage.getDimension().getHeight();
//
//           double scale = cw / ch;
//
//           double nw = iw * scale;
//           double nh = ih - (iw - nw);
//
//           POIUtil.scale(bytes , nw  , nh);
            pdfpCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            pdfpCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            Image image = Image.getInstance(bytes);
            pdfpCell.setImage(image);
        }
    }
 
Example 5
Source File: AbstractPdfReportBuilder.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
private void createGridColumnHeader(PdfPTable table, Collection<ColumnHeader> topHeaders, int maxHeaderLevel) throws Exception {
	for (int i = 1; i < 50; i++) {
		List<ColumnHeader> result = new ArrayList<ColumnHeader>();
		generateGridHeadersByLevel(topHeaders, i, result);
		for (ColumnHeader header : result) {
			PdfPCell cell = new PdfPCell(createParagraph(header));
			if (header.getBgColor() != null) {
				int[] colors = header.getBgColor();
				cell.setBackgroundColor(new BaseColor(colors[0], colors[1], colors[2]));
			}
			cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cell.setHorizontalAlignment(header.getAlign());
			cell.setColspan(header.getColspan());
			if (header.getColumnHeaders().size() == 0) {
				int rowspan = maxHeaderLevel - (header.getLevel() - 1);
				if (rowspan > 0) {
					cell.setRowspan(rowspan);
				}
			}
			table.addCell(cell);
		}
	}
}
 
Example 6
Source File: AbstractPdfReportBuilder.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
private void createGridTableDatas(PdfPTable table, Collection<ReportData> datas) {
	for (ReportData data : datas) {
		PdfPCell cell = new PdfPCell(createParagraph(data.getTextChunk()));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		int level = this.calculateIndentationCount(data.getTextChunk().getText());
		if (data.getBgColor() != null) {
			int[] colors = data.getBgColor();
			cell.setBackgroundColor(new BaseColor(colors[0], colors[1], colors[2]));
		}
		if (level == 0) {
			cell.setHorizontalAlignment(data.getAlign());
		} else {
			cell.setIndent(20 * level);
		}
		table.addCell(cell);
	}
}
 
Example 7
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 8
Source File: PDFWriter.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
private void cellStyle(PdfPCell cell) {
	//alignment
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	
	//padding
	cell.setPaddingTop(2f);
			
	//border
	cell.setBorder(0);
	cell.setBorderColor(BaseColor.WHITE);
	
}
 
Example 9
Source File: PdfExportDialogFragment.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private void addKeyValueToTableRow(PdfPTable table, String key, String value) {
    PdfPCell keyCell = new PdfPCell(new Paragraph(key));
    keyCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    keyCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    keyCell.setPadding(10);
    table.addCell(keyCell);

    PdfPCell valueCell = new PdfPCell(new Phrase(value));
    valueCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    valueCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    valueCell.setPadding(10);
    table.addCell(valueCell);
}
 
Example 10
Source File: CreateLink.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/34408764/create-local-link-in-rotated-pdfpcell-in-itextsharp">
 * Create local link in rotated PdfPCell in iTextSharp
 * </a>
 * <p>
 * This is the equivalent Java code for the C# code in the question. Indeed, this code
 * also gives rise to the broken result. The cause is simple: Normally iText does not
 * touch the current transformation matrix. So the chunk link creation code assumes the
 * current user coordinate system to be the same as used for positioning annotations.
 * But in case of rotated cells iText does change the transformation matrix and
 * consequently the chunk link creation code positions the annotation at the wrong
 * location.
 * </p>
 */
@Test
public void testCreateLocalLinkInRotatedCell() throws IOException, DocumentException
{
    Document doc = new Document();
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(RESULT_FOLDER, "local-link.pdf")));
    doc.open();

    PdfPTable linkTable = new PdfPTable(2);
    PdfPCell linkCell = new PdfPCell();

    linkCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    linkCell.setRotation(90);
    linkCell.setFixedHeight(70);

    Anchor linkAnchor = new Anchor("Click here");
    linkAnchor.setReference("#target");
    Paragraph linkPara = new Paragraph();
    linkPara.add(linkAnchor);
    linkCell.addElement(linkPara);
    linkTable.addCell(linkCell);

    PdfPCell linkCell2 = new PdfPCell();
    Anchor linkAnchor2 = new Anchor("Click here 2");
    linkAnchor2.setReference("#target");
    Paragraph linkPara2 = new Paragraph();
    linkPara2.add(linkAnchor2);
    linkCell2.addElement(linkPara2);
    linkTable.addCell(linkCell2);

    linkTable.addCell(new PdfPCell(new Phrase("cell 3")));
    linkTable.addCell(new PdfPCell(new Phrase("cell 4")));
    doc.add(linkTable);

    doc.newPage();

    Anchor destAnchor = new Anchor("top");
    destAnchor.setName("target");
    PdfPTable destTable = new PdfPTable(1);
    PdfPCell destCell = new PdfPCell();
    Paragraph destPara = new Paragraph();
    destPara.add(destAnchor);
    destCell.addElement(destPara);
    destTable.addCell(destCell);
    destTable.addCell(new PdfPCell(new Phrase("cell 2")));
    destTable.addCell(new PdfPCell(new Phrase("cell 3")));
    destTable.addCell(new PdfPCell(new Phrase("cell 4")));
    doc.add(destTable);

    doc.close();
}
 
Example 11
Source File: PdfProducer.java    From ureport with Apache License 2.0 4 votes vote down vote up
private PdfPCell buildPdfPCell(Cell cellInfo,int cellHeight) throws Exception{
	CellStyle style=cellInfo.getCellStyle(); 
	CellStyle customStyle=cellInfo.getCustomCellStyle();
	CellStyle rowStyle=cellInfo.getRow().getCustomCellStyle();
	CellStyle colStyle=cellInfo.getColumn().getCustomCellStyle();
	PdfPCell cell=newPdfCell(cellInfo,cellHeight);
	cell.setPadding(0);
	cell.setBorder(PdfPCell.NO_BORDER);
	cell.setCellEvent(new CellBorderEvent(style,customStyle));
	int rowSpan=cellInfo.getPageRowSpan();
	if(rowSpan>0){
		cell.setRowspan(rowSpan);
	}
	int colSpan=cellInfo.getColSpan();
	if(colSpan>0){
		cell.setColspan(colSpan);
	}
	Alignment align=style.getAlign();
	if(customStyle!=null && customStyle.getAlign()!=null){
		align=customStyle.getAlign();
	}
	if(rowStyle!=null && rowStyle.getAlign()!=null){
		align=rowStyle.getAlign();
	}
	if(colStyle!=null && colStyle.getAlign()!=null){
		align=colStyle.getAlign();
	}
	if(align!=null){
		if(align.equals(Alignment.left)){
			cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		}else if(align.equals(Alignment.center)){
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		}else if(align.equals(Alignment.right)){
			cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		}
	}
	Alignment valign=style.getValign();
	if(customStyle!=null && customStyle.getValign()!=null){
		valign=customStyle.getValign();
	}
	if(rowStyle!=null && rowStyle.getValign()!=null){
		valign=rowStyle.getValign();
	}
	if(colStyle!=null && colStyle.getValign()!=null){
		valign=colStyle.getValign();
	}
	if(valign!=null){
		if(valign.equals(Alignment.top)){
			cell.setVerticalAlignment(Element.ALIGN_TOP);
		}else if(valign.equals(Alignment.middle)){
			cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		}else if(valign.equals(Alignment.bottom)){
			cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
		}
	}
	String bgcolor=style.getBgcolor();
	if(customStyle!=null && StringUtils.isNotBlank(customStyle.getBgcolor())){
		bgcolor=customStyle.getBgcolor();
	}
	if(rowStyle!=null && StringUtils.isNotBlank(rowStyle.getBgcolor())){
		bgcolor=rowStyle.getBgcolor();
	}
	if(colStyle!=null && StringUtils.isNotBlank(colStyle.getBgcolor())){
		bgcolor=colStyle.getBgcolor();
	}
	if(StringUtils.isNotEmpty(bgcolor)){
		String[] colors=bgcolor.split(",");
		cell.setBackgroundColor(new BaseColor(Integer.valueOf(colors[0]),Integer.valueOf(colors[1]),Integer.valueOf(colors[2])));
	}
	return cell;
}
 
Example 12
Source File: Metodos.java    From ExamplesAndroid with Apache License 2.0 4 votes vote down vote up
public void GeneratePDF()
{
    Document document = new Document();
    try
    {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/sdcard/TutorialesHackro/hoja.pdf"));

        document.open();

        PdfPTable table = new PdfPTable(3); // 3 columns.
        table.setWidthPercentage(100); //Width 100%
        table.setSpacingBefore(10f); //Space before table
        table.setSpacingAfter(10f); //Space after table

        //Set Column widths
        float[] columnWidths = {1f, 1f, 1f};
        table.setWidths(columnWidths);

        PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
        cell1.setBorderColor(BaseColor.BLUE);
        cell1.setPaddingLeft(10);
        cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);

        PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
        cell2.setBorderColor(BaseColor.GREEN);
        cell2.setPaddingLeft(10);
        cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

        PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
        cell3.setBorderColor(BaseColor.RED);
        cell3.setPaddingLeft(10);
        cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);

        //To avoid having the cell border and the content overlap, if you are having thick cell borders
        //cell1.setUserBorderPadding(true);
        //cell2.setUserBorderPadding(true);
        //cell3.setUserBorderPadding(true);

        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);

        document.add(table);

        createDirectoryAndSaveFile(writer, "david");
        document.close();
        writer.close();
    } catch (Exception e)
    {
        e.printStackTrace();
        Log.e("ewdfhyfafedyatfawytedfytew b",e.getMessage());
    }

}