How to align bottom a PdfPCell spaning across more than one page? - itext

I'm using iTextSharp version 4.1.6.16. I have a PdfPTable with two columns (1 row).
There is a lot of content in right cell so it spans across whole page and reaches next page.
In the left cell I only want to put some small PdfPTable at its bottom. To do it I use cell.VerticalAlignment = Element.ALIGN_BOTTOM; on the left cell of topmost table. This works well if the row is not bigger than a page.
If the row reaches next page, then the content of the left cell is indeed aligned to bottom, but to bottom of first page. So the right cell's content still continues on the next page, but the left cell is empty there (I tested with background color that it spans to next page also).
I tried setting KeepTogether on the inner table but it doesn't have any effect. I also thought about doing it with CellEvent but I couldn't find so far how to position IElement (not only Image or text) absolutely in the cell.
Is it a bug or designed behavior, that the content is aligned to the bottom of the first page the cell occupies? Is there a workaround or some better way to put content at the bottom of the cell no matter where it ends?

I've finally solved the issue with following workaround.
I created a CellEvent that gets PdfPTable and shifts it upwards as much as its content takes:
class BottomFix : IPdfPCellEvent
{
private readonly PdfPTable _content;
public BottomFix(PdfPTable content)
{
_content = content;
}
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
_content?.WriteSelectedRows(0, -1, position.Left, position.Top + _content.TotalHeight, canvases[PdfPTable.BACKGROUNDCANVAS]);
}
}
Then to my original two-row table I added second row, and I add there a cell of height equal to 0, where instead of putting content into, I use this CellEvent. The original left cell is left empty, so the content of next row is pushed onto it and it looks as it was originally there. I don't have borders in this table so they are not an issue, but it can also be solved by setting border of this 1px cell to none.
var cell = new PdfPCell();
var table = new PdfPTable(1) { /* some content etc... */ };
cell.FixedHeight = 0;
cell.CellEvent = new BottomFix(table);
// such prepared cell goes to top level table, second row, left column

Related

itext 7 c# how to clip an existing pdf

let's say I have a bunch of pdf files that I want to migrate into a new pdf. BUT the new pdf file is a table-structured file. And the content of the pdf files should fit in the first cell of a two-column-table.
I am not sure if the approach of working with tables is correct. I am open to any other solutions. All I want is at the end some custom text at the top, followed by pdf content and a checkbox on the right side. (One per pdf content)
What I have so far:
`
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, PageSize.A4);
doc.SetMargins(0f, 0f, 18f, 18f);
PdfReader reader = new PdfReader(src);
PdfDocument srcDoc = new PdfDocument(reader);
Table table = new Table(new float[] { 2f, 1f });
PdfFormXObject imagePage = srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);
var image = new Image(imagePage);
Cell cell = new Cell().Add(image);
cell.SetHorizontalAlignment(HorizontalAlignment.LEFT);
cell.SetVerticalAlignment(VerticalAlignment.TOP);
table.AddCell(cell);
Table checkTable = new Table(2);
Cell cellCheck1 = new Cell();
cellCheck1.SetNextRenderer(new CheckboxCellRenderer(cellCheck1, "cb1", 0));
cellCheck1.SetHeight(50);
checkTable.AddCell(cellCheck1);
Cell cellCheck2 = new Cell();
cellCheck2.SetNextRenderer(new CheckboxCellRenderer(cellCheck2, "cb2", 1));
cellCheck2.SetHeight(50);
checkTable.AddCell(cellCheck2);
table.AddCell(checkTable);
doc.Add(table);
doc.Close();`
My Problem here is that the pdf content has still its margin. Which completely spoils the design. It is so frustrating, I appreciate any help.
You say
My Problem here is that the pdf content has still its margin. Which completely spoils the design.
PDFs (usually) don't know anything about margins. Thus, you have to detect the margins of the page to import first. You can do this by parsing the page content into an event listener that keeps track of the bounding box of drawing instructions, like the TextMarginFinder. Then you can reduce the source page to those dimensions. This can be done by means of the following method:
PdfPage restrictToText(PdfPage page)
{
TextMarginFinder finder = new TextMarginFinder();
new PdfCanvasProcessor(finder).ProcessPageContent(page);
Rectangle textRect = finder.GetTextRectangle();
page.SetMediaBox(textRect);
page.SetCropBox(textRect);
return page;
}
You apply this method in your code right before you copy the page as form XObject, i.e. you replace
PdfFormXObject imagePage = srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);
by
PdfFormXObject imagePage = restrictToText(srcDoc.GetFirstPage()).CopyAsFormXObject(pdfDoc);
This causes the Image this XObject will be embedded in to have the correct size. Unfortunately it will be somewhat mispositioned because the restricted page still has the same coordinate system as the original one, merely its crop box defines a smaller section than before. To fix this, one has to apply an offset, one has to subtract the coordinates of the lower left corner of the page crop box which has become the XObject bounding box. Thus, add after instantiating the Image the following code:
Rectangle bbox = imagePage.GetBBox().ToRectangle();
image.SetProperty(Property.LEFT, -bbox.GetLeft());
image.SetProperty(Property.BOTTOM, -bbox.GetBottom());
image.SetProperty(Property.POSITION, LayoutPosition.RELATIVE);
Now the restricted page is properly positioned in your table cell.
Beware: The TextMarginFinder (as its name indicates) determines the margins by text alone. Thus, if the page contains other contents, too, e.g. decorations like a logo, this logo is ignored and might eventually be cut out. If you want such decorations, too, in your overviews, you have to use a different margin finder class.

