我无法使用 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;
}
}