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)支持製作這樣的文檔。