2

PDFSharp を使用して、(描画された) 署名付きの png 画像を PDF に挿入したいと考えています。画像の位置とサイズは、以前に PDF で作成した非表示の PdfTextField によって決まります。現在のコードの問題: PdfTextField (このフィールドを含むページ) からページ参照を取得するにはどうすればよいでしょうか?

コード:

PdfDocument document = PdfReader.Open("C:\\filex.pdf", PdfDocumentOpenMode.Modify);

// Get the root object of all interactive form fields
PdfAcroForm form = document.AcroForm;

// Get all form fields of the whole document
PdfAcroField.PdfAcroFieldCollection fields = form.Fields;

//Get the invisible PdfTextField used as position and size reference
PdfTextField signatureposition= (PdfTextField)fields["thesignature"];

PdfArray signaturerect= (PdfArray)signatureposition.Elements["/Rect"];
string x = signaturerect.Elements[0].ToString();
string y = signaturerect.Elements[1].ToString();
string w = signaturerect.Elements[2].ToString();
string h = signaturerect.Elements[3].ToString();

string imagepath= "C:\\signature.png";

//how to get the correct page reference respect the especified field?
PdfPage page= signatureposition.Owner.Pages[???];

XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile(imagepath);
gfx.DrawImage(image, x, y, width, height);
4

1 に答える 1

2

最後に、この関数を作成して解決しました:

   protected int PaginaCampo(string campofirma, PdfDocument document)
{
    for (int i = 0; i < document.Pages.Count; i++)
    {
        PdfAnnotations anotations = document.Pages[i].Annotations;
        for (int j = 0; j < anotations.Count; j++)
        {
            if (anotations[j].Title != campofirma) continue;
            return i;
        }
    }
    return -1;
}

最善の解決策ではありませんが、うまくいきます...誰かがより良い解決策を追加した場合、私たちは彼/彼女に正しい答えを与えます.

于 2012-10-30T07:43:21.487 に答える