-1

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

1 回答 1

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 portrait
    int degOfRotat = 270;
    //this is for portrait 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;
}
于 2018-06-30T08:07:35.313 回答