Is there a way to specify different margins for my first and last pages in iText?
I have a large header to be placed in first page and a large footer to be present in the last page.
The trouble is when I position the footer absolutely the content of the page might overlap it because of the absolute positioning.
yes.
before creating last page, set margins and then call newpage() method of Document object.
Related
I am using iText 2.1.7
I write a pdf document for which the page size and contant can differ each time. What i want to achieve is a table at the very last page and at the bottom of that page. I am aware of the method 'writeSelectedRows', but with this it could happen that i overwrite text on the page, because i dont know if there is space for my table.
So in conclusion:
If i have reached the last page i want to add my table at the bottom of the page. But if the table does not fit, i want a new page and on this page i want to add the table at the bottom.
I could not find a solution so far.
Use this http://developers.itextpdf.com/de/node/1910 to calculate the height of a table and then check the available space on a page with PdfWriter.GetVerticalPostition(). You might want to consider your Document.BottomMargin or some other offset in your space calculation. (v4.1.6)
Check out this link:
http://developers.itextpdf.com/content/best-itext-questions-stackoverview/general-questions-about-itext/itext7-how-find-out-current-cursor-position-page
I suppose that once you know the current cursor position on this page, you can write a simple if-statement to add a new page, or not.
Kind regards,
Joris
I have a pdf document created by Google Chrome. When parsing the text via PDFBox (Java), I found that there was a hidden block of text straddled between the pages. Although the rendering mode was "FILL", I found that the element was off the page. Problem solved.
Now, I find that another similar element also appears off the page, but the coordinates to not tell this. It is within the visible margin of the second page that it straddles. It has y2 = 31.195312 and max height of 29.894833 (Font size = 36). Calculated y1 is about 1, still on the page.
The text positions obj shows some interesting internal properties, but they are not public vars. All I have is this TextPosition object (https://pdfbox.apache.org/docs/1.8.10/javadocs/org/apache/pdfbox/util/TextPosition.html) and the surrounding context.
I can reproduce the issue, but it requires my particular document. Could be attempted with page-break-inside tests, but I haven't found a simple test for it. I'm looking for some kind of margin, but so far all the boxes from within this.getCurrentPage() show just the plain page height, and no start position. Another possibility is that there is another way of looking for the coordinates than firstTextPos.getY() and firstTextPos.getHeight().
PDF in Mac Preview:
The text is selected between pages and is listed on the second page. In the case where it is listed on the first page, I am able to handle the issue as described above.
TextPosition object private vars:
I am new to itext in java. I have an existing pdf of 2 pages. I need to add 2 new pages to it and then add an image to the 3rd page and then add four small rectangles and some text in the 4th page. On searching I got codes for adding new pages and codes for adding images to the existing pdf separately. Column text was used to add text to a new page, I searched for adding image to column text but I cannot find it. getUnderContent helped me to add image at the bottom of 2nd page. I want the image to be added in 3rd page. And the 4th page gets more complicated. I add the rectangle and text using PdfContentByte. This should be done by creating a new page. Any ideas?
Based on your comments, I assume that you are using PdfStamper and that you're able to add an image to an existing page. This is, for instance, done using getUnderContent() and its addImage() method. Now you need to add an extra page.
In PdfStamper, you can use the insertPage() method to achieve this:
stamper.insertPage(pageNum, rectangle);
In this line pageNum is an int value indication the page number where you want to insert the new page, and rectangle is the size of the page. For instance:
stamper.insertPage(reader.getNumberOfPages() + 1, reader.getPageSize(1));
Once you have inserted the page, you can get the "over" or "under" content, and add an image to that PdfContentByte using the addImage() method. You might want to replace reader.getPageSize(1) with a Rectangle object that corresponds with the dimensions of the image.
I have a problem with my Android app: I need to take some PDF with acrofields, and add some content, but not in another page, or in a page in the middle, just under the content of the existing PDF. And then, when I finished, I have to attach another PDF file with acrofield to this.
For now, I have 3 pages: the first PDF with acrofield, the content that I want to add, and the second PDF with acrofield.
Is there a way to compact all in less pages? Because I have a lot of blank space between one page and the other, and I need to do this.
I am generating a PDF comprised of multiple tables and I want to know if there is a certain way by which I can get to know if the table size will exceed the PDF page size or not. I am using the information to decide wether I will generate the PDF in portrait or landscape mode. Would it be possible to get this size?
know if the table size will exceed the PDF page size
The table size is determined from your XHTML Table Content (rows, columns, headers, footers, etc), plus your CSS doc(s) (width, height and border properties).
The rendering engine (ITextRenderer) only knows your table size, and whether it will fit within a page after it's applied CSS, as part of the process of converting XHTML to rendered output.
So, if you were to query ITextRenderer for this information, you would need to:
Create an ITextRenderer instance, pass your Document in via renderer.setDocument(), and then apply the CSS via renderer.layout(), causing all of the layout calculations to occur based upon the original page orientation
Query somehow to determine if the table fits on the page
If it doesn't, switch between portrait/landscape. But this means changing to a new CSS, setup for a different page shape:
Pick a new CSS, appropriate for the new page orientation and set it within your document
Create a new ITextRenderer instance, pass your Document in via renderer.setDocument(), and then apply the CSS via renderer.layout(), causing all of the layout calculations to occur based upon the new page orientation.
Create PDF output via renderer.createPDF().
Problems:
This is inefficient and inelegant processing.
This is doing things the wrong way around. Controlling page layout should be part of the design process, and should be reflected in (a) the content generated (XHTML) and (b) the formatting (CSS). If that's done correctly, there should be no need to do render-time magic to reformat the entire page.
At step (2), there's no simple query interface that will simply tell you if your table fits within a page. Instead, you have to query the formatted output blocks, and try to work out for yourself where your table is and whether it fits in a page. E.g.:
BlockBox root = renderer.getRootBox();
List pageList = root.getLayer().getPages()
PageBox page = (PageBox)pageList.get(2);
List childBoxList = page.getChildren();
Box childBox = (Box)childBoxList.get(0);
// etc... until you locate your table
At step (4), ITextRenderer expects your CSS to be linked from within your XHTML document. That means you first need to modify your XHTML source, then you need to resubmit it to ITextRenderer.
Suggested Alternative:
Do a wireframe/sketch design with 2 scenarios - one for portrait & one for landscape
Determine the size of elements/rows/columns under the 2 scenarios
Determine the condition logic under which you would use each scenario, portrait v landscape. E.g. if I the number of rows is X and the number of columns is Y - then use landscape, ...
Now create the two CSS files
Now, when you generate your XHTML, use the condition logic to link to the correct CSS