无法打印检查框PDF [英] Unable to print Check Boxes in pdf

查看:445
本文介绍了无法打印检查框PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用WkHtmlToPdf转换从HTML到PDF。现在,我已经让所有的表单字段要求编辑为好。由于有一个bug在WkHtmlToPdf在转换HTML到PDF它使所有的表单字段的属性作为可见不可打印。

We are using WkHtmlToPdf to convert from html to pdf. Now I have make all the form fields editable on request as well. Since there is a bug in WkHtmlToPdf while converting Html to Pdf it makes all the form field's property as Visible Not Printable.

要解决这个问题,我们使用iTextSharp的阅读已被WkHtmlToPdf生成并设置该属性为可见每一个表单域现有的PDF。

To get around this problem we are using ITextSharp to read existing pdf that has been generated by WkHtmlToPdf and setting that property as Visible for every form field.

这是我过得怎么样了。

        var pdfReader = new PdfReader(inputFilename);

        using (var outputStream = new FileStream(outputFilename, FileMode.OpenOrCreate))
        {
            var stamper = new PdfStamper(pdfReader, outputStream);

            // Regenarating all the fileds using itextSharp. 
            // Since wkHtmltoPdf has differnt default settings for pdf form fields (Visible and not Printable)
            var formFields = stamper.AcroFields;
            var filedNames = formFields.Fields.Keys;

            foreach (var keyName in filedNames)
            {
                formFields.SetFieldProperty(keyName, "setflags", PdfAnnotation.FLAGS_PRINT, null);
                formFields.SetFieldProperty(keyName, "setflags", PdfFormField.FF_EDIT, null);
                formFields.SetFieldProperty(keyName, "textsize", 8.0f, null);
                formFields.RegenerateField(keyName);
              }
          }

这解决了我的问题的所有领域,但复选框。我有几个HTML复选框被转换使用WkHtmlToPdf为PDF。我意识到,当过我尝试RegenerateField()(最后一行foreach循环)复选框失去其所有属性(无论我选中或取消选中),它只是显示空复选框,它不能编辑。

This solved my problem for all the fields but check Boxes. I have few check boxes in html which are converted to pdf using WkHtmlToPdf. I realized that when ever I try to RegenerateField() (Last line in foreach loop) check box is loosing all its properties (Whether I checked or unchecked) it is only showing empty check box, and its not editable.

要解决这个问题,我想出了以下

To solve this problem I came up with the following

