我想以pdf格式创建帐单收据。我不打算从头开始创建所有内容,而是打算用pdf创建一个模板文件,然后使用客户名称,金额等相关信息更新此模板pdf文件。
我打算在MS Word或Inkscape中创建模板文件(pdf)。知道我应该怎么做。
编辑:我知道如何从头开始创建它。但是如果我可以创建模板文件,我可以节省很多编码。由于
答案 0 :(得分:0)
您可以使用RazorEngine作为模板,然后使用iTextSharp将生成的HTML转换为PDF。
在您的获胜应用中,您可以嵌入IE并通过呈现结果HTML来显示比尔收据的打印预览。
答案 1 :(得分: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);
}