GXT 3 - Sorting causes grid to horizontally scroll

If I try to sort a column that's out of view (I need to scroll on the right to see it)
then the column sorts but the table scrolls back to the left, (and the column i sorted is out of view again)
One can try this in the Basic Gid of the GXT showcase:
Just make the width of the columns larger so that the horizontal scroller shows up and then try to scroll at the end of the table and sort.
How to fix this?
Thanks
This is how I solved it:
Override onDataChanged of GridView and set preventScrollToTopOnRefresh to true before sorting and then set it back to whatever it was. I wonder why this is not the default behaviour.
final GroupSummaryView<Row> view = new GroupSummaryView<Row>()
{
protected void onDataChanged(StoreDataChangeEvent<Row> se)
{
boolean b = preventScrollToTopOnRefresh;
preventScrollToTopOnRefresh = true;
super.onDataChanged(se);
preventScrollToTopOnRefresh = b;
}
};
_table.setView(view);
There is no built-in feature to prevent that behavior, so you have to write it yourself. You just need to store the scroll state before sorting and restore it after:
// save scroll state
final int scrollTop = getView().getScroller().getScrollTop();
final int scrollLeft = getView().getScroller().getScrollLeft();
// restore scroll state
getView().getScroller().setScrollTop(scrollTop);
getView().getScroller().setScrollLeft(scrollLeft);
#Darek Kay : Thank You very much. I was having an issue of scroll down while changing the option by selecting value from EditorTreeGrid. Your suggesion worked for me. Here's what I did and worked for me :
final int scrollTop = editorTreeGrid.getView().getScroller().getScrollTop();
final int scrollLeft = ditorTreeGrid.getView().getScroller().getScrollLeft();
editorTreeGrid.getView().refresh(true);
editorTreeGrid.getView().getScroller().setScrollTop(scrollTop);
editorTreeGrid.getView().getScroller().setScrollLeft(scrollLeft);

iText: How to set page events for footer accounting for page rotation?

