Word to PDF conversion Checkbox problem

Hi,
I have a simple (Word) document with checkboxes (no ActiveX controls), but when I convert it to pdf, the checkbox doesn’t work properly. The pdf only shows a box but I can’t edit it (because it’s not a real checkbox) It is a simple square with a cross.
Could you tell me how I should create the checkbox in the Word Document, so that when I convert it to PDF it will still be a checkbox?

Hi Martin,

I’m afraid that currently only the content of the form fields is exported to PDF. Nevertheless, note that we do plans to support this in the future.

So for now, as a workaround can you check this solution that uses both GemBox.Document and GemBox.Pdf to achieve exporting of form fields:

Editable fields on PDF document from DOCX

Regards,
Mario

1 Like

Hi! Thanks for the fast solution!
But I have a problem with this solution (Editable fields on PDF document from DOCX - #2 by mario.gembox) I am using a .NET 7 app. And the line

var imageOptions = new GemBox.Document.ImageSaveOptions(GemBox.Document.ImageSaveFormat.Png);

It’s not working. The compiler indicate me:

“(“Image saving is supported only on WPF platforms (Windows OS).””

Do you have an alternative about this? To create the tempTextBox on the Save (tempTextBox.FormatDrawing().Save(imageStream, imageOptions))

Thanks!!

Hi Martin,

That method is used to get the field’s size, but yes, I’m afraid that currently saving to image format works only on Windows.

Anyway, perhaps you could use something like this:

private static readonly string PlaceholderImage = "any-image.png";

private static Dictionary<string, FormFieldData> ReplaceFieldsWithImages(DocumentModel document)
{
    var fieldsMapping = new Dictionary<string, FormFieldData>();

    foreach (var fieldData in document.Content.FormFieldsData)
    {
        var field = fieldData.Field;
        double width = 0, height = 0;

        switch (field.FieldType)
        {
            case FieldType.FormText:
                var format = field.ResultInlines.OfType<Run>().FirstOrDefault()?.CharacterFormat ?? field.CharacterFormat;
                height = format.Size + 2;
                width = 100;
                break;
            case FieldType.FormCheckBox:
                var checkboxData = (FormCheckBoxData)fieldData;
                width = height = checkboxData.Size ?? 10;
                break;
        }

        var picture = new Picture(document, PlaceholderImage, width, height);
        string guid = Guid.NewGuid().ToString();
        picture.Metadata.Title = guid;
        field.Content.Start.InsertRange(picture.Content);
        field.Content.Delete();

        fieldsMapping.Add(guid, fieldData);
    }

    return fieldsMapping;
}

As you may notice, I’m using a hardcoded value of 100 as a width for text fields.
Alternatively, you could try to get some rough estimation based on the length of the field.Content.ToString().

I hope this works for you.

Regards,
Mario

1 Like