问题描述
- Hi Every One
- 我使用包含添加到MultiColumnText的PdfPTable的iText生成的pdf报告,有时变得太大以至于它会被拆分多页,
- 这里添加了示例代码,其工作为
- Hi Every One
- I've a pdf report generated with iText containing a PdfPTable added to MultiColumnText, sometimes becomes so large that it will be split on more than one page,
- Here am added sample Code, Its working as
Page -1
--------------------------- -------------------------------------------------- -------------------------------------------------- ------------------- +
| 1st - Emp1-NO |第二 - Emp1-名称| 3 - Emp1-Desig |
--------------------------------------------------------------------------------------------------------------------------------------------------+| 1st - Emp1-NO | 2nd - Emp1-Name| 3rd - Emp1-Desig |
| 4 - Emp2-NO | 5号 - Emp2-名称| 6th - Emp2-Desig |
| 4th - Emp2-NO | 5th - Emp2-Name| 6th - Emp2-Desig |
| 7日 - Emp3-NO | 8号 - Emp3-名称| 9th - Emp3-Desig |
| 7th - Emp3-NO | 8th - Emp3-Name| 9th - Emp3-Desig |
----------------------------- -------------------------------------------------- -------------------------------------------------- ----------------- +
--------------------------------------------------------------------------------------------------------------------------------------------------+
-
但我要填写数据为第一个没有所有行然后每列,即逐列
如下所示
Page -1
---------- -------------------------------------------------- -------------------------------------------------- ------------------------------------ +
| 1st - Emp1-NO | 4 - Emp1-Name | 7th - Emp1-Desig |
--------------------------------------------------------------------------------------------------------------------------------------------------+| 1st - Emp1-NO | 4th - Emp1-Name| 7th - Emp1-Desig |
|第二 - Emp2-NO | 5号 - Emp2-名称| 8th - Emp2-Desig |
| 2nd - Emp2-NO | 5th - Emp2-Name| 8th - Emp2-Desig |
| 3 - Emp3-NO | 6th - Emp3-Name | 9th - Emp3-Desig |
| 3rd - Emp3-NO | 6th - Emp3-Name| 9th - Emp3-Desig |
----------------------------- -------------------------------------------------- -------------------------------------------------- ----------------- +
--------------------------------------------------------------------------------------------------------------------------------------------------+
**我的代码是**
**My Code is **
package com.ace.pdf;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPage;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
public class AttendanceTimeSheet {
public static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, BaseColor.BLACK);
public static Font subSecFont = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD, BaseColor.BLUE);
public static Font tableCellFont = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
public static Font smallFont = new Font(Font.FontFamily.TIMES_ROMAN, 6, Font.NORMAL);
public static Font smallFontofnames = new Font(Font.FontFamily.TIMES_ROMAN, 7, Font.BOLD);
public static Font smallFontofcomp = new Font(Font.FontFamily.TIMES_ROMAN, 7, Font.BOLD);
public static Font smallFontofstopped = new Font(Font.FontFamily.TIMES_ROMAN, 7, Font.BOLD);
public static Font smallFontofGPS = new Font(Font.FontFamily.TIMES_ROMAN, 7, Font.BOLD);
public static Font smallWhiteFont = new Font(Font.FontFamily.TIMES_ROMAN, 6, Font.NORMAL);
public static Font smallFontUnderline = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
public static Font smallHeadersFont = new Font(Font.FontFamily.TIMES_ROMAN, 7, Font.BOLD);
public static Font FontForAllBold = new Font(Font.FontFamily.TIMES_ROMAN, 7, Font.BOLD);
public static Font headerBold = new Font(Font.FontFamily.TIMES_ROMAN, 5, Font.BOLD);
public static Font headerBoldData = new Font(Font.FontFamily.TIMES_ROMAN, 5, Font.BOLD);
public class Rotate extends PdfPageEventHelper {
protected PdfNumber rotation = PdfPage.PORTRAIT;
public void setRotation(PdfNumber rotation) {
this.rotation = rotation;
}
public void onEndPage(PdfWriter writer, Document document) {
//writer.addPageDictEntry(PdfName.ROTATE, rotation);
}
}
public static void main(String[] args) throws IOException, DocumentException {
new AttendanceTimeSheet().createPdf();
}
public void createPdf() throws IOException, DocumentException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Rectangle pagesize = new Rectangle(842f, 595f);
Document document = new Document(pagesize, 10f, 10f, 10f, 10f);
try {
OutputStream file = new FileOutputStream(new File("E:\\ExamplePDF\\AttSheet+" + new Date().getTime() + ".pdf"));
PdfWriter writer = PdfWriter.getInstance(document, file);
Rotate rotation = new Rotate();
writer.setPageEvent(rotation);
rotation.setRotation(PdfPage.SEASCAPE);
document.open();
Paragraph paraRepName = new Paragraph();
paraRepName.add(new Paragraph("SHEET", catFont));
paraRepName.setSpacingAfter(04);
document.add(paraRepName);
int pageNo = 1;
PdfPTable headeTable = headerTable_Main();
document.add(headeTable);
PdfPTable data_HeadeTable = headerTable_Data();
document.add(data_HeadeTable);
for (int rows = 1; rows <= 10; rows++) {
//Here will dynamic data some List
PdfPTable rdTable = rawDataTable();
document.add(rdTable);
}
document.newPage();
pageNo = pageNo + 1;
Paragraph balnkParaSummary = new Paragraph();
document.add(balnkParaSummary);
document.close();
System.out.println("Pdf created successfully..");
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
private static PdfPTable headerTable_Main() throws DocumentException {
PdfPTable table = new PdfPTable(7);
table.setWidthPercentage(100);
table.setWidths(new int[] { 20, 40, 10, 10, 10, 10, 10 });
PdfPCell cell = new PdfPCell();
for (int tblrow = 1; tblrow <= 3; tblrow++) {
String cellHead1 = "";
String cellHead2 = "";
String cellHead3 = "";
String cellHead4 = "";
String cellHead5 = "";
String cellHead6 = "";
String cellHead7 = "";
if (tblrow == 1) {
cellHead1 = "Project : ACE";
cellHead2 = "";
cellHead3 = "Legend:";
cellHead4 = "AB = Absent";
cellHead5 = "R = Rest Day";
cellHead6 = "O = Off Day";
cellHead7 = "H = Holi Day";
} else if (tblrow == 2) {
cellHead1 = "CLIENT : DOPMO";
cellHead2 = "";
cellHead3 = "";
cellHead4 = "AL = Annual Leave";
cellHead5 = "EL = Emg Leave";
cellHead6 = "L1 = ";
cellHead7 = "L2 = ";
} else {
cellHead1 = "Period : ";
cellHead2 = "";
cellHead3 = "";
cellHead4 = "";
cellHead5 = "";
cellHead6 = "";
cellHead7 = "";
}
for (int i = 1; i <= 7; i++) {
if (i == 1) {
cell = new PdfPCell(new Phrase(cellHead1, headerBold));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
} else if (i == 2) {
cell = new PdfPCell(new Phrase(cellHead2, headerBold));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
} else if (i == 3) {
cell = new PdfPCell(new Phrase(cellHead3, headerBold));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
} else if (i == 4) {
cell = new PdfPCell(new Phrase(cellHead4, headerBold));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
} else if (i == 5) {
cell = new PdfPCell(new Phrase(cellHead5, headerBold));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
} else if (i == 6) {
cell = new PdfPCell(new Phrase(cellHead6, headerBold));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
} else {
cell = new PdfPCell(new Phrase(cellHead7, headerBold));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
}
if (i <= 3 || tblrow == 3) {
cell.setBorderWidthRight(0);
cell.setBorderWidthLeft(0);
cell.setBorderWidthTop(0);
cell.setBorderWidthBottom(0);
cell.setBorderColorBottom(BaseColor.WHITE);
cell.setBorderColorLeft(BaseColor.WHITE);
cell.setBorderColorRight(BaseColor.WHITE);
cell.setBorderColorTop(BaseColor.WHITE);
}
cell.setBackgroundColor(BaseColor.WHITE);
cell.setFixedHeight(8);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
table.addCell(cell);
}
}
return table;
}
private static PdfPTable headerTable_Data() throws DocumentException {
PdfPTable table = new PdfPTable(33);
table.setWidthPercentage(100);
table.setWidths(new int[] { 10, 10, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02,
02, 02, 02, 02, 02 });
table.setSpacingBefore(05.0f);
PdfPCell cell = new PdfPCell();
Date currentDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
for (int tblRow = 1; tblRow <= 2; tblRow++) {
currentDate = new Date();
cal = Calendar.getInstance();
cal.setTime(currentDate);
int dateNo = 1;
int dateNos = 1;
for (int i = 1; i <= 33; i++) {
cal.set(Calendar.DATE, dateNos);
if (i == 1) {
if (tblRow != 1) {
cell = new PdfPCell(new Phrase("Name", smallHeadersFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
}
} else if (i == 2) {
if (tblRow != 1) {
cell = new PdfPCell(new Phrase("Role", smallHeadersFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
}
} else {
if (tblRow == 1) {
if (i >= 3) {
cell = new PdfPCell(new Phrase(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US), headerBold));
if(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US) == "Fri"){
cell.setBackgroundColor(BaseColor.BLUE);
}else{
cell.setBackgroundColor(BaseColor.WHITE);
}
dateNos = dateNos + 1;
}
} else {
cell = new PdfPCell(new Phrase("" + dateNo, headerBold));
if(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US) == "Fri"){
cell.setBackgroundColor(BaseColor.BLUE);
}else{
cell.setBackgroundColor(BaseColor.WHITE);
}
dateNo = dateNo + 1;
dateNos = dateNos + 1;
}
}
cell.setFixedHeight(15);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
table.addCell(cell);
}
}
return table;
}
private static PdfPTable rawDataTable() throws DocumentException {
PdfPTable table = new PdfPTable(33);
table.setWidthPercentage(100);
table.setWidths(new int[] { 10, 10, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02, 02,
02, 02, 02, 02, 02 });
table.setSpacingBefore(0.0f);
String head1;
String head2;
Date currentDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
for (int j = 1; j <= 2; j++) {
currentDate = new Date();
cal = Calendar.getInstance();
cal.setTime(currentDate);
if (j == 1) {
head1 = "Rajinikanth Developer";
head2 = "Designation ";
} else {
head1 = "";
head2 = "";
}
PdfPCell cell = new PdfPCell();
int dateNos = 1;
for (int i = 1; i <= 33; i++) {
cal.set(Calendar.DATE, dateNos);
if (i == 1) {
if (j == 2) {
cell = new PdfPCell(new Phrase("10:12", headerBoldData));
dateNos = dateNos + 1;
} else {
cell = new PdfPCell(new Phrase(head1, headerBoldData));
}
} else if (i == 2) {
if (j == 2) {
cell = new PdfPCell(new Phrase("10:13", headerBoldData));
dateNos = dateNos + 1;
} else {
cell = new PdfPCell(new Phrase(head2, headerBoldData));
}
} else {
if (j == 1) {
cell = new PdfPCell(new Phrase("10:11", headerBoldData));
dateNos = dateNos + 1;
} else {
cell = new PdfPCell(new Phrase("08:59", headerBoldData));
dateNos = dateNos + 1;
}
}
if (j == 1 && i <= 2) {
cell.setRowspan(2);
}
cell.setFixedHeight(15);
if(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US) == "Fri"){
cell.setBackgroundColor(BaseColor.BLUE);
}else{
cell.setBackgroundColor(BaseColor.WHITE);
}
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
table.addCell(cell);
}
}
return table;
}
}
**样本PDF是**
**sample PDF is **
问题更新
-
我有150名员工数据
I have 150 Employees Data
每位员工都有姓名& InTime& OutTime
Each Employee Have Name & InTime & OutTime
请找PDF图像
我需要有关细胞填充逻辑的帮助作为
a。在第1栏打印员工ID - 150名员工
b。然后已经打印了每一行第二列,员工姓名为
c。然后使用In time 3rd Column打印
I Need Help on Logic of cell fill asa. Print Employee Ids in 1st Column - 150 employeesb. Then already printed each row 2nd Column with Employee Namec. Then print with In time 3rd Column
只需从 - 首先打印3行与第1列[1066,1067,1069] Ids然后再次打印退出3行第2列[名称-ABCD,名称-XYZ,名称-PQR] ..等等
Simply From - First Print 3 rows with 1st Column [1066,1067,1069] Ids Then again Print again Exiting 3 rows 2nd Column[Name-ABCD, Name-XYZ, Name-PQR]..etc
ConcurrentHashMap<String,EmpHistory> emp_HistoryList = new ConcurrentHashMap<String, EmpHistory>();
emp_HistoryList = this.getHistoryData();
for(EmpInfo emp : empInfoList){
empHistory = emp_HistoryList.get(emp.getId());
//Print all 105 Emps rows with IDs in 1st Column
//Print each Emp row Name and IN time column
}
问题更新 - 2
- 我是使用带有iText的Java
- 我需要将文本放在列中,
- 我创建的PDFTable为:
PdfPTable table = new PdfPTable (33);
- 问题是PdfPTable逐行填充表,也就是说,首先在第1行的第1列中给出单元格,然后第1行的第2列,依此类推,但我需要逐列进行,因为这就是数据的输入方式。
- I am using Java with iText
- I need to put text in columns,
- I Created PDFTable as :
PdfPTable table = new PdfPTable(33);
- The problem is that PdfPTable fills the table row by row, that is, you first give the cell in column 1 of row 1, then column 2 of row 1, and so on, but I need to do it column by column, because that is how the data is being fed to me.
我需要像:
1 3 5
2 4 6
- 当前代码为
1 2 3
4 5 6
- 请知道我从我的示例代码中做了哪些更改
- 请帮忙,谢谢Advan ce。
推荐答案
出于逻辑原因,表格在iText中逐行填写:
For logical reasons, tables are filled row by row in iText:
如果您想更改订单,则需要首先通过创建数据矩阵来自己完成。一种方法是创建一个二维数组(还有其他方法,但为了简单起见,我使用的是数组)。
If you want to change the order, you need to do this yourself by creating a data matrix first. One way to do this, is by creating a two-dimensional array (there are other ways, but I'm using an array for the sake of simplicity).
示例显示了它是如何完成的。
The RowColumnOrder example shows how it's done.
这是正常行为:
document.add(new Paragraph("By design tables are filled row by row:"));
PdfPTable table = new PdfPTable(5);
table.setSpacingBefore(10);
table.setSpacingAfter(10);
for (int i = 1; i <= 15; i++) {
table.addCell("cell " + i);
}
document.add(table);
您想要更改此行为,这意味着您必须创建一个矩阵,您可以在其中更改订单行和列已填充:
You want to change this behavior, which means that you have to create a matrix in which you change the order rows and columns are filled:
String[][] array = new String[3][];
int column = 0;
int row = 0;
for (int i = 1; i <= 15; i++) {
if (column == 0) {
array[row] = new String[5];
}
array[row++][column] = "cell " + i;
if (row == 3) {
column++;
row = 0;
}
}
如您所见,没有涉及iText代码。我有15个单元格添加到一个包含5列和3行的表中。为实现这一点,我创建了一个 String [3] [5]
数组,其中我填充了一列的每一行,然后在当前列已满时切换到下一列。一旦我有了二维矩阵,我就可以用它来填充 PdfPTable
:
As you can see, there is no iText code involved. I have 15 cells to add in a table with 5 columns and 3 rows. To achieve this, I create a String[3][5]
array of which I fill every row of a column, then switch to the next column when the current column is full. Once I have the two-dimensional matrix, I can use it to fill a PdfPTable
:
table = new PdfPTable(5);
table.setSpacingBefore(10);
for (String[] r : array) {
for (String c : r) {
table.addCell(c);
}
}
document.add(table);
如您所见,为您提供所需的结果。
As you can see, the second table in row_column_order.pdf gives you the result you want.
这篇关于使用Java中的iText逐列填充每行的动态数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!