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:
But what I want to see is:
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.
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: