5

iTextSharp を使用して PDF テンプレートに入力しています。私が使用しているデータはデータベースに保存されており、HTML 形式になっています。私の問題は、AcroFieldこのテキストをロードすると改行が行われることですが、太字や斜体はありません。

私はすでに HtmlWorker を使用しようとしましたが、オンラインのすべての例は、HTML を PDF に変換するために使用されていることを示していますがAcroField、PDF テンプレートに を設定しようとしています。

4

2 に答える 2

11

フォーラムと iTextsharp のソース コードを何日も調べた後、解決策を見つけました。Acrofield を HTML 形式のテキストで埋める代わりに、ColumnText を使用しました。HTML テキストを解析し、IElements を段落に読み込みます。次に、段落を ColumnText に追加します。次に、フィールドの座標を使用して、Acrofield があるべき場所の上に ColumnText をオーバーレイしました。

    public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList<AcroFields.FieldPosition> pos) 
    {
        Paragraph par = new Paragraph();
        ColumnText c1 = new ColumnText(contentBtye);
        try
        {
            List<IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
            foreach (IElement element in elements) 
            {
               par.Add(element);
            }
            
            c1.AddElement(par);
            c1.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
            c1.Go(); //very important!!!
        }
        catch (Exception ex)
        {
            
            throw;
        }
    }

この関数の呼び出しの例を次に示します。

string htmlText ="<b>Hello</b><br /><i>World</i>";
IList<AcroFields.FieldPosition> pos = form.GetFieldPositions("Field1");
//Field1 is the name of the field in the PDF Template you are trying to fill/overlay
AddHTMLToContent(htmlText, stamp.GetOverContent(pos[0].page), pos);
//stamp is the PdfStamper in this example

これを行っているときに遭遇したことの 1 つは、私の Acrofield に定義済みのフォント サイズがあるという事実です。この関数はフィールドの上に ColumnText を設定するため、フォントの変更はすべて関数で行う必要があります。フォントサイズを変更する例を次に示します。

 public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList<AcroFields.FieldPosition> pos) 
    {
        Paragraph par = new Paragraph();
        ColumnText c1 = new ColumnText(contentBtye);
        try
        {
            List<IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
            foreach (IElement element in elements) 
            {
                foreach (Chunk chunk in element.Chunks) 
                {
                    chunk.Font.Size = 14;
                }
            }
            par.Add(elements[0]);
            c1.AddElement(par);
            c1.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
            c1.Go();//very important!!!
        }
        catch (Exception ex)
        {
            
            throw;
        }
    }
于 2013-03-27T18:50:22.597 に答える
0

そのため、過去数か月間、このコードを少し調整する必要がありましたが、これを行うためのより良い/より少ない方法を見つけました。

    public void Final(string text,string fieldName,string filename) 
    {
        iTextSharp.text.pdf.PdfReader reader = null;
        iTextSharp.text.pdf.PdfStamper stamp = null;

        reader = new PdfReader(file path to template);
        stamp = new PdfStamper(reader, new FileStream(path to new file, FileMode.CreateNew));

        AcroFields form = stamp.AcroFields;

        //get the position of the field
        IList<AcroFields.FieldPosition> pos = form.GetFieldPositions(fieldName);
        //tell itextSharp to overlay this content 
        PdfContentByte contentBtye = stamp.GetOverContent(pos[0].page);

        //create a new paragraph
        Paragraph par = new Paragraph();
        //parse html
        List<IElement> elements = HTMLWorker.ParseToList(new StringReader(text), null);
        for (int k = 0; k < elements.Count; k++)
        {
            par.Add((IElement)elements[k]);
        }
        //create a ColumnText to hold the paragraph and set position to the position of                   the field
        ColumnText ct = new ColumnText(contentBtye);
        ct.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
        ct.AddElement(par);
        ct.Go();
        stamp.Close();
        reader.Close();

    }
于 2014-01-30T17:23:16.533 に答える