我无法使用itext旋转整个pdftable。我已经在pdf中旋转了特定页面,它工作正常,但它也旋转了我不想实现的页面中该表内的数据。我想在纵向页面中旋转整个表格。
以下是相同的代码。
请提出建议。
if(totalColumnsOfGrid<=7){
document.add(table);
document.newPage();
//document.setPageSize(new Rectangle(792f, 612f));
pdfWriterEvent.setRotation(PdfPage.PORTRAIT);
document.setPageSize(PageSize.LETTER.rotate());
}
public static PdfWriterEvent setPageEventData(com.itextpdf.text.Document document,String outputFilePath,String headerLogoPath,
String headerText,String footerText,String waterMarkLogoPath) {
File tempFilePath=new File(""+new Random().nextInt()+".pdf");
PdfPTable tempHeaderTable=getPdfHeaderTable(headerLogoPath, headerText);
PdfPTable tempFooterTable=getPdfFooterTable(footerText);
Chunk waterMarkLogo=getImageChunk(waterMarkLogoPath,360,280,true);
try
{
PdfPTable headerTable=getPdfHeaderTable(headerLogoPath, headerText);
PdfPTable footerTable=getPdfFooterTable(footerText);
PdfWriterEvent pdfWriterEvent = new PdfWriterEvent(headerTable,footerTable,waterMarkLogo);
document.setMargins(document.leftMargin(), document.rightMargin(), tempHeaderTable.getTotalHeight()+10,
tempFooterTable.getTotalHeight()+14);
FileOutputStream fos = new FileOutputStream(outputFilePath);
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.setPageEvent(pdfWriterEvent);
return pdfWriterEvent;
}
class PdfWriterEvent extends PdfPageEventHelper
{
PdfTemplate total;
PdfPTable headerTable;
PdfPTable footerTable;
Rectangle page;
Chunk waterMarkChunk;
PdfNumber rotation;
PdfWriterEvent()
{}
PdfWriterEvent(PdfPTable headerTable,PdfPTable footerTable,Chunk waterMarkChunk)
{
this.headerTable=headerTable;
this.footerTable=footerTable;
this.waterMarkChunk=waterMarkChunk;
}
public void onOpenDocument(PdfWriter writer, com.itextpdf.text.Document document) {
total = writer.getDirectContent().createTemplate(30, 16);
page= document.getPageSize();
}
public void onEndPage(PdfWriter writer, com.itextpdf.text.Document document)
{
try
{
writer.addPageDictEntry(PdfName.ROTATE, rotation);
document.setPageSize(PageSize.A4.rotate());
//Some code over here
}
catch(DocumentException de)
{
throw new ExceptionConverter(de);
}
}
@Override
public void onStartPage(PdfWriter writer, com.itextpdf.text.Document document)
{
page= document.getPageSize();
document.setPageSize(PageSize.A4.rotate());
headerTable.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
headerTable.writeSelectedRows(0, -1,document.leftMargin(),page.getHeight(), writer.getDirectContent());
}
public void onCloseDocument(PdfWriter writer, com.itextpdf.text.Document document) {
ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber() - 1),HTMLTranscationUtil.normalFont),2, 2, 0);
}
public void setRotation(PdfNumber rotation) {
this.rotation = rotation;
}
}
答案 0 :(得分:1)
如果到目前为止您还没有解决,这是您可能遇到的问题的解决方案:
iText不能轻松旋转整个表格。因此,您必须旋转其单元格并将其添加到新表中。
public static PdfPTable rotateTable(PdfPTable table){
//in the rotated table the columns and rows are transposed
int numberOfColumns = table.getRows().size();
PdfPTable rotatedTable = new PdfPTable(numberOfColumns);
int numberOfRows = table.getRow(0).getCells()[0].length;
//if you desire landscape you have to use 90, this is seascape
int degOfRotat = 270;
//this is for seascape also, if you want it for landscape you have to change indices
for(int columnOld = 0; columnOld < numberOfRows;columnOld++){
for(int rowOld = numberOfColumns -1; rowOld >= 0; rowOld--){
PdfPCell currentCell = table.getRow(rowOld).getCells()[columnOld];
currentCell.setRotation(degOfRotat);
rotatedTbale.addCell(currentCell);
}
}
return rotatedTable;
}