Align a table to bottom of page using Itext - itext

I have a velocity template which has a nested table. I want to align the content of last cell
which has a table inside it to the bottom of the page. I am writing the xml in velocity template and processing the pdf using IText.

I'm not sure I've understood your question, but there is the method setExtendLastRow on PdfPTable object that make the last row on the table high as long as needed to fill the current page, until bottom margin. Example:
PdfPTable tabella = new PdfPTable(4);
PdfPCell cell = new PdfPCell(new Phrase("some text here"));
tabella.addCell(cell);
tabella.addCell(cell);
tabella.addCell(cell);
tabella.addCell(cell);
tabella.setExtendLastRow(true);
the table has only 4 cells that extends to the bottom of the page.

Related

Get current height of xwpf row, cell, paragraph

I am using XWPF to read .docx file which contain a table. In this table I get row then cell then I add a paragraph inside and in this paragraph I add multiple Run (with different style of font)
My question is (I know I don’t have posibility to get when word add new page or it is out of scope to me...?) But what I want achieve is get current height in my loop of the tableRow or tableCell is this possible? I mean something like this squelette code
XWPFTableRow r = ...;
For(...){
r.getCell(0).addNewParagraph(0).newRun().setText(...);
// at this step of programm I want height of each cell and each paragraph,
// is this possible? Because each iteration my table row height increment..
}
I tried tableRow.getHeight() to get row height but it get always 0, someone have idea to get row or cell or paragraph height?

Create Table in Itext Pdf in java

I am creating a table in PDF in java using itext. I want to place it in top right corner.
Here is my code snippet. When I execute the below code, table is aligned in the bottom right of pdf but i want it on Top right corner.
PdfPTable table = new PdfPTable(1);
table.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.setWidthPercentage(160 / 5.23f);
PdfPCell cell = new PdfPCell(new Phrase(" Date" , NORMAL));
cell.setBackgroundColor(BaseColor.BLACK);
cell.setBorderWidth(2f);
table.addCell(cell);
PdfPCell cellTwo = new PdfPCell(new Phrase("10/01/2015"));
cellTwo.setBorderWidth(2f);
table.addCell(cellTwo);
You have omitted the line that actually adds the table to the document.
Suppose that you have:
document.add(table);
In that case, iText will add it at the current position of the cursor. If no content was added yet, the table will be added at the top right. The top right is determined by the top margin and the right margin, but if those aren't 0, you may have the impression that the table isn't added at the top right.
You could also have:
PdfContentByte canvas = writer.getDirectContent();
table.writeSelectedRows(0, -1, document.right() - tablewidth, document.top(), canvas);
However, in that case you'd have to define the width of the table differently:
table.setTotalWidth(tableWidth);
I don't know how wide you want your table. You're using a rather odd formula to define the width percentage.
If this doesn't answer your question, please clarify by updating your question. Currently, it isn't entirely clear what you're doing. Your problem can't be reproduced. See the RightCornerTable example:
If my eyes don't fool me, the table is shown in the top-right corner when I use your code snippet and not in the bottom right as you claim...

RowSelectionModel shows all Column Header cells

I have created a NatTable with a RowSelectionModel and a RowSelectionProvider:
dataProvider = new ListDataProvider<>(rowData, columnAccessor);
bodyDataLayer = new DataLayer(dataProvider);
glazedListEventsLayer = new GlazedListsEventLayer<>(bodyDataLayer, rowData);
columnReorderLayer = new ColumnReorderLayer(glazedListEventsLayer);
columnHideShowLayer = new ColumnHideShowLayer(columnReorderLayer);
selectionLayer = new SelectionLayer(columnHideShowLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
selectionProvider = new RowSelectionProvider<>(selectionLayer, dataProvider, true);
selectionLayer.setSelectionModel(new RowSelectionModel<>(selectionLayer, dataProvider, idAccessor, false));
Basically, the table does what I want it to do. There is but one exception:
The table looks like this:
As intended, the table shows the row as selected (1). Also it highlights the actually selected cell (2), which is very nice. But, it renders the whole table column header as selected (3). I don't want that. I want either not highlight the header cells at all, or (even better:) I'd like only the column header cell of the cursor-cell (2) to be highlighted.
I thought that maybe there is a configuration label attached to the column header cells which results in the highlighting (so I could just change the style for this kind of label to get rid of the highlighting), but COLUMN_HEADER is the only configuration label, I can see when debugging.
So, I am bit stuck now. What causes the header cells to be highlighted and how can I change this behavior? Is it possible to highlight only the header of the cursor cell (as is done with the selected row's cursor cell (2))?
It is not the label you need to check, for selection it is the DisplayMode. So to not render the column header highlighted if you select a row you need to register the same style configuration for DisplayMode.SELECT as you register for DisplayMode.NORMAL.
If you only want to highlight the selection anchor in the column header you will need to register a custom IConfigLabelAccumulator to the DataLayer of the column header that is connected to the SelectionLayer and adds a custom label in case the cell in the column header is in the same column as the selection anchor.
For only highlighting the selection anchor, there is no default in NatTable itself. Although it should be easy to add. Feel free to create an enhancement ticket for this and even contribute. :)

automatic resize of the tab panel based on contents

Hello I have implemented something like
private VerticalPanel resultPanel;
private TabLayoutPanel tabPanel = new TabLayoutPanel(2.5, Unit.EM);
ResizeLayoutPanel resizePanel = new ResizeLayoutPanel();
myMethod(){
resizePanel.setWidth("100%");
resizePanel.setHeight("415px");
resizePanel.setWidget(tabPanel);
resultPanel = new VerticalPanel();
resultPanel.setWidth("100%");
resultPanel.add(resizePanel);
tabPanel.add(myVerticalPanel, tabHeader);
}
so the myVerticalPanel is the actual contents which is little large in height. As resizePanel height is set to 415px so rest of the contents are hidden.
I am looking for two things to achieve:
1. If there is a larger screen available then increase the tabPanel area to show more contents automatically
2. If screen is small then a scroll to appear to see rest of the contents
Please advise
You can try to replace the TabLayoutPanel with a HeaderPanel:
A panel that includes a header (top), footer (bottom), and content
(middle) area. The header and footer areas resize naturally. The
content area is allocated all of the remaining space between the
header and footer area.
Alternatively you can override the onResize() method your ResizeLayoutPanel calculate the height of your embedded content and set the appropriate height.
If you want scrolling functionality you have to embed your VerticalPanel in a ScrollPanel or use CSS to set the oferflow property.

How to add a table to a new page in an ItextSharp PdfStamper

I'm using ItextSharp to populate a Pdf template with data. The template is a single page form with a number of input fields. I can create a PdfStamper and populate the fields with no problem, however, I want to add a second page to the stamper and then add a dynamically created PdfPtable to that page. I can add a page using stamper.InsertPage(2,reader.GetpageSize()) but I can not work out how to add the table to the page.
Any help would be much appreciated.
Get your new page's PdfContentByte, then use PdfPTable.WriteSelectedRows(). There are 4 different overrides providing you with various options.
WriteSelectedRows just takes row [& column] numbers to draw, an X/Y location, and a PdfContentByte or array of same. This means it won't do any page breaking or width validation or what have you. It'll just draw what you ask it to draw, where you ask it to draw it. You have to do all the layout yourself.