Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Tue Nov 09, 2021 10:27 am

Hi team,
I'm trying to convert a .docx with some text place holder document to .pdf but the checkbox element gets lost in the process.
I'm using FreeSpire.Doc 7.11.0 and using these lines of code:
Code: Select all
if (splitPlaceHolder[0] == "cb")
                    {
                        _logger.LogInformation( $"{nameof(DocumentGeneratorService)} - process for check box");
                        var bolCheck = !string.IsNullOrEmpty(templateContentKeyValue.Value) && System.Convert.ToBoolean(templateContentKeyValue.Value);
                        var selections = document.FindAllString(key, true, true);

                        if (selections == null) continue;

                        foreach (var selection in selections)
                        {
                            _logger.LogInformation( $"{nameof(DocumentGeneratorService)} - process for check box of {templateContentKeyValue.Key} with value {bolCheck}");
                            var sdt = new StructureDocumentTagInline(document)
                            {
                                CharacterFormat = {FontSize = sizeCheckBox, TextColor = Color.FromArgb(argb)}
                            };
                            var range = selection.GetAsOneRange();
                            var index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                            range.OwnerParagraph.ChildObjects.Insert(index, sdt);
                            sdt.SDTProperties.SDTType = SdtType.CheckBox;
                            var scb = new SdtCheckBox();
                            sdt.SDTProperties.ControlProperties = scb;
                            var tr = new TextRange(document)
                            {
                                CharacterFormat =
                                {
                                    FontName = "MS Gothic",
                                    FontSize = sizeCheckBox,
                                    TextColor = Color.FromArgb(argb)
                                }
                            };
                            sdt.ChildObjects.Add(tr);
                            scb.Checked = bolCheck;
                            sdt.SDTProperties.Alias = "CheckoBox";
                            sdt.SDTProperties.Tag = "Checkbox";
                            range.OwnerParagraph.ChildObjects.Remove(range);
                        }
                    }
                    else
                        document.Replace(key, safeValue, true, true);


//////////////////////////////////////
 document.SaveToStream(outStream, FileFormat.PDF);


And we run local this code work well with the attached image
local.PNG


, but when deploying azure, this function does not display the correct checkbox.
cloud.PNG


Many thanks for any assistance that can help resolve this issue.

Haidoannam
 
Posts: 2
Joined: Mon Aug 16, 2021 8:17 am

Wed Nov 10, 2021 7:37 am

Hello,

Thank you for your inquiry.
When deploying the project to Microsoft Azure, you need to use the PS method to generate PDF. Please refer to the following code. I used the latest version of Free Spire.Doc 9.9.7 to do an initial test, but did not reproduce your issue. I suggest you first try the latest free version. If the issue still exists after trying, please provide the following information for further investigation. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.
1) Your sample word file.
2) Your complete test code.
3) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).
4) Your application type, such as Console app (. Net Framework 4.5).
Code: Select all
            var selections = document.FindAllString(key, true, true);

            foreach (var selection in selections)
            {
                var sdt = new StructureDocumentTagInline(document)
                {
                    CharacterFormat = { FontSize = sizeCheckBox, TextColor = Color.FromArgb(argb) }
                };
                var range = selection.GetAsOneRange();
                var index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                range.OwnerParagraph.ChildObjects.Insert(index, sdt);
                sdt.SDTProperties.SDTType = SdtType.CheckBox;
                var scb = new SdtCheckBox();
                sdt.SDTProperties.ControlProperties = scb;
                var tr = new TextRange(document)
                {
                    CharacterFormat =
                                {
                                    FontName = "MS Gothic",
                                    FontSize = sizeCheckBox,
                                    TextColor = Color.FromArgb(argb)
                                }
                };
                sdt.ChildObjects.Add(tr);
                scb.Checked = true;
                sdt.SDTProperties.Alias = "CheckoBox";
                sdt.SDTProperties.Tag = "Checkbox";
                range.OwnerParagraph.ChildObjects.Remove(range);
            }
            // PS method
            ToPdfParameterList ps = new ToPdfParameterList();
            ps.UsePSCoversion = true;         
            MemoryStream outStream = new MemoryStream();
            document.SaveToStream(outStream, ps);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1158
Joined: Wed Apr 07, 2021 2:50 am

Thu Nov 11, 2021 2:33 am

Thanks for your support.
I will try to apply your code and notify you when I get the result.
I want to add some info about our environment: We deploy docker container to Azure Cloud. I think we miss some fonts for symbols as "MS Gothic", "winding 2", ... so we cannot generate checkbox correctly
Here is code used to add front in dockerfile
Code: Select all
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
WORKDIR /app
# Update the sources.list for apt-get so it knows where to download the Microsoft Core fonts.
RUN echo "deb http://deb.debian.org/debian stretch contrib non-free" >> /etc/apt/sources.list \
    && echo "deb http://deb.debian.org/debian stretch-updates contrib non-free" >> /etc/apt/sources.list \
    && echo "deb http://security.debian.org/ stretch/updates contrib non-free" >> /etc/apt/sources.list

RUN apt-get update \
    && apt-get install -y --allow-unauthenticated \
        libc6-dev \
        libgdiplus \
        libx11-dev \
        xvfb \
        tzdata \
        ttf-mscorefonts-installer \
        wkhtmltopdf \
        libfontconfig1 \
     && rm -rf /var/lib/apt/lists/*
RUN fc-cache -f -v


Please help me to review if I have any mistakes.
Thanks

Haidoannam
 
Posts: 2
Joined: Mon Aug 16, 2021 8:17 am

Thu Nov 11, 2021 9:11 am

Thank you for your response.
Regarding installing fonts in docker, we recommend that you copy the local font files directly to the docker container. In addition, you can also use the embedded font method to set the font (put the font file under the program). This method is more convenient, the reference code is as follows. I am looking forward to your feedback.
Code: Select all
var selections = document.FindAllString(key, true, true);
foreach (var selection in selections)
{
    var sdt = new StructureDocumentTagInline(document)
    {
        CharacterFormat = { FontSize = sizeCheckBox, TextColor = Color.FromArgb(argb) }
    };
    var range = selection.GetAsOneRange();
    var index = range.OwnerParagraph.ChildObjects.IndexOf(range);
    range.OwnerParagraph.ChildObjects.Insert(index, sdt);
    sdt.SDTProperties.SDTType = SdtType.CheckBox;
    var scb = new SdtCheckBox();
    sdt.SDTProperties.ControlProperties = scb;
    var tr = new TextRange(document)
    {
        CharacterFormat =
                    {
                        FontName = "MS Gothic",
                        FontSize = sizeCheckBox,
                        TextColor = Color.FromArgb(argb)
                    }
    };
    sdt.ChildObjects.Add(tr);
    scb.Checked = true;
    sdt.SDTProperties.Alias = "CheckoBox";
    sdt.SDTProperties.Tag = "Checkbox";
    range.OwnerParagraph.ChildObjects.Remove(range);
}

//Embed private font from font file into the document
document.PrivateFontList. Add(new PrivateFontPath("MS Gothic", @"..\fontFiles\MS Gothic.ttc"));

// PS method
ToPdfParameterList ps = new ToPdfParameterList();
ps.UsePSCoversion = true;
MemoryStream outStream = new MemoryStream();
document.SaveToStream(outStream, ps);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1158
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc