0

我想创建一个 pdf 格式的账单收据。我不打算从头开始创建所有内容,而是打算在 pdf 中创建一个模板文件,然后使用客户姓名、支付金额等相关信息更新此模板 pdf 文件。

我打算在 MS Word 或 Inkscape 中创建模板文件(pdf)。知道我应该怎么做。

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

谢谢

4

2 回答 2

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);
}
于 2012-11-02T13:37:29.710 回答
0

您可以将RazorEngine用于您的模板,然后使用 iTextSharp 将生成的 HTML 转换为 PDF。

在您的 win 应用程序中,您可以嵌入 IE 并通过呈现结果 HTML 来显示账单收据的打印预览。

于 2012-11-02T12:34:40.663 回答