【问题标题】:How do you bold specific text in a PDF form field *and then flatten the form* using iTextSharp?如何在 PDF 表单字段中加粗特定文本*然后使用 iTextSharp 展平表单*?
【发布时间】:2012-08-31 14:46:03
【问题描述】:

鉴于已指定字段的 PDF 表单,我希望能够在其中一个表单字段中放置类似的内容:

罗德里戈,你是不是父亲!

使用 iTextSharp,我可以使用富文本值(尽管有点费力)完成此任务,并根据this answer 传递带有适当粗体标记的 XML 表示内容。

但是,根据相同的答案(以及我的第一手经验),您在完成此操作后无法锁定+展平表单(以防止进一步编辑)。不幸的是,在这种情况下这也是必要的。

previously-mentioned answer 的作者提到,这可以通过“[具有 Javascript] 在打开表单时重置字段值以强制 Acrobat/Reader 为您构建外观 [s] 来解决。

如何将 Javascript 代码添加到 PDF(最好通过 iTextSharp 本身,尽管我们可能能够在文件本身的后端执行此操作)以便执行何时打开表单以强制 PDF 阅读器为您构建外观?

此外,这是否允许我们不必说GenerateAppearances = false,从而允许我们锁定和展平表单?

如果没有,除了 iTextSharp 之外,还有什么东西可以让我们将表单字段中的某些字词加粗,但也可以锁定和展平表单以防止我们在完成后进行编辑?

【问题讨论】:

    标签: .net pdf itextsharp rtf


    【解决方案1】:

    我发现有一种方法可以实现这一目标,尽管不是通过任何特别优雅的方式(据我所知)。

    您的方法是使用 iTextSharp在现有表单域上直接写入文本域

    这里的主要警告是 pdf 基本上不再成为表单,所以如果您需要阅读表单字段的内容,那您就完蛋了。

    另一方面,如果表单字段基本上被用作一个简单的指南来帮助告诉 iTextSharp 将文本放在 pdf 的哪个位置(并且仅用于输出),那么这可能会起作用。

    using (FileStream filestream = new FileStream(outputpath, FileMode.CreateNew, FileAccess.Write))
    {
        var stamper = new PdfStamper(reader, filestream);
    
        var acroFields = stamper.AcroFields;
    
        string fieldName = "Field Name";
        var fieldPositions = acroFields.GetFieldPositions(fieldName);
    
        var helveticaBold = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14);
        var helveticaRegular = FontFactory.GetFont(FontFactory.HELVETICA, 14);
    
        List<Chunk> chunks = new List<Chunk>();
    
        chunks.Add(new Chunk("Rodrigo, you are ", helveticaRegular));
        chunks.Add(new Chunk("NOT", helveticaBold));
        chunks.Add(new Chunk(" the father!", helveticaRegular));
    
        Phrase currentPhrase = new Phrase();
    
        foreach (Chunk chunk in chunks)
        {
            currentPhrase.Add(chunk);
        }
    
        foreach (var currentPosition in fieldPositions)
        {
            PdfContentByte currentCanvas = stamper.GetOverContent(currentPosition.page);
    
            ColumnText currentColumnText = new ColumnText(currentCanvas);
    
            currentColumnText.SetSimpleColumn(currentPhrase, currentPosition.position.Left,
                                              currentPosition.position.Bottom, currentPosition.position.Right,
                                              currentPosition.position.Top, 13, Element.ALIGN_LEFT);
            currentColumnText.Go();
        }
    
        stamper.FormFlattening = true;
        stamper.Close();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      相关资源
      最近更新 更多