1

I am trying to create a PDF with a mixture of Portrait and Landscape/Seascape pages.

I have rotated the pages to be Seascape where required but the content is still being output as portrait. I am reading the data in from an HTML file using iText's pdfHTML solution.

Below is some sample code I've used to do this.

var elements = HtmlConverter.ConvertToElements(htmlSource, props);
var pdf = new PdfDocument(new PdfWriter(pdfDest));
var pageOrientationHandler = new PageOrientationsEventHandler();
pdf.AddEventHandler(PdfDocumentEvent.INSERT_PAGE, pageOrientationHandler);
                
var document = new Document(pdf, PageSize.A4);
document.SetFontProvider(new DefaultFontProvider());

document.Add(new Paragraph(LOREM_IPSUM));
                
pageOrientationHandler.SetSeascape();
document.Add(new AreaBreak());
                
foreach (IElement element in elements)
{
    document.Add((Div)element);
}

pageOrientationHandler.SetSeascape();
document.Add(new AreaBreak());

foreach (IElement element in elements)
{
    document.Add((Div)element);
}

pageOrientationHandler.SetPortrait();
                
document.Add(new AreaBreak());
document.Add(new Paragraph(LOREM_IPSUM));

document.Close();

The PageOrientationsEventHandler class looks like this:

public class PageOrientationsEventHandler : IEventHandler
{
    private PdfNumber _orientation = new(0);

    public void SetPortrait()
    {
        _orientation = new PdfNumber(0);
    }

    public void SetLandscape()
    {
        _orientation = new PdfNumber(90);
    }

    public void SetSeascape()
    {
        _orientation = new PdfNumber(270);
    }

    public void HandleEvent(Event currentEvent)
    {
        PdfDocumentEvent pdfDocumentEvent = (PdfDocumentEvent)currentEvent;
        var page = pdfDocumentEvent.GetPage();
        page.Put(PdfName.Rotate, _orientation);
    }
}

What I would like is for pages 1 and 4 to be portrait orientation with portrait content (i.e. a "normal" looking page with text) and pages 2 and 3 to be Seascape with Seascape content.

What I am seeing in the generated PDF is:

This current incorrect layout

But what I want to see is:

Something like this

Can somebody tell me what I am doing wrong?

NB: These images are just showing pages 1 and 2.

EDIT:

I think I've managed to work something out, but it doesn't feel like the correct way to go about it.

I've changed the code that adds the table from the HTML to this:

foreach (IElement element in elements)
{
    document.Add((Div)element);
    Div div = (Div)element;
    div.SetRotationAngle(_radiansConverter.ToRadians(270));
    div.SetWidth(PAGE_HEIGHT);
    div.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT);
}

I am still using the page orientation handler to rotate the actual page, but also setting the table to rotate by 270 degrees, and then setting the width of the table to be slightly less than the height of the page.

This has given me the following output

Which is better than what I was after from the second image I posted (I messed up what I thought the orientation of the table should be).

Also the print preview for 2 x 2 looks like what I am after with the top of the table being aligned along the left side rather than the right:

Print Preview

Serigan
  • 21
  • 5
  • Maybe this will help https://stackoverflow.com/questions/70786737/how-to-rotate-a-specific-text-in-a-pdf-which-is-already-present-in-the-pdf-using – QuiShimo May 03 '22 at 17:00
  • @QuiShimo *'Maybe this will help'* - Not necessarily. The OP wants to change page rotation when creating the document, not after the fact. – mkl May 03 '22 at 20:26
  • @Serigan your `PageOrientationsEventHandler` changes the page rotation behind the back of the `Document`. Thus, the document does not know about the rotation and will not try to compensate. – mkl May 03 '22 at 20:29
  • @mkl Ah ok thanks that makes sense. Perhaps the only option is to keep track of which pages need to be rotate after the document is closed, and then to rotate them once the PDF has been created, rather than during the creation process? – Serigan May 04 '22 at 08:08

0 Answers0