Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用itextsharp为表单字段值加下划线_C#_Pdf_Itextsharp_Field_Underline - Fatal编程技术网

C# 使用itextsharp为表单字段值加下划线

C# 使用itextsharp为表单字段值加下划线,c#,pdf,itextsharp,field,underline,C#,Pdf,Itextsharp,Field,Underline,我有一个应用程序,它使用itextsharp填充PDF表单字段 我从客户那里得到了新的要求,允许在字段值下划线 我在这个网站上读过很多帖子,包括问题的答案,但我想不出一个办法 当前我的代码执行以下操作: 创建PDFStamper实例 使用stamper.AcroFields属性获取表单字段 使用AcroFields.SetFieldRichValue()方法设置字段值 但是当我打开PDF时,字段是空的 我验证了该字段在PDF本身中设置为富文本 知道我做错了什么吗 下面是我的代码的一个snnipe

我有一个应用程序,它使用itextsharp填充PDF表单字段

我从客户那里得到了新的要求,允许在字段值下划线

我在这个网站上读过很多帖子,包括问题的答案,但我想不出一个办法

当前我的代码执行以下操作:

  • 创建PDFStamper实例
  • 使用stamper.AcroFields属性获取表单字段
  • 使用AcroFields.SetFieldRichValue()方法设置字段值
  • 但是当我打开PDF时,字段是空的

    我验证了该字段在PDF本身中设置为富文本

    知道我做错了什么吗

    下面是我的代码的一个snnipest:

                    FileStream stream = File.Open(targetFile, FileMode.Create);                                     
                    var pdfStamper = new PdfStamper(new PdfReader(sourceFile), stream);                     
    
                    // Iterate the fields in the PDF
                    foreach (var fieldName in pdfStamper.AcroFields.Fields.Keys)
                    {                       
                        // Get the field value of the current field
                        var fieldValue = "<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:contentType=\"text/html\" xfa:APIVersion=\"Acrobat:8.0.0\" xfa:spec=\"2.4\"><p style=\"text-align:left\"><b><i>Here is some bold italic text</i></b></p><p style= \"font-size:16pt\">This text uses default text state parameters but changes the font size to 16.</p></body>"
    
                        // Set the field value
                        if (String.IsNullOrEmpty(fieldValue) == false)
                        {
                            pdfStamper.AcroFields.SetFieldRichValue(key, fieldValue);
                        }
                    }
    
    FileStream-stream=File.Open(targetFile,FileMode.Create);
    var pdfStamper=新的pdfStamper(新的PdfReader(sourceFile),流);
    //迭代PDF中的字段
    foreach(pdfStamper.AcroFields.Fields.Keys中的变量fieldName)
    {                       
    //获取当前字段的字段值
    var fieldValue=“

    这里是一些粗体斜体文本

    此文本使用默认文本状态参数,但将字体大小更改为16。

    “ //设置字段值 if(String.IsNullOrEmpty(fieldValue)==false) { pdfStamper.AcroFields.SetFieldRichValue(键,fieldValue); } }
    编辑:

    我已经根据MarkStorer的帖子修改了我的代码(http://stackoverflow.com/questions/1454701/adding-rich-text-to-an-acrofield-in-itextsharp). 新守则是:

            // Create reader to read the source file
            var reader = new PdfReader(sourceFile);
    
            // Create a stream for the generated file
            var stream = File.Open(targetFile, FileMode.Create); 
    
            // Create stamper to generate the new file
            var pdfStamper = new PdfStamper(reader, stream);
    
            // Field name and value
            var fieldName = "myfield";
            var fieldValue = "<?xml version=\"1.0\"?><body xfa:APIVersion=\"Acroform:2.7.0.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\"><p dir=\"ltr\" style=\"margin-top:0pt;margin-bottom:0pt;font-family:Helvetica;font-size:12pt\"><b>write line1 bold</b></p</body>";
    
            // Output stream for the temporary file that should contain the apearance of the field
            var msOutput = new FileStream(@"d:\temp.pdf", FileMode.Create);
    
            // string reader to read the field value
            var textReader = new StringReader(fieldValue);
    
            // Create new document
            var document = new Document(pdfStamper.AcroFields.GetFieldPositions(fieldName)[0].position);
    
             // writer for the new doucment
            var writer = PdfWriter.GetInstance(document, msOutput);
    
            // Open the document                
            document.Open();
    
            // Get elements to append to the doucment
            var list = HTMLWorker.ParseToList(textReader, null);
    
            // Append elements to the doucment
            foreach (var element in list)
            {
                document.Add(element);
            }                                           
    
            // close the documnet
            document.Close();                            
    
            // Append push button that contains the generated content as its apearance
            // this approach is based on the suggestion from Mark storer that can be found in:
            // http://stackoverflow.com/questions/1454701/adding-rich-text-to-an-acrofield-in-itextsharp
            var button = new PushbuttonField(pdfStamper.Writer, pdfStamper.AcroFields.GetFieldPositions(fieldName)[0].position, fieldName + "_")
                                 {
                                     Layout = PushbuttonField.LAYOUT_ICON_ONLY,
                                     BackgroundColor = null,
                                     Template = pdfStamper.Writer.GetImportedPage(new PdfReader(targetFile, 1)
                                 };
                pdfStamper.AddAnnotation(button.Field, 1);                
    
            pdfStamper.FormFlattening = true;
            pdfStamper.Close();
            pdfStamper.Dispose();
    
    //创建读取器以读取源文件
    变量读取器=新的PdfReader(源文件);
    //为生成的文件创建流
    var stream=File.Open(targetFile,FileMode.Create);
    //创建母版以生成新文件
    var pdfStamper=新的pdfStamper(读卡器、流);
    //字段名和值
    var fieldName=“myfield”;
    var fieldValue=“

    write line1 bold上面有很多代码,将近400行。将来,尝试将所有内容提取到一个非常简单且可复制的测试用例中

    使用
    SetFieldRichValue
    时,还需要将
    PdfStamper
    AcroFields.generateapearances
    属性设置为
    false

    stamper.AcroFields.GenerateAppearances = false;
    

    您可以阅读更多关于此的信息,以及一些注意事项和其他解决方法。

    我用了一个小技巧解决了这个问题-我使用了Anchor(hyperling)元素内部的ColumnText元素,并将其放置在表单字段上方。默认情况下,锚点以下划线显示。为了避免用户将鼠标悬停在锚点上时出现手形标记,我将锚点的“Reference”属性设置为null

    以下是我使用的代码:

     // Create reader
     var reader = new PdfReader(sourceFilePathAndName);
    
     // Create stream for the target file
     var stream = File.Open(targetFilePathAndName, FileMode.Create); 
    
     // Create the stamper
     var pdfStamper = new PdfStamper(reader, stream);
    
     const string fieldName = "MyField";     
    
     // Get the position of the field
     var targetPosition = pdfStamper.AcroFields.GetFieldPositions(fieldName)[0].position;
    
     // Set the font
     var fontNormal = FontFactory.GetFont("Arial", 16, Font.UNDERLINE, BaseColor.BLACK);
    
     // Create the anchor
     var url = new Anchor("some default text", fontNormal) { Reference = null };
    
     // Create the element that will contain the anchor and allow to position it anywhere on the document
     var data = new ColumnText(pdfStamper.GetOverContent(1));
    
     // Add the anchor to its container
     data.SetSimpleColumn(url, targetPosition.Left, targetPosition.Bottom, targetPosition.Right, targetPosition.Top, 0, 0);
    
     // Write the content to the document
     data.Go();
    
     pdfStamper.FormFlattening = true;
     pdfStamper.Close();
     pdfStamper.Dispose();
    

    谢谢。我会检查链接。我已经更新了我的问题,以减少代码长度。我不知道这是否是您的问题,但您正在从您正在写入的流中读取。您的第二行代码将打开
    targetFile
    ,然后在代码仍然打开的情况下尝试从底部读取。当我运行您的代码时,我会执行以下操作:ally在尝试这样做时遇到异常。不-这不是问题所在。我没有检查您所说的是否与问题相关,因为问题在代码的前面就开始了-在代码中,我正在创建文件d:\temp.pdf,该文件在调用document.Close()后应具有内容。在这一行代码之后,临时文件仍然是空的,我不知道为什么。我假设在解决这个问题后,将临时文件内容应用于按钮的模板属性会更直接。您使用的是什么版本的iTextSharp?Chris-我通过一个简单的解决方法解决了这个问题。请参阅我的答案低。textsharp版本为5.1.1.0