Rotating PDF in C# using iTextSharp

后端 未结 5 1162
野的像风
野的像风 2020-12-08 17:04

I am using the below function to split the pdf into two.

Though it is spliting the pdf, the content is appearing upside down. How do I rotate it by 180 degrees.

相关标签:
5条回答
  • 2020-12-08 17:16

    You should try this. It worked for me:

                    if (rotation == 90 || rotation == 270)
                    {
    
                        if (rotation == 90)
                        {
                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
                        }
                        if (rotation == 270)
                        {
                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(pagenumber).Width, 0);
    
                        }
    
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
    
    0 讨论(0)
  • 2020-12-08 17:20

    @TimS' answer was very close to perfect, and provided the correct parameters to AddTemplate, but I needed to make a few additions to allow for 90, 180, 270 rotation of a PDF where the pages already have rotation of 0, 90, 180 or 270:

    Assuming a parameter of RotateFlipType rotateFlipType is passed to the function to specify the rotation (the handy enum from the GDI+ RotateFlip call):

    iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
    iTextSharp.text.pdf.PdfImportedPage page;
    int rotation;
    int i = 0;
    while (i < pageCount)
    {
        i++;
        var pageSize = reader.GetPageSizeWithRotation(i);
    
        // Pull in the page from the reader
        page = writer.GetImportedPage(reader, i);
    
        // Get current page rotation in degrees
        rotation = pageSize.Rotation;
    
        // Default to the current page size
        iTextSharp.text.Rectangle newPageSize = null;
    
        // Apply our additional requested rotation (switch height and width as required)
        switch (rotateFlipType)
        {
            case RotateFlipType.RotateNoneFlipNone:
                newPageSize = new iTextSharp.text.Rectangle(pageSize);
                break;
            case RotateFlipType.Rotate90FlipNone:
                rotation += 90;
                newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
                break;
            case RotateFlipType.Rotate180FlipNone:
                rotation += 180;
                newPageSize = new iTextSharp.text.Rectangle(pageSize.Width, pageSize.Height, rotation);
                break;
            case RotateFlipType.Rotate270FlipNone:
                rotation += 270;
                newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
                break;
        }
    
        // Cap rotation into the 0-359 range for subsequent check
        rotation %= 360;
    
        document.SetPageSize(newPageSize);
        document.NewPage();
    
        // based on the rotation write out the page dimensions
        switch (rotation)
        {
            case 0:
                cb.AddTemplate(page, 0, 0);
                break;
            case 90:
                cb.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height);
                break;
            case 180:
                cb.AddTemplate(page, -1f, 0, 0, -1f, newPageSize.Width, newPageSize.Height);
                break;
            case 270:
                cb.AddTemplate(page, 0, 1f, -1f, 0, newPageSize.Width, 0);
                break;
            default:
                throw new System.Exception(string.Format("Unexpected rotation of {0} degrees", rotation));
                break;
        }
    }
    

    Hopefully this will help someone else wishing to correct the rotation of incoming PDFs. Took me 2 days to perfect it.

    0 讨论(0)
  • 2020-12-08 17:23

    I have found the above answers do not rotate correctly for all 4 of the main rotations.

    Below is my code to handle 0, 90, 180 and 270 correctly. This has been tested with a PDF rotated in each of these directions.

    var pageRotation = reader.GetPageRotation(currentPageIndex);
    var pageWidth = reader.GetPageSizeWithRotation(currentPageIndex).Width;
    var pageHeight = reader.GetPageSizeWithRotation(currentPageIndex).Height;
    switch (pageRotation)
    {
        case 0:
            writer.DirectContent.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
            break;
    
        case 90:
            writer.DirectContent.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, pageHeight);
            break;
    
        case 180:
            writer.DirectContent.AddTemplate(importedPage, -1f, 0, 0, -1f, pageWidth, pageHeight);
            break;
    
        case 270:
            writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
            break;
    
        default:
            throw new InvalidOperationException(string.Format("Unexpected page rotation: [{0}].", pageRotation));
    }
    
    0 讨论(0)
  • 2020-12-08 17:26

    A little change in above code old code

    case 270:
        writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
    

    new code

    case 270:
        writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageHeight, 0);
    

    This will fix the issue with 270 degree rotation

    0 讨论(0)
  • 2020-12-08 17:34

    I tried your code and it worked fine for me; split pages kept their original orientation.

    A workaround might be to explicitly rotate your pages 180 degrees.

    Replace:

    cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); 
    

    With:

    cb1.AddTemplate(page, -1f, 0, 0, -1f, 
                    inputPdf.GetPageSizeWithRotation(i).Width, 
                    inputPdf.GetPageSizeWithRotation(i).Height);
    

    If your call to inputPdf.GetPageRotation(i) returns 180 then you can handle this in the if statement that follows (using my suggested code for rotation == 180).

    0 讨论(0)
提交回复
热议问题