iTextSharp Fill Pdf Form Image Field

荒凉一梦 提交于 2019-12-24 09:26:57

问题


I created a pdf form with Acrobat DC 2015. I have a image field on it. I fill text field succesfully. But I don't know how to fill image field. Do you help me?

    private static void FillPdfForm()
    {
        // Original File
        const string pdfTemplate = @"pdf\form.pdf";
        // New file which will be created after fillin PDF
        var newFile = @"pdf\FilledCV.PDF";
        var pdfReader = new PdfReader(pdfTemplate);
        var pdfStamper = new PdfStamper(pdfReader, new FileStream(
            newFile, FileMode.Create));
        var pdfFormFields = pdfStamper.AcroFields;

        // So one of our fields in PDF is FullName I am filling it with my full name
        pdfFormFields.SetFieldProperty("01", "textsize", 8f, null);
        pdfFormFields.SetField("01", "Example");

        // flatten the form to remove editting options, set it to false
        // to leave the form open to subsequent manual edits

        foreach (var de in pdfReader.AcroFields.Fields)
        {
            pdfFormFields.SetFieldProperty(de.Key.ToString(),
                                       "setfflags",
                                        PdfFormField.FF_READ_ONLY,
                                        null);
        }

        pdfStamper.FormFlattening = false;
        pdfStamper.FormFlattening = false;
        pdfStamper.Close();
    }

回答1:


I fixed it.

    private static void FillPdfForm()
    {
        const string pdfTemplate = @"pdf\form.pdf";
        var newFile = @"pdf\FilledCV.PDF";
        var pdfReader = new PdfReader(pdfTemplate);
        var pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
        var pdfFormFields = pdfStamper.AcroFields;

        string TestImage = @"pdf\test.jpg";
        PushbuttonField ad = pdfFormFields.GetNewPushbuttonFromField("08");
        ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
        ad.ProportionalIcon = true;
        ad.Image = Image.GetInstance(TestImage);
        pdfFormFields.ReplacePushbuttonField("08", ad.Field);


        pdfFormFields.SetFieldProperty("01", "textsize", 8f, null);
        pdfFormFields.SetField("01", "Example");
        foreach (var de in pdfReader.AcroFields.Fields)
        {
            pdfFormFields.SetFieldProperty(de.Key,"setfflags",PdfFormField.FF_READ_ONLY,null);
        }
        pdfStamper.FormFlattening = false;
        pdfStamper.Close();
    }


来源:https://stackoverflow.com/questions/44794670/itextsharp-fill-pdf-form-image-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!