2017-07-28 56 views
-2

我创建了一个使用itextsharp的pdf,其中包含一些可编辑的字段,当服务被调用时,pdf被创建。但是,我面临的问题是,如果我正在更改pdf中的任何内容并下载它,那么更改不会被保存。另外我想通过服务器端在新标签中打开PDF。我在用以pdf格式保存修改后的数据itextsharp

代码是:

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(); 
    } 

可有人建议我的解决方案?

回答

1

让我们用一个比较简单的文件类型来比喻一下。假设你正在向用户显示一个文本文件。他们在自己的原生.txt应用程序(记事本)中打开它并进行一些更改。

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

现在,正如您所见,存在一个特定的pdf文档标准,它恰好存在于您的用例中。本质上,文档建立到服务器的连接并进行同步。但是,这个标准比较模糊,没有很多观众支持它。

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