How to create checkboxes on multiple pages in java iText PDF? - java

I currently have a method that looks like this for making checkboxes on my pdf:
private static void createCheckbox(PdfWriter writer, float lowerLeftX, float lowerLeftY, float upperRightX, float upperRightY, String fieldName, boolean startChecked) throws IOException, DocumentException {
RadioCheckField bt = new RadioCheckField(writer, new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY), fieldName, "Yes");
bt.setCheckType(RadioCheckField.TYPE_CHECK);
bt.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
bt.setBorderColor(Color.BLACK);
bt.setBackgroundColor(Color.WHITE);
bt.setChecked(startChecked);
bt.setOptions(RadioCheckField.READ_ONLY);
PdfFormField ck = bt.getCheckField();
writer.addAnnotation(ck);
}
This lets me send in lower left X and Y coords and upper right X and Y coords in order to make a box. This seems to work fine on the first page of a document where I create checkboxes. If I try making them on later pages, they just don't appear.
Example: I make a PDF form some text on page one, make a new page, add more info plus checkboxes, make a new page, add more info and checkboxes. That last page will not actually display checkboxes even if I use the same code for both pages with checkboxes.

Try putting a different name on each page. Field names are document-wide.

Related

How to make an image float above the text in OpenPDF to realize the seal function?

I found that OpenPDF's Image.class seems to have only 6 image positioning methods, and among them, there is only the option to add the image below the text (that is Image.UNDERLYING), but there is no option to let the image float above the text.
The source code of OpenPDF's Image class is as follows:
public static final int DEFAULT = 0;
public static final int RIGHT = 2;
public static final int LEFT = 0;
public static final int MIDDLE = 1;
public static final int TEXTWRAP = 4;
public static final int UNDERLYING = 8;
I want to implement the function of editing the contract. It needs to put a seal image on top of the text, and the position of the image needs to be dynamically set (because the length of the contract text may change). This may involve pagination and absolute positioning. I found that the PdfContentByte class seems to be able to add images to the text, but I don't know how it can dynamically position the images. It would be nice if there was a way to do this in sequence with Document#add like the Paragraph class, but I haven't found it yet.
The effect I want to achieve is roughly like this: Click to view the picture
How can I achieve my needs?
If you want to add something to an existing PDF with OpenPdf, you surely use a PdfStamper.
To add an image above the existing content of some page, simply retrieve the overcontent of the target page from the stamper and add the image to it.

how to put css styling for one specific checkbox in checkboxGroup of vaadin 8

I have a checkboxgroup which has multiple checkboxes. I have certain checkboxes which needs to look different, either by bold text or colored text ,which wont change irrespective of selection/unselection.
I have following code to build checkboxgroup. But I am not able to put style specific to one checkbox, because I dont have access to it. How can I do that
CheckBoxGroup<ReferenceScreenResultAnswer> answersOptionGroup = new CheckBoxGroup<>(question.getText());
List<ReferenceScreenResultAnswer> checkBoxItems = new ArrayList<>();
answersOptionGroup.setItems(checkBoxItems);
.......
// this is where i want to put CSS to specific checkbox/values
for (Answer answer : preSelectedAnswer)
{
ReferenceScreenResultAnswer rsra = new ReferenceScreenResultAnswer();
rsra.setAnswer(answer);
rsra.setReferenceScreenResultQuestion(rsrq);
answersOptionGroup.select(rsra);
}
I can do invidiual checkboxes like
CheckBox cb = new CheckBox();
cb.setCaptionAsHtml(true);
cb.setCaption("<b> hello </b> there");
But I am not able to access individual checkboxes from CheckBoxGroup. Any idea how to access them
i found the answer:
// css style the pre selected answer, so they look different irrespective
// of their selection
answersOptionGroup.setItemCaptionGenerator(new ItemCaptionGenerator<ReferenceScreenResultAnswer>()
{
#Override
public String apply(ReferenceScreenResultAnswer item)
{
if (preSelectedAnswer.contains(item.getAnswer()))
return "<strong>" + item.getAnswer().toString() + "</strong>";
else
return item.getAnswer().toString();
}
});

How can I move the whole layout to adapt to pre-printed form on different printers

