0

我终于成功地弄清楚了如何使用 iTextSharp 使用我的自定义数据用 XFA 表单填充 PDF。

问题是我丢失了让我将 XFA 设为只读的代码。在将工作版本提交到我的源代码管理之前,我犯了一个可怕的错误,即更改我的代码。现在,在谷歌搜索了一个小时后,我仍然找不到它:(如果有人能提醒我代码,那将不胜感激。

            PdfReader.unethicalreading = true;
            PdfReader reader = new PdfReader(pdfFileName);
            PdfStamper stamper = new PdfStamper(reader, ms);

            XfaForm xfa = new XfaForm(reader);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(CreateXmaData(XDocument.Parse(xfa.DomDocument.InnerXml)));

            xfa.DomDocument = doc;
            xfa.Changed = true;

            XfaForm.SetXfa(xfa, stamper.Reader, stamper.Writer);

            PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
            stamper.Writer.SetOpenAction(action);


            // Somewhere here I had the code that made my XFA form read only...


            stamper.Writer.CloseStream = false;

            stamper.Close();
            reader.Close();

            byte[] buffer = new byte[ms.Position];
            ms.Position = 0;
            ms.Read(buffer, 0, buffer.Length);

            return buffer;
4

2 回答 2

0

不知道我是否梦想着我有只读的工作或什么,我怀疑这是最好的方法,但这是我最终能够做到的:

...
    doc.LoadXml(CreateXmaData(XDocument.Parse(xfa.DomDocument.InnerXml)));

    PdfAction readOnlyAction = PdfAction
         .JavaScript(MakeReadOnly(xfa.DomDocument.InnerXml), stamper.Writer);
    stamper.Writer.AddJavaScript(readOnlyAction);

    xfa.DomDocument = doc;
...



    private string MakeReadOnly(string xml) 
    {
        string formName = string.Empty;
        int subFormStart = xml.IndexOf("<subform", 0);
        if (subFormStart > -1)
        {
            int nameTagStart = xml.IndexOf("name", subFormStart);
            int nameStart = xml.IndexOf("\"", nameTagStart);
            int nameEnd = xml.IndexOf("\"", nameStart + 1);

            formName = xml.Substring(nameStart + 1, (nameEnd - nameStart) - 1);
        }

        string readOnlyFunction = "ProcessAllFields(xfa.form." + formName + ");";
        readOnlyFunction += "function ProcessAllFields(oNode) {";
        readOnlyFunction += " if (oNode.className == \"exclGroup\" || oNode.className == \"subform\"  || oNode.className == \"subformSet\" || oNode.className == \"area\") { ";
        readOnlyFunction += "  for (var i = 0; i < oNode.nodes.length; i++) {";
        readOnlyFunction += "   var oChildNode = oNode.nodes.item(i); ProcessAllFields(oChildNode);";
        readOnlyFunction += "  }";
        readOnlyFunction += " } else if (oNode.className == \"field\") {";
        readOnlyFunction += "  oNode.access = \"readOnly\"";
        readOnlyFunction += " }";
        readOnlyFunction += "}";

        return readOnlyFunction;
    }
于 2013-09-16T05:26:58.457 回答
-1

这对我有用

String script = "for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) { var oFields = xfa.layout.pageContent(nPageCount, \"subform\"); var nNodesLength = oFields.length;";
       script += "for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) { oFields.item(nNodeCount).access = \"readOnly\"; } } ";
于 2018-01-04T19:42:16.290 回答