使用ITextSharp在PDF表单字段中的文本上写入图像

时间:2014-01-28 06:48:05

标签: itextsharp

当我尝试在文本字段上写入图像时,图像写在文本后面。它适用于普通文本和图像,但不适用于表单字段。我想在Text上写下覆盖文本的图像。

void OldMethod()
{
    string path = @"C:\Users\";

    string OutLocation = path + "New.pdf";

    string Old = path + "Old.pdf";

    PdfReader reader1 = new PdfReader(Old);

    using (FileStream fs = new FileStream(OutLocation, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        using (PdfStamper stamper = new PdfStamper(reader1, fs))
        {
            int pageCount1 = reader1.NumberOfPages;

            //Create a new layer
            for (int i = 1; i <= pageCount1; i++)
            {
                iTextSharp.text.Rectangle rect = reader1.GetPageSize(i);     

                PdfContentByte cb = stamper.GetOverContent(i);

                AcroFields Fields = stamper.AcroFields;

                cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);       

                Fields.SetField("Price", @"1323423345");

                Fields.SetFieldProperty("Price", "TEXTCOLOR", BaseColor.RED, null);

                string Img = path + "red_slash.jpg";

                iTextSharp.text.Image jpeg = Image.GetInstance(System.Drawing.Image.FromFile(Img), ImageFormat.Jpeg);

                float width = 100;

                float height = 200;

                jpeg.ScaleToFit(width, height); 

                jpeg.SetAbsolutePosition(210, 315);

                cb.AddImage(jpeg);             
            }             
        }
    }
}

在这里,我需要在“价格”字段上写下图像

0 个答案:

没有答案