I'm using Itext7, I've been trying to change left margin of the document with no luck. I have two columns, I want left margin for odd page 30, and for even to be 50.
PageHeaderHandler pageHeaderHandler = new PageHeaderHandler(SummaryDoc, pdfdoc, sumTable);
pdfdoc.AddEventHandler(PdfDocumentEvent.END_PAGE, pageHeaderHandler);
columnWidth = ((PageSize.A4.GetWidth() - offSet * 2) / 2 - gutter) + 30;
columnHeight = PageSize.A4.GetHeight() - offSet * 2;
Rectangle[] columns = {
new Rectangle(offSet, topMargin, columnWidth, columnHeight),
new Rectangle(offSet + columnWidth + gutter, topMargin, columnWidth, columnHeight)};
SummaryDoc.Doc.SetRenderer(new ColumnDocumentRenderer(SummaryDoc.Doc, columns));
And my Handler :
public void HandleEvent(Event @event)
{
if (doc.CurrentPage() % 2 == 1)
{
doc.Doc.SetLeftMargin(30);
}
else
{
doc.Doc.SetLeftMargin(50);
}
}
This doesn't work, it works for a normal DocumentRenderer but not here. Is there a way to set leftMargin for ColumnDocumentRenderer ?
If you look at the implementation of ColumnDocumentRenderer
, first of all you would see that it does not take margins into account (margins concept might be controversial for several columns and mean different things for different people) and columns are defined in absolute units.
But most importantly, you can see that the implementation of ColumnDocumentRenderer
is pretty straightforward and does not contain a lot of code. You can get inspiration from there to implement your own renderer that would use different column positions depending on whether the page is odd or even.
Here is a very basic implementation of such custom renderer class:
private static class VariableColumnDocumentRenderer extends DocumentRenderer {
protected Rectangle[][] columns;
protected int nextAreaNumber;
protected int curColumnLayoutId;
public VariableColumnDocumentRenderer(Document document, Rectangle[][] columns) {
super(document);
this.columns = columns;
}
@Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
if (overflowResult != null && overflowResult.getAreaBreak() != null && overflowResult.getAreaBreak().getType() != AreaBreakType.NEXT_AREA) {
curColumnLayoutId++;
curColumnLayoutId %= columns.length;
nextAreaNumber = 0;
}
if (nextAreaNumber % columns[curColumnLayoutId].length == 0) {
curColumnLayoutId++;
curColumnLayoutId %= columns.length;
super.updateCurrentArea(overflowResult);
}
return (currentArea = new RootLayoutArea(currentPageNumber, columns[curColumnLayoutId][nextAreaNumber++ % columns.length].clone()));
}
}
So it accepts an array of arrays of columns and in fact serves a more flexible purpose than you need. It will rotate over inner array in scope of one page and when the areas are exhausted will go to the next outer array item and so on. So in your case you can pass the columns for odd and even pages and the implementation will just rotate over those columns infinitely.
Example of how such code can be used:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFile));
Document doc = new Document(pdfDoc);
Rectangle[] columnsOdd = {new Rectangle(100, 100, 100, 500), new Rectangle(400, 100, 100, 500)};
Rectangle[] columnsEven = {new Rectangle(200, 100, 100, 500), new Rectangle(500, 100, 100, 500)};
doc.setRenderer(new VariableColumnDocumentRenderer(doc, new Rectangle[][] {columnsOdd, columnsEven}));