All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.librepdf.openpdf.examples.radiobutton.RadioButtonPageBreak Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package org.librepdf.openpdf.examples.radiobutton;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RadioCheckField;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class RadioButtonPageBreak {

    public static void main(String... args) throws FileNotFoundException, DocumentException {
        Document document = new Document(PageSize.A4);
        FileOutputStream outputStream = new FileOutputStream("RadioButtonPageBreak.pdf");
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        almostFillThePage(document);
        addTableWithRadioButtons(document, writer);
        document.close();
    }

    private static void almostFillThePage(Document document) throws DocumentException {
        document.add(new Paragraph(
                "This example shows: A table with a radio group. Each radio button is in a different row. If the"
                        + " table is split due a page break, then the radio button on page 1will be drawn in page 2,"
                        + " but in the corresponding page position, as if it was drawn in page 1."));
        for (int i = 0; i < 36; i++) {
            document.add(Chunk.NEWLINE);
        }
    }

    private static void addTableWithRadioButtons(Document document, PdfWriter writer) throws DocumentException {
        PdfPTable table = new PdfPTable(3);
        // Add some rows before the radio buttons, so we get the break in the desired position
        addRow(table, "Column 1", "Column 2", "Column 3");
        addRow(table, "Radio buttons", "coming", "soon");

        // Radio button rows
        PdfFormField radioGroup = PdfFormField.createRadioButton(writer, true);
        radioGroup.setFieldName("radio");
        String[] values = {"Selection One", "Selection Two"};
        for (String value : values) {
            PdfPCell radioCell = new PdfPCell();
            radioCell.setCellEvent(new MyCellEvent(radioGroup, value, writer));
            table.addCell(radioCell);
            table.addCell(value);
            table.completeRow();
        }

        // Add table and radioGroup
        document.add(table);
        writer.addAnnotation(radioGroup); // Adding radioGroup after adding the table
    }

    private static void addRow(PdfPTable table, String... cells) {
        Arrays.stream(cells).forEach(table::addCell);
        table.completeRow();
    }

    private static class MyCellEvent implements PdfPCellEvent {

        private final PdfFormField radioGroup;
        private final String value;
        private final PdfWriter writer;

        public MyCellEvent(PdfFormField radioGroup, String value, PdfWriter writer) {
            this.radioGroup = radioGroup;
            this.value = value;
            this.writer = writer;
        }

        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            RadioCheckField radio = new RadioCheckField(writer, position, null, value);
            radio.setBorderColor(GrayColor.GRAYBLACK);
            radio.setCheckType(RadioCheckField.TYPE_CIRCLE);
            try {
                radioGroup.addKid(radio.getRadioField());
            } catch (final IOException | DocumentException e) {
                throw new ExceptionConverter(e);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy