使用iTextSharp的pdf文件,我想以PDF形式的比爾收據。我打算用pdf創建一個模板文件,然後用相關信息(如客戶名稱,金額支付等)更新此模板的pdf文件,而不是從頭開始創建所有內容。創建在C#
我打算創建模板文件(pdf)無論是在MS Word還是Inkscape中。任何想法我應該如何去做。
編輯:我知道如何從頭開始創建。但是如果我可以創建模板文件,我可以節省很多編碼。
感謝
使用iTextSharp的pdf文件,我想以PDF形式的比爾收據。我打算用pdf創建一個模板文件,然後用相關信息(如客戶名稱,金額支付等)更新此模板的pdf文件,而不是從頭開始創建所有內容。創建在C#
我打算創建模板文件(pdf)無論是在MS Word還是Inkscape中。任何想法我應該如何去做。
編輯:我知道如何從頭開始創建。但是如果我可以創建模板文件,我可以節省很多編碼。
感謝
您可以使用RazorEngine您的模板,然後使用iTextSharp的所得HTML轉換爲PDF。
在你贏應用程序,你可以嵌入IE瀏覽器,並通過渲染HTML結果顯示比爾收據打印預覽。
這是我在過去所做的那樣。 我使用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);
}
你希望它在Web或Windows? –
我正在建立一個桌面應用程序。 – Kiran