-1

I am not able rotate the whole pdftable with using itext. I have rotated perticular page in pdf, it works fine but it also rotates the data inside that table in the page which i dont want to implement. I want to rotate the whole table in portrait page.

Below is the code for the same.

Please give suggestions.

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;
    }
}
Riddhi Makwana
  • 29
  • 1
  • 1
  • 5
  • 1
    Can you give sketches of what you expect and what happens in contrast to your expectation? I don't get a clear image from what you describe... Furthermore, there are some issues in your code; e.g. you add content to the document in `onStartPage` which iText development constantly advises against in iText 5.x. – mkl Feb 08 '17 at 16:30
  • Thank you for the response as i am not able to add pdf over here, I have describe the things, Again I am trying to describe it for you in a better way, I have requirement of changing the orientation of pdf for different different control, In one control(Grid) I have multiple parent child rows, As I am rotation the page from landscape to portrait, It also rotatesthe content of it, But I do not want to rotate the content, I just want set the page size as portrait and don't want to rotate the content of it – Riddhi Makwana Feb 09 '17 at 06:07
  • By "sketches" I did not necessarily mean full PDFs, they could also be mere images showing quick and dirty pen&paper sketches. I simply have not yet understood how you eventually want tables and table contents to be oriented on the portrait result page. That been said, I see you changing the document page size (an operation relevant only for pages to come) and page dictionary **Rotate** entries (an operation relevant only for the current page) in multiple places in your code. Most likely you will only need one of those operation types, merely done at the right time. – mkl Feb 09 '17 at 09:08

1 Answers1

1

If you haven't solved it so far; here is a possible solution for your problem:
iText doesn't provide an easy rotation of the whole Table. So you have to rotate its cells and add them to a new table.

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;
}