Create Table in Itext Pdf in java - itext

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...

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?

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. :)

Is it possible to "shrink" a PdfPtable?

I am currently working with Itextsharp and I have some trouble with PDfPtables.
Sometimes they get too big for a page and, when added to a document, are broken up on multiple pages.
Sadly ths rational behviour is not acceptable for some of my superiors - they keep insisting that the table shall be "shrunk" to a page. Is there a way to achieve this? There are some tantalizing hints that i could be possible - but hints are all that i have.
What is the alternative? Perhaps I could delete the fat table from the document and build the table again with smaller Fonts and Cells, but that would be very cumberosme - I would prefer to "zoom out", in lack of a better word.
My current code:
Dim test As PdfContentByte = mywriter.DirectContent
Dim templ = test.CreateTemplate(mywriter.PageSize.Width, mywriter.PageSize.Height)
Table.WriteSelectedRows(0, Table.Rows.Count - 1, 0.0F, mywriter.PageSize.Height, templ)
Dim myimage = Image.GetInstance(templ)
' myimage.ScaleAbsolute(mywriter.PageSize.Width, mywriter.PageSize.Height)
would scaleabsolute be necessary?
myimage.SetAbsolutePosition(0, 0)
test.AddImage(myimage)
This code puts something one the page, but it has the height of the page and the width is about a quarter of the page - wi will try to find the bug...
Create the table and define a 'total width'. As soon as iText knows the width of the table, you can calculate the height of all the rows. Once you know the height, you can check:
Does the table fit the page? Just add it as is. Maybe using WriteSelectedRows if you don't want to take any page margins into account.
Isn't there enough space on the page? Add the table to a PdfTemplate (there's more than one way to do this), wrap the PdfTemplate inside an Image. Scale the image, and add it to the document.

Align a table to bottom of page using 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.

Eclipse layout for table and two buttons

I am trying to lay out a form for an Eclipse editor and running into a few problems.
The idea is to have a page which contains 2 sections - the left hand section contains a table and two buttons. The table should line up with the top of the section and expand right to the bottom. I want the buttons to sit to the right of the table in the section with each button under the other and for them to align with the top of the table.
Does anyone know what settings for GridLayouts I need to make this work? I've tried every combination I can think of with no luck.
The closest I can get ends up with the second button at the bottom of the page.
Here is an excerpt of my code so far:-
Section section = toolkit.createSection(sashForm, ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED | ExpandableComposite.NO_TITLE_FOCUS_BOX);
section.setText("All Items");
Composite client = toolkit.createComposite(section);
section.setClient(client);
client.setLayout(new GridLayout(2, false));
Table table = toolkit.createTable(client, SWT.NULL);
table.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
TableViewer viewer = new TableViewer(table);
Button addButton = toolkit.createButton(client, "Add", SWT.PUSH);
addButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
Button removeButton = toolkit.createButton(client, "Remove", SWT.PUSH);
removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
Have a look at SWT Layout Tutorials it has a good section on GridLayout.
You will want to use the GridData.verticalSpan for the table to get it to cover two rows. That should put the two buttons on the right. But then the buttons will take the same space as the table which is probably not what you want, so you may need to give vertical size hint to the table.