为有中文需求的客户提供多渠道中文技术支持.

Tue Nov 15, 2022 12:12 pm

我尝试通过.net代码向PDF文件的域中填写指定的信息,但是在填写保存后,文本域中的内容就变成了乱码,请问怎么解决这一问题,还是我的赋值手段有问题?

//获取文档的所有域
PdfFormWidget form = pdf.Form as PdfFormWidget;

for (int i = 0; i < form.FieldsWidget.Count; i++)
{
string fieldName = form.FieldsWidget[i].Name;

//填充文本域
PdfTextBoxFieldWidget textboxField = form.FieldsWidget[i] as PdfTextBoxFieldWidget;

if (textboxField.Text.Trim() != fieldNames[fieldName].ToString().Trim())
{
textboxField.Text = fieldNames[fieldName].ToString();
}
}



PdfFormWidget form = pdf.Form as PdfFormWidget;

for (int i = 0; i < form.FieldsWidget.Count; i++)
{
string fieldName = form.FieldsWidget[i].Name;

PdfComboBoxWidgetFieldWidget comboBoxField = form.FieldsWidget[i] as PdfComboBoxWidgetFieldWidget;


if (comboBoxField.SelectedIndex[0] != fieldNames[fieldName].ToInt())
{
comboBoxField.SelectedIndex = new int[] { fieldNames[fieldName].ToInt() };
}
}

LoveIceCream
 
Posts: 5
Joined: Tue Nov 15, 2022 11:58 am

Wed Nov 16, 2022 7:26 am

您好,

感谢您的留言。
根据您提供的信息,我无法对您提出的问题进行准确的调查。为了帮助我们重现您的问题并为您提供解决方案。请提供以下信息。感谢您的帮助。
1) 您的pdf文档。
2) 您完整的测试代码,对于您代码中的“fieldNames[fieldName]”我不知道它代表的含义。
3) 您的项目类型,如控制台项目(.Netframework4.8)。
4) 您的系统环境,如win10; 电脑的区域语言设置,如中国-中文。

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 944
Joined: Tue Mar 08, 2022 2:02 am

Thu Nov 24, 2022 8:13 am

你好,我这边做了一些改动,目前通过代码向PDF文件域中填入中文后,打开PDF文件直接显示的是乱码,但是点击域时乱码就会变成正常的中文,当鼠标离开域时又会变成乱码,我想知道是什么原因造成的这一现象,我使用的系统是win10,使用的是c#的.net的4.7.2。谢谢

LoveIceCream
 
Posts: 5
Joined: Tue Nov 15, 2022 11:58 am

Thu Nov 24, 2022 8:22 am

Abel.He wrote:您好,

感谢您的留言。
根据您提供的信息,我无法对您提出的问题进行准确的调查。为了帮助我们重现您的问题并为您提供解决方案。请提供以下信息。感谢您的帮助。
1) 您的pdf文档。
2) 您完整的测试代码,对于您代码中的“fieldNames[fieldName]”我不知道它代表的含义。
3) 您的项目类型,如控制台项目(.Netframework4.8)。
4) 您的系统环境,如win10; 电脑的区域语言设置,如中国-中文。

Sincerely
Abel
E-iceblue support team

LoveIceCream
 
Posts: 5
Joined: Tue Nov 15, 2022 11:58 am

Thu Nov 24, 2022 9:50 am

您好,

感谢您的反馈。
您需要再给Textbox绘制文本前指定字体,如下面的代码片段:
Code: Select all
 //为textbox填入文本设置字体
                    textBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);
                    textBoxField.Text = "你好!";

我把完整的测试代码和测试文档放在下面供您参考:
Code: Select all
PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile(@"../../data/EmbedGridInCell.pdf");

            //获取文档的所有域
            PdfFormWidget form = pdf.Form as PdfFormWidget;

            //遍历所有域名
            for (int i = 0; i < form.FieldsWidget.Count; i++)
            {
                PdfField field = form.FieldsWidget.List[i] as PdfField;
                //如果为TextBox域名
                if (field is PdfTextBoxFieldWidget)
                {               
                    //填充文本域
                    PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;

                    //为textbox填入文本设置字体
                    textBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);
                    textBoxField.Text = "你好!";
                }
            }
         
            pdf.SaveToFile(@"../../output/result_free.pdf", FileFormat.PDF);




Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 944
Joined: Tue Mar 08, 2022 2:02 am

Thu Nov 24, 2022 10:54 am

Abel.He wrote:您好,

感谢您的反馈。
您需要再给Textbox绘制文本前指定字体,如下面的代码片段:
Code: Select all
 //为textbox填入文本设置字体
                    textBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);
                    textBoxField.Text = "你好!";

我把完整的测试代码和测试文档放在下面供您参考:
Code: Select all
PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile(@"../../data/EmbedGridInCell.pdf");

            //获取文档的所有域
            PdfFormWidget form = pdf.Form as PdfFormWidget;

            //遍历所有域名
            for (int i = 0; i < form.FieldsWidget.Count; i++)
            {
                PdfField field = form.FieldsWidget.List[i] as PdfField;
                //如果为TextBox域名
                if (field is PdfTextBoxFieldWidget)
                {               
                    //填充文本域
                    PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;

                    //为textbox填入文本设置字体
                    textBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);
                    textBoxField.Text = "你好!";
                }
            }
         
            pdf.SaveToFile(@"../../output/result_free.pdf", FileFormat.PDF);




Sincerely
Abel
E-iceblue support team

根据您的提示,目前的情况比原来的有所改善但仍存在两个问题
1、下拉菜单域认识原本的情况并没有改变
2、设置字体后中文字符串中的数字或符号会缺失掉
请问这两个问题有相关的解决方法吗?

LoveIceCream
 
