how to draw checkbox in destination?

1 Answer 27 Views
PdfProcessing
David
Top achievements
Rank 1
Iron
Iron
Iron
David asked on 29 Jul 2021, 03:56 PM

I need to put a series of aligned checkboxes in a specific location on a pdf.

We are able to fill in all the text fields and I have a destination added to the nameddestinations and I can draw text into it with the fixedcontenteditor, but I can not figure out how to put checkboxes on the form.

 

  document.NamedDestinations.Add("services", new Location() { Page = document.Pages[0], Left = 500, Top = 400 });


                NamedDestination destination = null;
                document.NamedDestinations.TryGetValue("services", out destination);

                if (destination != null)
                {
                    var loc = destination.Destination as Location;
                    var editor = new FixedContentEditor(destination.Destination.Page);

                    editor.Position.Translate(loc.Left.Value, loc.Top.Value);

                    CheckBoxField checkBoxField = new CheckBoxField("SampleCheckBox");
                    checkBoxField.IsChecked = true;

                    
                    editor.DrawText("Found the text box place!");
                }

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 30 Jul 2021, 12:13 PM

Hi David,

The CheckBoxField is only a logical representation and it does not draw any content. You need to add the appropriate widget and set its rectangle as well. Here is an example: 

if (destination != null)
{
    var loc = destination.Destination as Location;
    var editor = new FixedContentEditor(destination.Destination.Page);

    CheckBoxField checkBoxField = new CheckBoxField("SampleCheckBox");
    checkBoxField.IsChecked = true;

    TwoStatesButtonWidget widget = checkBoxField.Widgets.AddWidget();
    widget.Rect = new Rect(loc.Left.Value, loc.Top.Value, 20, 20);
    widget.RecalculateContent();

    document.AcroForm.FormFields.Add(checkBoxField);
    document.Pages[0].Annotations.Add(widget);

}

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

David
Top achievements
Rank 1
Iron
Iron
Iron
commented on 30 Jul 2021, 08:18 PM

ok that is working great. I made a routine that draws checkmarks and text after it. But the text fontsize is too large. Is there a way to control the font size?

void CreateCheckBoxeWLabelAtDestination(RadFixedDocument document, NamedDestination destination, bool _checked, int locOffSetX, int locOffSetY, string labelText)
{
var loc = destination.Destination as Location;
var editor = new FixedContentEditor(destination.Destination.Page);

CheckBoxField checkBoxField = new CheckBoxField(labelText);
checkBoxField.IsChecked = _checked;

TwoStatesButtonWidget widget = checkBoxField.Widgets.AddWidget();
widget.Rect = new Rect(loc.Left.Value + locOffSetX, loc.Top.Value + locOffSetY, 10, 10);
widget.RecalculateContent();

document.AcroForm.FormFields.Add(checkBoxField);
document.Pages[0].Annotations.Add(widget);

editor.Position.Translate(loc.Left.Value + locOffSetX + 20, loc.Top.Value + locOffSetY);
editor.DrawText(labelText);
}

I call this method like this:

document.NamedDestinations.Add("services", new Location() { Page = document.Pages[0], Left = 400, Top = 400 });


NamedDestination destination = null;
document.NamedDestinations.TryGetValue("services", out destination);

if (destination != null)
{
CreateCheckBoxeWLabelAtDestination(document, destination, false, 115, 0, "CAFF");
CreateCheckBoxeWLabelAtDestination(document, destination, true, 180, 0, "NON-CAFF");

CreateCheckBoxeWLabelAtDestination(document, destination, true, 0, 50, "WEAPON**");
CreateCheckBoxeWLabelAtDestination(document, destination, false, 150, 50, "MILITARY ISSUED CLOTHING");

CreateCheckBoxeWLabelAtDestination(document, destination, false, 0, 70, "BILLETING");
CreateCheckBoxeWLabelAtDestination(document, destination, true, 150, 70, "MILITARY ISSUED EQUIPMENT");

CreateCheckBoxeWLabelAtDestination(document, destination, false, 0, 90, "CAC");
CreateCheckBoxeWLabelAtDestination(document, destination, false, 150, 90, "MWR FACILITES");

CreateCheckBoxeWLabelAtDestination(document, destination, false, 0, 110,"LOCAL ACCESS BADGE");
CreateCheckBoxeWLabelAtDestination(document, destination, false, 150, 110,"MILITARY EXCHANGE");

CreateCheckBoxeWLabelAtDestination(document, destination, false, 0, 130,"DINING FACILITY(DFAC)");
CreateCheckBoxeWLabelAtDestination(document, destination, false, 150, 130, "COMMISSARY");

}
Dimitar
Telerik team
commented on 02 Aug 2021, 06:49 AM

Hi David,

You can use the TextProperties for this: 

var editor = new FixedContentEditor(destination.Destination.Page);
editor.TextProperties.FontSize = 10;

More information is available here: Text and Graphic Properties.

Tags
PdfProcessing
Asked by
David
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or