0

itextsharpを使用してPDFフォームフィールドに入力するアプリケーションがあります。

フィールドの値に下線を引くことを許可するという新しい要件を顧客から受け取りました。

このサイトで質問への回答を含む多くの投稿を読みましたが、その方法がわかりませんでした。

現在の私のコードは次のことを行います:

  1. PDFStamperインスタンスを作成します
  2. Stamper.AcroFieldsプロパティを使用してフォームフィールドを取得します
  3. AcroFields.SetFieldRichValue()メソッドを使用してフィールド値を設定します。

しかし、PDFを開いているとき、フィールドは空です。

PDF自体でフィールドがリッチテキストとして設定されていることを確認しました。

私が間違っていることについて何か考えはありますか?

これが私のコードの概要です:

                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);
                    }
                }

編集:

Mark Storerの質問への投稿(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();

しかし、現在の問題は、一時的なドキュメントにコンテンツが含まれていないことです。

何か案は ?

4

2 に答える 2

0

上記のコードはたくさんあり、400行近くあります。将来的には、すべてを非常にシンプルで再現性のあるテストケースにまとめてみてください。

使用するときは、 's プロパティを次のSetFieldRichValueように設定する必要もあります。PdfStamperAcroFields.GenerateAppearancesfalse

stamper.AcroFields.GenerateAppearances = false;

これについては、いくつかの注意事項やその他の回避策とともに、ここで詳しく読むことができます。

于 2012-05-01T13:11:53.590 に答える
0

私は少しのトリックでそれを解決することができました-ColumnText要素内のアンカー(ハイパーリング)要素を使用して、フォームフィールドの上に配置しました。デフォルトでは、アンカーは下線付きで表示されます。ユーザーがアンカーにマウスを置いたときにハンドマーカーを回避するために、アンカーの「参照」プロパティを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();
于 2012-05-03T13:16:15.613 に答える