【问题标题】:saving modified data in pdf itextsharp将修改后的数据保存为 pdf itextsharp
【发布时间】:2017-07-28 06:14:48
【问题描述】:

我使用 itextsharp 创建了一个包含一些可编辑字段的 pdf,当调用该服务时会创建 pdf。但我面临的问题是,如果我要更改 pdf 中的任何内容并下载它,那么更改不会保存。我还想通过服务器端在新标签页中打开 pdf。

我使用的代码是:

public static String[] LANGUAGES_gc = { "English", "Math", "Science" };
    [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;
        PdfFormField field;
        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(300, 806, 370, 788);
      PushbuttonField button = new PushbuttonField(wri, _rect, "Buttons");
      button.BackgroundColor = new GrayColor(0.75f);
      button.BorderColor = GrayColor.GRAYBLACK;
      button.BorderWidth = 1;
      button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
      button.TextColor = GrayColor.GRAYBLACK ;
      button.FontSize = 12;
      button.Text = "Submit";
      //button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
      button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
      button.ProportionalIcon = true;
      button.IconHorizontalAdjustment = 0;

      field = button.Field;
      field.Action = PdfAction.JavaScript("this.showButtonState()", wri);
      wri.AddAnnotation(field);

    //}
    //return ms.ToArray();
        /*----------------------------------------------------*/
        wri.AddAnnotation(_radioGroup);
        wri.AddAnnotation(button.Field);
        cb = wri.DirectContent;
        doc.Close();
        HttpContext.Current.Response.Write(doc);
        HttpContext.Current.Response.End();
    }

有人可以建议我解决方案吗?

【问题讨论】:

    标签: itext asp.net-web-api2


    【解决方案1】:

    让我们在这里做一个类比,使用更简单的文件类型。假设您正在向用户显示一个文本文件。他们在本地 .txt 应用程序(记事本)中打开它并进行一些更改。

    您是否希望他们的更改自动传回您的服务器? 或者他们的变化被神奇地传播了?当然不是。 即使可以,这似乎是记事本必须提供的功能,而不是文件创建者应该做的事情。

    现在,碰巧的是,有一个特定标准的 pdf 文档可以精确地用于您的用例。本质上,该文档建立了与服务器的连接并进行同步。不过这个标准比较晦涩,支持的人不多。

    据我所知,没有任何 pdf 库(包括 iText)支持制作这样的文档。

    【讨论】:

      猜你喜欢
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多