if (formFields.GetFieldType(keyName).Equals(AcroFields.FIELD_TYPE_CHECKBOX))
{
                    // Regenerate checkbox formfield causing it to reset all its properties 
                    // including checkd/uncheked property     
                    formFields.SetFieldProperty(keyName, "setflags", PdfAnnotation.FLAGS_PRINT, null);

// Ignore the commented code as it is not working Not sure why??? and how

                    //var item = formFields.GetFieldItem(keyName);
                    //var appearance = item.GetValue(0).Get(PdfName.AS);

                    //if (appearance != null)
                    /{
                    //    if (appearance.tostring().tolower().equals("/yes"))
                    //    {
                    //        formfields.regeneratefield(keyname);

                     // Not even one working when I try to set the check box status like below
                    //        success  = formfields.setfield(keyname, "yes");
                    //        success  = formfields.setfield(keyname, "/yes");
                    //        success  = formfields.setfield(keyname, "/yes");
                    //        success = formfields.setfield(keyname, "on");
                    //        success =  formfields.setfield(keyname, "1");


                    //    }
                    }

通过代码(不包括注释)代码复选框成功保持其地位,并在生成的PDF显示。但它不是打印。我检查复选框的属性就好像在PDF任何其他领域可见。除了复选框所有字段(文本框)打印其值。

With the code (excluding commented) code Check box successfully retaining its status and displaying in the generated Pdf. But it's not printing. I checked the properties of check box it's visible like any other fields in the pdf. Apart from check boxes all the fields(text boxes) are printing its values.

我通过检查的属性 - >打开PDF文档 - >窗体 - >添加/编辑域

I am checking properties through --> Open pdf document--> Forms--> Add/Edit Fields

我在做什么错在这里?相反,在复选框设置属性我试图构建使用iTextSharp的一个全新的复选框,但我不能使用ItexSharp手动例子。 DO任何人做这个东西之前?

What am I doing wrong here? Instead of setting property on check box I tried to construct a brand new check box using Itextsharp but I couldn't using ItexSharp manual example. DO anyone did this stuff before?

即使我产生使用iTextSharp的一个新的复选框,我怎么能替代现有究竟复选框?

Even if I generate a new check box using ItextSharp, how can I replace exactly existing check box?

非常感谢我的故事..

Thanks a lot for reading my story..

推荐答案

好我用iTextSharp的替代wkHtmltoPdf复选框解决了这个问题。我回答得很详细,可能节省您的时间。

Well I solved this problem by replacing wkHtmltoPdf check box with ITextSharp. I am answering it in detail it might save your time

            /// <summary>
    /// By default, wkHtmlToPdf leaves pdf form fields as being "Visible but not printable". Using iTextSharp we will change them to being "Visible".
    /// </summary>
    private string MakePdfFormFieldsEditable(string inputFilename)
    {
        var outputFilename = CreateTempPdfFilename();

        var pdfReader = new PdfReader(inputFilename);

        var checkedCheckBoxNames = new List<string>();

        using (var outputStream = new FileStream(outputFilename, FileMode.OpenOrCreate))
        {
            var stamper = new PdfStamper(pdfReader, outputStream);

            // Regenarating all the fileds using itextSharp. 
            // Since wkHtmltoPdf has differnt default settings for pdf form fields (Visible and not Printable)
            var formFields = stamper.AcroFields;
            var filedNames = formFields.Fields.Keys;
            var removableFiledNames = new List<string>();

            foreach (var keyName in filedNames)
            {
                if (formFields.GetFieldType(keyName).Equals(AcroFields.FIELD_TYPE_CHECKBOX))
                {
                    // Since check boxes generated by WKHtmltoPdf is crapy and click behaviour is annoying
                    // we are replacing it with new checkbox
                    var item = formFields.GetFieldItem(keyName);

                    // Return "/yes" if it's checked
                    var appearance = item.GetValue(0).Get(PdfName.AS);

                    // Holds current page number information and Location on the page (Rectangle Dimentions)
                    var position = formFields.GetFieldPositions(keyName);

                    var newCheckBoxFieldname = CreateNewCheckBox(position, stamper, keyName);

                    // set newly created check box value
                    if (appearance != null && appearance.ToString().ToLower().Equals("/yes"))
                    {
                        checkedCheckBoxNames.Add(newCheckBoxFieldname);
                    }
                    // List of Check Box field names to be removed
                    removableFiledNames.Add(keyName);
                }
                else
                {
                    formFields.SetFieldProperty(keyName, "setflags", PdfAnnotation.FLAGS_PRINT, null);
                    formFields.SetFieldProperty(keyName, "setflags", PdfFormField.FF_EDIT, null);
                    formFields.SetFieldProperty(keyName, "textsize", 8.0f, null);

                    formFields.RegenerateField(keyName);
                }
            }

            // Removing check boxes generaed with WkHtmlToPdf
            foreach (var oldCheckBox in removableFiledNames)
            {
                formFields.RemoveField(oldCheckBox);
            }

            stamper.Close();
        }

        return checkedCheckBoxNames.Any() ? UpdateCheckBoxes(outputFilename, checkedCheckBoxNames) : outputFilename;
    }


    /// <summary>
    /// Updating Check Boxe's status checked or not in input file
    /// </summary>
    private string UpdateCheckBoxes(string inputFilename, ICollection<string> checkedCheckBoxNames)
    {
        var outputFilename = CreateTempPdfFilename();

        var pdfReader = new PdfReader(inputFilename);

        using (var outputStream = new FileStream(outputFilename, FileMode.OpenOrCreate))
        {
            var stamper = new PdfStamper(pdfReader, outputStream);

            var newformFields = stamper.AcroFields;
            var newfiledNames = newformFields.Fields.Keys;

            foreach (var keyName in newfiledNames)
            {
                if (!newformFields.GetFieldType(keyName).Equals(AcroFields.FIELD_TYPE_CHECKBOX)) continue;

                if (!checkedCheckBoxNames.Contains(keyName)) continue;

                newformFields.SetField(keyName, "On");
            }

            stamper.Close();
        }

        return outputFilename;
    }

    /// <summary>
    /// Constructing new check box
    /// </summary>
    /// <param name="fieldPosition"></param>
    /// <param name="stamper"></param>
    /// <param name="keyName"></param>
    /// <returns>new field name</returns>
    private string CreateNewCheckBox(IList<AcroFields.FieldPosition> fieldPosition, PdfStamper stamper, string keyName)
    {
        var pageNumber = fieldPosition.First().page;
        var locationRectangle = fieldPosition.First().position;

        PdfContentByte canvas = stamper.GetOverContent(1);

        // Create array with two appearances
        var onOff = new PdfAppearance[2];
        onOff[0] = canvas.CreateAppearance(15, 15);
        onOff[0].Rectangle(1, 1, 13, 13);
        onOff[0].Stroke();
        onOff[1] = canvas.CreateAppearance(15, 15);
        onOff[1].SetRGBColorFill(255, 128, 128);
        onOff[1].Rectangle(1, 1, 13, 13);
        onOff[1].FillStroke();
        onOff[1].MoveTo(1, 1);
        onOff[1].LineTo(14, 14);
        onOff[1].MoveTo(1, 14);
        onOff[1].LineTo(14, 1);
        onOff[1].Stroke();

        var newFieldName = keyName + "_G";

        // Creates new check boxes
        var checkbox = new RadioCheckField(stamper.Writer, locationRectangle, newFieldName, "On");

        var field = checkbox.CheckField;

        // Add check box field to writer
        stamper.AddAnnotation(field, pageNumber);

        return newFieldName;
    }

这篇关于无法打印检查框PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