2012-11-02 34 views
0

使用iTextSharp的pdf文件,我想以PDF形式的比尔收据。我打算用pdf创建一个模板文件,然后用相关信息(如客户名称,金额支付等)更新此模板的pdf文件,而不是从头开始创建所有内容。创建在C#

我打算创建模板文件(pdf)无论是在MS Word还是Inkscape中。任何想法我应该如何去做。

编辑:我知道如何从头开始创建。但是如果我可以创建模板文件,我可以节省很多编码。

感谢

+0

你希望它在Web或Windows? –

+0

我正在建立一个桌面应用程序。 – Kiran

回答

0

您可以使用RazorEngine您的模板,然后使用iTextSharp的所得HTML转换为PDF。

在你赢应用程序,你可以嵌入IE浏览器,并通过渲染HTML结果显示比尔收据打印预览。

0

这是我在过去所做的那样。 我使用itext sharp并使用pdf压模。

/// <summary> 
     /// Updates the PDF document. From the class that you send it. It will match up any property names to any fields in the PDF. 
     /// if no properties are found then it will check the custom attribute PdfFieldName to see if the Name is the same in this attribute. 
     /// if so then it will map that property still. 
     /// Example: 
     /// PDF Field name: "TextBox Name" 
     /// 
     /// [PdfFieldName("TextBox Name")] 
     /// public string TextBoxName { get; set; } 
     /// 
     /// Since the Property name is TextBoxName it would not be mapped however since the attribute PdfFieldName does match it wil be mapped. 
     /// </summary> 
     /// <typeparam name="T"></typeparam> 
     /// <param name="oldPdfPath">The old PDF path.</param> 
     /// <param name="newPdfPath">The new PDF path.</param> 
     /// <param name="genericClass">The generic class.</param> 
     /// <param name="usePdfFieldNameAttribute"> </param> 
     public static void UpdatePdfDocument<T>(string oldPdfPath, string newPdfPath, T genericClass, bool usePdfFieldNameAttribute) 
     { 
      PdfReader pdfReader = new PdfReader(oldPdfPath); 
      using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newPdfPath, FileMode.Create))) 
      { 
       AcroFields pdfFormFields = pdfStamper.AcroFields; 
       var props = from p in typeof(T).GetProperties() 
          let attr = p.GetCustomAttributes(typeof(PdfFieldName), true) 
          where attr.Length == 1 
          select new { Property = p, Attribute = attr.First() as PdfFieldName }; 

       PropertyInfo[] properties = typeof(T).GetProperties(); 

       // set form pdfFormFields 
       foreach (string field in pdfReader.AcroFields.Fields.Select(de => de.Key)) 
       { 
        PropertyInfo pi = properties.FirstOrDefault(p => p.Name.ToUpper() == field.ToUpper()); 
        if (pi != null) 
         pdfFormFields.SetField(field, pi.GetValue(genericClass, null).ToString()); 
        else if (props != null && usePdfFieldNameAttribute) 
        { 
         var result = props.FirstOrDefault(p => p.Attribute.Name.ToUpper() == field.ToUpper()); 
         if (result != null) 
         { 
          pi = result.Property; 
          pdfFormFields.SetField(field, pi.GetValue(genericClass, null).ToString()); 
         } 
        } 
       } 

       // flatten the form to remove editting options, set it to false 
       // to leave the form open to subsequent manual edits 
       pdfStamper.FormFlattening = true; 

       // close the pdf 
       pdfStamper.Close(); 
       // close the pdfreader 
       pdfReader.Close(); 
      } 
     } 

然后在这里调用这个类是一些例子。

[System.AttributeUsage(System.AttributeTargets.Property)] 
public class PdfFieldName : System.Attribute 
{ 
    public string Name; 

    public PdfFieldName(string name) 
    { 
     Name = name; 
    } 
} 

public class test 
{ 
    [PdfFieldName("TextBox Name")] 
    public string TextBoxName { get; set; } 
} 

private static void Main(string[] args) 
{ 

    Dictionary<string, string> fields = new Dictionary<string, string> {{"TextBox Name", "Natan"}}; 
    test genericClass = new test() {TextBoxName = "Nathan"}; 
    UpdatePdfDocument(OLD_PDF_DOCUMENT_PATH, NEW_PDF_DOCUMENT_PATH, genericClass, true); 
}