Posts: 5
Joined: Tue Nov 15, 2022 11:58 am

Fri Nov 25, 2022 9:54 am

您好,

感谢您的反馈。
1) 首先你要确保您在代码中为文本框指定的字体再您本地电脑上已将安装了。
2) 对于下拉菜单域同样可以使用代码指定字体:

Code: Select all
  PdfComboBoxWidgetFieldWidget comboBoxField = field as PdfComboBoxWidgetFieldWidget;

                    //为textbox填入文本设置字体
                     comboBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);

3) 我这边模拟的文档和代码测试,没有重现您提到的文本框中数字、符号乱码的情况。我把我的测试代码和测试文档放在下面供您参考。
Code: Select all
PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile(@"../../data/EmbedGridInCell.pdf");

            //获取文档的所有域
            PdfFormWidget form = pdf.Form as PdfFormWidget;

            //遍历所有域名
            for (int i = 0; i < form.FieldsWidget.Count; i++)
            {
                PdfField field = form.FieldsWidget.List[i] as PdfField;
               
                //如果为TextBox域名
                if (field is PdfTextBoxFieldWidget)
                {               
                    //填充文本域
                    PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;

                    //为textbox填入文本设置字体
                    textBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);
                    textBoxField.Text = "123%%%%***&&&^^^你好!";
                }
               
                if (field is PdfComboBoxWidgetFieldWidget)
                {
                    //填充文本域
                    PdfComboBoxWidgetFieldWidget comboBoxField = field as PdfComboBoxWidgetFieldWidget;

                    //为textbox填入文本设置字体
                     comboBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);

                    comboBoxField.SelectedIndex = new int[] {0};
                }
            }
         
            pdf.SaveToFile(@"../../output/result_free.pdf", FileFormat.PDF);

如果这些对您没有帮助,请提供以下信息来帮助我们重现您的问题并为您提供解决方案。
a. 您的输入Pdf文档,您可以附加在此贴中也可以发送到我们的邮箱(support@e-iceblue.com)。
b. 您完整的测试代码,确保我们可以重现您的问题。
c. 您的项目类型,如控制台,.NET Framework4.8.
d. 您测试的系统环境,如win7.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 944
Joined: Tue Mar 08, 2022 2:02 am

Mon Nov 28, 2022 10:41 am

Abel.He wrote:您好,

感谢您的反馈。
1) 首先你要确保您在代码中为文本框指定的字体再您本地电脑上已将安装了。
2) 对于下拉菜单域同样可以使用代码指定字体:

Code: Select all
  PdfComboBoxWidgetFieldWidget comboBoxField = field as PdfComboBoxWidgetFieldWidget;

                    //为textbox填入文本设置字体
                     comboBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);

3) 我这边模拟的文档和代码测试,没有重现您提到的文本框中数字、符号乱码的情况。我把我的测试代码和测试文档放在下面供您参考。
Code: Select all
PdfDocument pdf = new PdfDocument();

            pdf.LoadFromFile(@"../../data/EmbedGridInCell.pdf");

            //获取文档的所有域
            PdfFormWidget form = pdf.Form as PdfFormWidget;

            //遍历所有域名
            for (int i = 0; i < form.FieldsWidget.Count; i++)
            {
                PdfField field = form.FieldsWidget.List[i] as PdfField;
               
                //如果为TextBox域名
                if (field is PdfTextBoxFieldWidget)
                {               
                    //填充文本域
                    PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;

                    //为textbox填入文本设置字体
                    textBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);
                    textBoxField.Text = "123%%%%***&&&^^^你好!";
                }
               
                if (field is PdfComboBoxWidgetFieldWidget)
                {
                    //填充文本域
                    PdfComboBoxWidgetFieldWidget comboBoxField = field as PdfComboBoxWidgetFieldWidget;

                    //为textbox填入文本设置字体
                     comboBoxField.Font = new PdfTrueTypeFont(new Font("宋体", 16f, FontStyle.Regular), true);

                    comboBoxField.SelectedIndex = new int[] {0};
                }
            }
         
            pdf.SaveToFile(@"../../output/result_free.pdf", FileFormat.PDF);

如果这些对您没有帮助,请提供以下信息来帮助我们重现您的问题并为您提供解决方案。
a. 您的输入Pdf文档,您可以附加在此贴中也可以发送到我们的邮箱(support@e-iceblue.com)。
b. 您完整的测试代码,确保我们可以重现您的问题。
c. 您的项目类型,如控制台,.NET Framework4.8.
d. 您测试的系统环境,如win7.

Sincerely
Abel
E-iceblue support team



我通过您的方法,给文本框和下拉框都设置了字体,设置时是按照您的写法设置的宋体但是通过Adobe软件打开域的设置时看到的并不宋体而是类似ERCZGD+SimSun,这种字体会在网页显示PDF文件时丢失部分文本框中的信息

LoveIceCream
 
Posts: 5
Joined: Tue Nov 15, 2022 11:58 am

Tue Nov 29, 2022 10:11 am

您好,

感谢您的反馈。
请注意SimSun就是宋体,对于您提到的在网页中打开pdf文件出现文本框中部分内容丢失的问题。您使用的是最新版的Spire.Pdf8.11.2吗?如果没有,我建议您可以使用我们最新的商业版测试您的问题,因为我们商业版相对免费版更加问题且修复了更多bug。另外,如果最新的商业版也有这个问题,请您提供以下信息来帮助我们重现您的问题并为您提供解决方案。
1) 您的测试代码。
2) 您的输入文档和结果文档,您可以附在此贴中,也可以发送到我们的邮箱(support@e-iceblue.com)。
3) 您是使用什么浏览器打开的pdf文件呢?如谷歌浏览器。

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 944
Joined: Tue Mar 08, 2022 2:02 am

Return to 中文技术支持