2013-06-24 50 views
0

我試圖從FDF將數據保存到一個PDFTemplate,在我WPF應用保存PDF文件。免費庫導入FDF成PDF

所以,情況是這樣的。我有一個PDFTemplate.pdf它充當模板並具有佔位符(或字段)。現在我生成這個FDF文件親語法,又包含所有必需的PDFTemplate要填寫的字段名。此外,這FDF包含了PDFTemaplte也文件路徑,這樣開放時,知道使用哪個PDF

現在,當嘗試並雙擊該FDF,它打開Adob​​er Acrobat Reader軟件並顯示PDFTemplate與填充。數據,但使用文件菜單我無法保存此文件,因爲它說這個文件將被保存而沒有數據。

我想知道是否有可能到FDF數據導入PDF並保存不使用THRID方組件。

另外,如果要做到這一點非常困難,對於一個免費的圖書館來說可能的解決方案是什麼?

我剛剛意識到,iTextSharp不是免費的商業應用程序。

回答

1

我已經能夠實現這個使用另一個庫PDFSharp

它與iTextSharp的工作方式有些類似,除了iTextSharp中的一些地方更好,更易於使用。我張貼的代碼,以防有人想要做類似的事情:

//Create a copy of the original PDF file from source 
//to the destination location 
File.Copy(formLocation, outputFileNameAndPath, true); 

//Open the newly created PDF file 
using (var pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(
        outputFileNameAndPath, 
        PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify)) 
{ 
    //Get the fields from the PDF into which the data 
    //is supposed to be inserted 
    var pdfFields = pdfDoc.AcroForm.Fields; 

    //To allow appearance of the fields 
    if (pdfDoc.AcroForm.Elements.ContainsKey("/NeedAppearances") == false) 
    { 
     pdfDoc.AcroForm.Elements.Add(
      "/NeedAppearances", 
      new PdfSharp.Pdf.PdfBoolean(true)); 
    } 
    else 
    { 
     pdfDoc.AcroForm.Elements["/NeedAppearances"] = 
      new PdfSharp.Pdf.PdfBoolean(true); 
    } 

    //To set the readonly flags for fields to their original values 
    bool flag = false; 

    //Iterate through the fields from PDF 
    for (int i = 0; i < pdfFields.Count(); i++) 
    { 
     try 
     { 
      //Get the current PDF field 
      var pdfField = pdfFields[i]; 

      flag = pdfField.ReadOnly; 

      //Check if it is readonly and make it false 
      if (pdfField.ReadOnly) 
      { 
       pdfField.ReadOnly = false; 
      } 

      pdfField.Value = new PdfSharp.Pdf.PdfString(
          fdfDataDictionary.Where(
          p => p.Key == pdfField.Name) 
          .FirstOrDefault().Value); 

      //Set the Readonly flag back to the field 
      pdfField.ReadOnly = flag; 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ERROR_FILE_WRITE_FAILURE + ex.Message); 
     } 
    } 

    //Save the PDF to the output destination 
    pdfDoc.Save(outputFileNameAndPath);     
    pdfDoc.Close(); 
} 
+0

想知道的可能性,這個代碼將工作,如果fdf文件包含annot而不是字段? –