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

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.

Related

Writing to absolute position while using direct content

I am creating a pdf-document. First I add a table and some Texts to the PdfWriter. Now I want to add a costum template (including images and texts): I have to get the direct Content, which ist a layer over the PdfWriter-layer:
over= PdfWriter.getDirectContent();
I want to set the template exactly after the content on PdfWriter-layer.
I can use
writer.getVerticalPosition(true)
for my calculation of y-Position on PdfWriter-layer.
This way I can add the costum template to the upper layer at that position. Now back to PdfWriter-layer how can I set the position of PdfWriter-layer after the tempalte on over-layer?!
Can somebody help?
Thanks in advance.
Mixing content added with document.add() and direct content us always a delicate operation. You need to know the height of the custom template and then add some white space that matches that height. However: what are you going to do if the content of the custom template doesn't match the page?
I would advise against your plans, and I would recommend another approach.
I am assuming that your custom template is a PdfTemplate object. In that case, you can wrap this object inside an Image object, and use document.add() to add the template.
See for instance: Changing Font on PDF Rotated Text
In this example, we create a PdfTemplate with a bar code and some text:
PdfTemplate template = canvas.createTemplate(rect.getWidth(), rect.getHeight() + 10);
ColumnText.showTextAligned(template, Element.ALIGN_LEFT,
new Phrase("DARK GRAY", regular), 0, rect.getHeight() + 2, 0);
barcode.placeBarcode(template, BaseColor.BLACK, BaseColor.BLACK);
We want to add this template to a document with document.add(), so we wrap the template inside an Image object:
Image image = Image.getInstance(template);
We can now add this image to the document:
document.add(image);
Afterwards, we can add extra content:
Paragraph p3 = new Paragraph("SMALL", regular);
p3.setAlignment(Element.ALIGN_CENTER);
document.add(p3);
That content is added under the template at the right position. You don't need to worry anymore about getting the Y position with getVerticalPosition() and you don't need to worry about setting the Y position after adding the template. If the template doesn't fit on the current page, it will automatically be moved to the next page.
Important: Maybe you are worried about the resolution of your template. You shouldn't be. If the template consists of vector data, wrapping that data inside an Image object won't change it into raster data. The vector data will be preserved. This is important in the bar code example, because you don't want the quality of your bar code to deteriorate by making it a raster image.

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.

Switching page orientation in ireport

I've read there's no way to handle mixed orientations natively using iReport, however reading the documentation I wonder if by using JRDefaultScriptlet's beforePageInit() it could be accomplished somehow. In my case there's a portrait front page, as many landscape pages as there's data to populate them, and a last frontal page.
On the other hand does anybody know:
If this is a feature to be supported in the near futureIf there's an alternative that does as requested and generates a jasper-compliant xml file
Thanks in advance.
So I decided to play around with iReport and see what options there where for this. Turns out it is sort of possible to pull off, with some effort and imagination. This is assuming your first page is in the Title Section, and your Last Page is in the summary section.
Create your report in landscape mode.
Under Report Properties in iReport set Title on New Page and Summary on New Page to true.
Assuming you are using a standard 8.5" X 11" Letter sized page with all the margins set to 20, set the height of the Title and Summary sections to 572.
Add your static text fields into the appropriate section.
Now for each static text field you need to set the Rotate property to Left (well it could actually be Right, the point is they all need to be the same.
Of course add the all the other fields you want into the appropriate bands for page header, data, etc.
Export your report.
Note: if you have any images that need to go into the Title or Summary Section your will need to rotate them appropriately outside of iReport and save it. Then set the rotated image as the image in the report. Unfortunately the image tag does not seem to have a rotate property, as that would make life to easy.
Also if you do not set the properties listed in step 2 you will not be able to set the height of the Title and Summary bands to the appropriate width. If you are using a different size paper and/or
margins the easy way to figure out the max size (which is what you will need) is to set the height of the band to a very large number. It will then popup and tell you it is to large, and what the max size actually is.
There is no support to mixed landscape and portrait subreport, in the future they will add an object call JasperBook or something like that where you can add different subreports of different orientations without problems, but for the moment you have simulate that by doing different reports and join them just before showing them.
I.E.
//Create the reports separately
InputStream report1 = (InputStream) getClass().getResourceAsStream("/com/app/jasper/reportPortrait.jasper");
InputStream report2 = (InputStream) getClass().getResourceAsStream("/com/app/jasper/reportLandscape.jasper");
InputStream report3 = (InputStream) getClass().getResourceAsStream("/com/app/jasper/reportPortrait.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, map, conn);
JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, map, conn);
JasperPrint jasperPrint3 = JasperFillManager.fillReport(report3, map, conn);
JRPdfExporter exp = new JRPdfExporter();
//Add the JasperPrint objects to an ArrayList
List list = new ArrayList();
list.add(jasperPrint);
list.add( jasperPrint2 );
list.add(jasperPrint3);
//And say to the exporter to join the list of reports.
exp.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, list);
exp.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exp.exportReport();
I'm doing that in my reports and it works. Good luck!

Pdf generation for dynamic contents

i am generating pdf report in my app,When the page contents exceeds over one page how to populate the contents,actually in this situation i tried to create one more new page by giving CGContextBeginPage();
but it is showing error like
: CGContextEndPage: Don't nest calls to this function -- the results will not be what you expect.
**** : CGContextBeginPage: Don't nest calls to this function -- the results will not be what you expect.
Can somebody tell me how to create pdf during this kind of situation.
You should have a variable that stores your current Y position as you are laying out content, incrementing this value by the height of the content (and any padding).
Each time you want to render some text or image etc, check that you have enough space on the page before rendering and if not end the current page and begin a new one. Check the space by looking at the current Y position, adding the content height to it and comparing to your page rect.
The errors you are getting are due to you nesting PDF page calls, the OS expects the following approach...
CGContextBeginPage
... render content for page 1
CGContentEndPage
CGContextBeginPage
... render content page 2
CGContentEndPage
However your code is most likely nesting these as follows...
CGContextBeginPage
... render content for page 1
CGContextBeginPage
... render content for page 2
CGContextEndPage
CGContextEndPage

BIRT auto size grid to match the table

I am trying to create a bill notice with payment stub underneath as a BIRT report.
My bill line items can be more than one. The report uses a gird layout with the bill line item information in a table.
I would like to
1 . get the table and the enclosing grid to expand as much as possible, if needed (without affecting the tear off stub position)
and if there is more line items it should spill over to the next page.
So, the first page layout is different from the next page(s) layout. Has anyone done something similar.
Please let me know how to acheive this in BIRT.
As long as you use relative sizing (percentages) instead of fixed dimensions in the "size" property (under the "General" properties) you should be fine.