使用iTextSharp的PDF XFA表格只读

时间:2013-09-12 00:30:05

标签: c# pdf readonly xfa

我终于成功地找到了如何使用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;

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:-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\"; } } ";