We have US Healthcare Medical Billing Product developed using Java, Hibernate, Spring and Jasper Report 5.6. that are printing with CMS 1500 and UB04 Form.
We want to print the values in the pre printed form i.e user will keep the this pre printed form in the printer and from the application we need to print the values in the boxes.
So we attached the image in the Jasper Report and put the text boxes in each of the boxes. It is printing correctly, but if the user change the printer, then alignment is becomes a problem. As a alternate dirty option, we took the copy and make the alignment for that printer, so now for every printer we have separate jasper report file even though the values that are printed are the same.
My client is asking to give them a option to set the X and Y value in separate form and then use these values to print correctly.
So the question is can we do this in jasper reports?
The easiest way to move all report elements is to modify the report margins
Load the jrxml in to the JasperDesign object and switch margins as desired. The minimum you can move in x is the original report margin, the maximum depends on your report (naturally the columWidth can not become 0, but has no really sense checking this better to define a max)
Example:
JasperDesign design = JRXmlLoader.load("YourReport.jrxml");
moveDesign(design,x,y);
JasperReport report = JasperCompileManager.compileReport(design);
private void moveDesign(JasperDesign design, int x, int y) {
int maxX = 100; //I define it so that elements is not out of report
int maxY = 100;
int pageWidth = design.getPageWidth();
int intitalLeftMargin = design.getLeftMargin();
int intitalRightMargin = design.getRightMargin();
int intitalTopMargin= design.getTopMargin();
//Check that not less then 0 and not more then my max
int newLeftMargin = Math.min(Math.max(intitalLeftMargin+x,0),maxX);
int newTopMargin = Math.min(Math.max(intitalTopMargin+y,0),maxY);
//set our new margins
int newColumWidth = pageWidth - newLeftMargin - intitalRightMargin;
design.setLeftMargin(newLeftMargin);
design.setTopMargin(newTopMargin);
design.setColumnWidth(newColumWidth);
}
The down-side of this is that you need to recompile your report (this will take a couple of ms).
If execution speed is of fundamental importance another solution (more complex but probably faster) is to move all elements in every page in the JasperPrint
I will leave the complete code to OP but it will be similar to this
List<JRPrintPage> pages = jasperPrint.getPages();
for (JRPrintPage jrPrintPage : pages) {
List<JRPrintElement> elements = jrPrintPage.getElements();
for (JRPrintElement jjpe : elements) {
jjpe.setX(newX);
jjpe.setY(newX);
}
}

PDFBox - how to create table of contents

Is there a way to create a table of contents using Java PDFBox library?
The table of contents should be clickable (jump to the right page)
Thanks.
There's no simple method for doing this, but here's an approach. I haven't figured out how to attach links directly to text, so my approach means you have to draw the annotations as rectangles and the text separately. It's a bit rough around the edges, but it works.
// there are other types of destinations, choose what is appropriate
PDPageXYZDestination dest = new PDPageXYZDestination();
// the indexing is odd here. if you are doing this on the first page of the pdf
// that page is -1, the next is 0, the next is 1 and so on. odd.
dest.setPageNumber(3);
dest.setLeft(0);
dest.setTop(0); // link to top of page, this is the XYZ part
PDActionGoTo action = new PDActionGoTo();
action.setDestination(dest);
PDAnnotationLink link = new PDAnnotationLink();
link.setAction(action);
link.setDestination(dest);
PDRectangle rect = new PDRectangle();
// just making these x,y coords up for sample
rect.setLowerLeftX(72);
rect.setLowerLeftY(600);
rect.setUpperRightX(144);
rect.setUpperRightY(620);
PDPage page = // however you are getting your table of contents page, eg new PDPage() or doc.getDocumentCatalog().getAllPages().get(0)
page.getAnnotations().add(link);
PDPageContentStream stream = new PDPageContentStream(doc, page, true, true);
stream.beginText();
stream.setTextTranslation(85, 600); // made these up, have to test to see if padding is correct
stream.drawString("Page 1");
stream.endText();
stream.close();
Phew! That's a lotta code. That should get you on your way. You can make the rectangle the same color as your document background if you want it to look like they are just clicking a link, but that requires more experimentation.

iText 5 header and footer

how I can add in my PDF page the header and the footer?
I wanna a table with 3 column in header and other table, 3 column in the footer.
My page could be A3 or A4, and landscape or portrait.
Can anyone help me? I can not found on internet good examples.
Thanks!
Tommaso
Create a class MyPageEventListener that extends PdfPageEventHelper
Add a page event listener to the PdfWriter object
In the onEndPage method of MyPageEventListener class, put the code
for header/footer
Example:
public class MyPageEventListener extends PdfPageEventHelper {
. . .
#Override
public void onEndPage(PdfWriter writer, Document document) {
//code skeleton to write page header
PdfPTable tbl = new PdfPTable(3);
tbl.addCell("1st cell");
tbl.addCell("2nd cell");
tbl.addCell("3rd cell");
float x = document.leftMargin();
float hei = getMyHeaderHeight(); //custom method that return header's height
//align bottom between page edge and page margin
float y = document.top() + hei;
//write the table
tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
}
}
to register the listener simply do
writer.setPageEvent(new MyPageEventListener());
The easiest way to do this is first generate the contents of your entire PDF in memory, then once all the pages have been created you need to open the in-memory PDF in the pdfStamper and iterate through all the pages adding in the header and footer objects are the correct coordinates.
If you do a quick google search of adding page numbers in itextPDF you will find a number of examples that you can quickly adapt for your needs.
The key is that it is done after you create the pdf, not before.

Categories

Resources