使用Itextsharp在pdf中添加按钮

时间:2017-07-19 13:12:05

标签: c# itext asp.net-web-api2

我正在尝试使用iTextSharp在Pdf中添加一个按钮,当单击该按钮时,我想调用带有修改数据的服务。我可以添加radiobuttons但无法添加Button。编码我使用的是:

public static String[] LANGUAGES_gc = { "English", "German", "Spanish" };
    [HttpGet]
    [ODataRoute("GetPdf")]
    public void DownloadPDF()
    {
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=Example.pdf");
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Document doc = new Document(iTextSharp.text.PageSize.A4, 10f, 10f, 100f, 0f);
        string pdfFilePath = HttpContext.Current.Server.MapPath(".") + "/PDFFiles";
        PdfWriter wri = PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);
        doc.Open();
        doc.AddAuthor("Test author");
        doc.AddCreationDate();
        PdfContentByte cb = wri.DirectContent;
        Font _bf = new Font(Font.FontFamily.HELVETICA, 6);
        PdfFormField _radioGroup = PdfFormField.CreateRadioButton(wri, true);
        _radioGroup.FieldName = "language_gc";
        Rectangle _rect;
        RadioCheckField _radioG;
        PdfFormField _radioField1;
        for (int i = 0; i < LANGUAGES_gc.Length; i++)
        {
            _rect = new Rectangle(46, 806 - i * 40, 60, 788 - i * 40);
            _radioG = new RadioCheckField(wri, _rect, null, LANGUAGES_gc[i]);
            _radioG.BackgroundColor = new GrayColor(0.8f);
            _radioG.BorderColor = GrayColor.BLACK;
            _radioG.CheckType = RadioCheckField.TYPE_CIRCLE;
            _radioField1 = _radioG.RadioField;
            _radioGroup.AddKid(_radioField1);              

            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 18)), 70, 790 - i * 40, 0);
        }
        /* Button */
        _rect = new Rectangle(46, 806 * 40, 60, 788 * 40);
        PushbuttonField button = new PushbuttonField(wri, _rect, "button");
        PdfAnnotation widget = button.Field;
        button.BackgroundColor = new GrayColor(0.75f);
        button.BorderColor = GrayColor.GRAYBLACK;
        button.BorderWidth = 1;
        button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
        button.TextColor = GrayColor.GRAYBLACK;
        button.FontSize = 11;
        button.Text = "Button";
        button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
        button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
        button.ProportionalIcon = true;
        button.IconHorizontalAdjustment = 0;
        /*----------------------------------------------------*/
        wri.AddAnnotation(_radioGroup);
        cb = wri.DirectContent;
        doc.Close();
        HttpContext.Current.Response.Write(doc);
        HttpContext.Current.Response.End();
    }

请让我知道我做错了什么。

0 个答案:

没有答案