1

我们正在使用 WkHtmlToPdf 将 html 转换为 pdf。现在我也使所有表单字段都可以根据要求进行编辑。由于在将 Html 转换为 Pdf 时 WkHtmlToPdf 中存在错误,因此它使所有表单字段的属性为可见不可打印。

为了解决这个问题,我们使用 ITextSharp 读取由 WkHtmlToPdf 生成的现有 pdf 并将该属性设置为每个表单字段的可见性。

我就是这样做的

        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 循环中的最后一行)复选框时,它会丢失其所有属性(无论我选中还是未选中),它只显示空复选框,并且不可编辑。

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

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 中的任何其他字段一样可见。除了复选框之外,所有字段(文本框)都在打印其值。

我正在通过 --> Open pdf document--> Forms--> Add/Edit Fields 检查属性

我在这里做错了什么?我没有在复选框上设置属性,而是尝试使用 Itextsharp 构建一个全新的复选框,但我无法使用 ItexSharp 手动示例。以前有人做过这些东西吗?

即使我使用 ItextSharp 生成了一个新的复选框,我如何才能完全替换现有的复选框?

非常感谢您阅读我的故事。。

4

2 回答 2

1

好吧,我通过用 ITextSharp 替换 wkHtmltoPdf 复选框解决了这个问题。我正在详细回答它可能会节省您的时间

            /// <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;
    }
于 2012-11-08T04:50:54.193 回答
1

这是我使用 CSS 伪元素的解决方案:

<style>
label.checkbox:before {
    content: "☐ ";
}
label.checkbox.checked:before {
    content: "☑ ";
}
</style>
<label class="checkbox checked">Yes</label>
<label class="checkbox">No</label>
于 2016-12-22T05:51:11.773 回答