Java Code Examples for com.itextpdf.text.pdf.PdfPTable#setWidths()

The following examples show how to use com.itextpdf.text.pdf.PdfPTable#setWidths() . 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: CreateTableDirectContent.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
     * <a href="http://stackoverflow.com/questions/43807931/creating-table-in-pdf-on-last-page-bottom-wrong-official-solution">
     * Creating table in pdf on last page bottom (wrong official solution)
     * </a>
     * <p>
     * Helper method for {@link #testCreateTableLikeUser7968180()}.
     * </p>
     */
    private static PdfPTable createFooterTable() throws DocumentException
    {
        int[] columnWidths = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
        PdfPTable datatable = new PdfPTable(columnWidths.length);
        datatable.setKeepTogether(true);
        datatable.setWidthPercentage(100);
        datatable.setWidths(columnWidths);
        datatable.getDefaultCell().setPadding(5);

//        datatable.getDefaultCell().setHorizontalAlignment(horizontalAlignment);
//        datatable.getDefaultCell().setVerticalAlignment(verticalAlignment);

        for (int i = 0; i < 100; i++)
        {
            datatable.addCell("Přehledová tabulka");
//            addCellToTable(datatable, horizontalAlignmentLeft, verticalAlignmentMiddle, "Přehledová tabulka",
//                    columnWidths.length, 1, fontTypeBold, fontSizeRegular, cellLayout_Bottom);
        }

        return datatable;
    }
 
Example 2
Source File: AbstractPdfReportBuilder.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
protected PdfPTable createGridTable(ReportDataModel dataModel, boolean isRepeatHeader) throws Exception {
	PdfPTable table = new PdfPTable(calculateGridColumnCount(dataModel.getTopColumnHeaders()));
	table.setWidthPercentage(100);
	Collection<ColumnHeader> topHeaders = dataModel.getTopColumnHeaders();
	List<Integer> widths = new ArrayList<Integer>();
	generateGridColumnWidths(topHeaders, widths);
	int[] values = new int[widths.size()];
	for (int i = 0; i < widths.size(); i++) {
		values[i] = widths.get(i);
	}
	table.setWidths(values);
	int maxHeaderLevel = getGridMaxColumngroup(topHeaders);
	createGridColumnHeader(table, topHeaders, maxHeaderLevel);
	createGridTableDatas(table, dataModel.getReportData());
	if (isRepeatHeader) {
		table.setHeaderRows(maxHeaderLevel);
	}
	return table;
}
 
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: 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);
     
}
 
Example 5
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());
    }

}