When creating a PDF from scratch, I'm trying to adapt this code
http://itextpdf.com/examples/iia.php?id=104
specifically the onEndPage() function shown there, to set a footer using page events. The problem is in my application, some pages are Portrait and some Landscape, and I don't know how to implement a query in that function to determine the page rotation.
First, I got it working when all pages are portrait. Then I added some landscape pages and tried to modify it as shown below. I originally thought a quick-and-dirty solution would be simply to center the footer table by adding,
table.setHorizontalAlignment(Element.ALIGN_CENTER);
but this didn't seem to have any effect (on landscape pages, the table is always aligned left on the long-edge of the paper). Then I tried to do a better solution by querying the page rotation, and based on its results, set the table columns to the correct widths, using,
if (???==90)
table.setTotalWidth(new float[]{2.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,2.25f*K.PPI,}); // add to 6.5"
else
table.setTotalWidth(new float[]{3.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,3.25f*K.PPI,}); // add to 6.5"
but I'm not sure how to query the page rotation (as you can tell). Any help would be appreciated. My code is as follows.
class HeaderFooter extends PdfPageEventHelper {
/** The header text. */
String footerLeft, footerRight;
/** The template with the total number of pages. */
PdfTemplate total;
/** Flag indicating true for first page */
Boolean firstPageFlag=true;
...
public void onEndPage(PdfWriter writer, Document document) {
if (firstPageFlag==false) {
/** The footer font */
FontFactory.register("/home/appFonts/Arial_Narrow.ttf", "arial_narrow");
Font styleFooter = FontFactory.getFont("arial_narrow", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, K.TEXT_FOOTER_FONT_SIZE, Font.UNDEFINED, BaseColor.BLACK);
PdfPTable table = new PdfPTable(4);
try {
//table.setHorizontalAlignment(Element.ALIGN_CENTER); // doesn't seem to have an effect for landscape pages
if (how to query page rotation, or other method to evaluate whether page is landscape or portrait?)
table.setTotalWidth(new float[]{2.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,2.25f*K.PPI,}); // add to 6.5"
else
table.setTotalWidth(new float[]{3.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,3.25f*K.PPI,}); // add to 8.5"
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(14);
table.getDefaultCell().setBorder(Rectangle.TOP);
// col1, row1
table.addCell(new Phrase(footerLeft, styleFooter));
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
// col2, row1
table.addCell(new Phrase(String.format("page %d of", writer.getPageNumber()),styleFooter));
// col3, row1
PdfPCell cell = new PdfPCell(Image.getInstance(total));
cell.setBorder(Rectangle.TOP);
table.addCell(cell);
// col4, row1
table.addCell(new Phrase(footerRight, styleFooter));
table.writeSelectedRows(0,-1,document.left(),document.bottom()-0.35f*K.PPI,writer.getDirectContent());
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
} else
firstPageFlag=false;
}
...
}
Summarizing the solution developed in the comments to the question
As #Bruno initially stated correctly, the original attempt of the OP was
mixing page events (used when creating PDFs from scratch) with PdfReader (used when manipulating existing PDFs).
It would have been possible to actually use the OP's PdfReader centric test for landscape by switching to a two-pass architecture as used in the sample TwoPasses.java but the OP preferred a one-pass approach.
For this one-pass approach the original HeaderFooter page event listener was extended to also store the page dimension information upon page construction. This was done by overriding onStartPage() to retrieve the page size using
Rectangle pSize=document.getPageSize();
and storing its relevant information in a member variable of the listener. Eventually this information now is used in onEndPage() to determine how to set table columns.
In case of the OP's processes, landscape pages are created by means of rotation. Thus the relevant information in his case was whether
(pSize.getRotation()==90)
is true (landscape) or not (portrait). In general the whole Rectangle including both its rotation and its dimension values would have to be stored and eventually be used to determine the desired header and footer locations.

Smart GWT listgrid, how to expand and row span at the same time

So I have a Smart GWT ListGrid and I want to be able to make the rows expandable and at the same time do a row span on them. Expanding rows which are not merged (by doing row span) works fine, however if I have several rows that are merged the expand icon disappears. My code for expanding and row spanning is this:
lisgrid = new LisGrid()
{
#Override
protected Canvas getExpansionComponent(final ListGridRecord record)
{
// Add a nested grid to display the remaining requests for the tag
// (after the first N being shown be default).
VLayout layout = new VLayout(5);
layout.setPadding(1);
final ExtendedListGrid requestGrid = new ExtendedListGrid();
requestGrid.setWidth(500);
requestGrid.setHeight(224);
requestGrid.setCellHeight(22);
requestGrid.setDataSource(datasource);
requestGrid.fetchData();
layout.addMember(requestGrid);
return layout;
}
};
listGrid.setAllowRowSpanning(true);
listGrid.setCanExpandRecords(true);
listGrid.setMergeCols(new String[] { "requestSummaryTagId", "gca" });
I have overridden the getRowSpan method of the ListGrid to span a cell until the cell right under (same column index and next row index) it has the same value, and I also have a method, setMergeCols, that tells the grid which cells to span across rows. Here is what it looks like. As you can see, the first 2 cells at the bottom span 4 rows, but the expand symbol is missing, while for the rows above (which don't have any row span) the expand symbol is there. Any idea why?

GXT LayoutContainer with scrollbar reports a client height value which includes the area below the scrollbar

I have this code which sets up a "main" container into which other modules of the application will go.
LayoutContainer c = new LayoutContainer();
c.setScrollMode(Scroll.ALWAYS);
parentContainer.add(c, <...>);
Then later on, I have the following as an event handler
pContainer = c; // pContainer is actually a parameter, but it has c's value
pContainer.removeAll();
pContainer.setLayout(new FitLayout());
LayoutContainer wrapperContainer = new LayoutContainer();
wrapperContainer.setLayout(new BorderLayout());
wrapperContainer.setBorders(false);
pContainer.add(wrapperContainer);
LayoutContainer west = pWestContentContainer;
BorderLayoutData westLayoutData = new BorderLayoutData(LayoutRegion.WEST);
westLayoutData.setSize(pWidth);
westLayoutData.setSplit(true);
wrapperContainer.add(west, westLayoutData);
LayoutContainer center = new LayoutContainer();
wrapperContainer.add(center, new BorderLayoutData(LayoutRegion.CENTER));
pCallback.withSplitContainer(center);
pContainer.layout();
So in effect, the container called 'west' here will be where the module's UI gets displayed. That module UI then does a simple rowlayout with two children. The botton child has RowData(1, 1) so it fills up all the available space.
My problem is that the c (parent) container reports a height and width value which includes the value underneath the scrollbars. What I would like is that the scrollbars show all the space excluding their own space.
This is a screenshot showing what I mean:
alt text http://img265.imageshack.us/img265/9206/scrollbar.png
Try adding padding equivalent to the scroll bars size 14px? on the container where the Scroll.Always is being applied