النتائج 1 إلى 2 من 2

الموضوع: Dynamically Generating PDFs in .NET

  1. #1

    المشرف العام

    تاريخ التسجيل
    Jun 2008
    المشاركات
    4,392
    معدل تقييم المستوى
    1076

    Post Dynamically Generating PDFs in .NET


    End Google Ads 201810 - BS.net 01 -->
    Dynamically Generating PDFs in .NET


    It's perfectly possible to generate a PDF from scratch, using a library such as iTextSharp, a port of a free Java PDF library. However, it can be hard work defining all the code you need to generate the layout you're after, and impossible for someone to tweak the layout without going back to the developer.
    One alternative, using the same free library, is instead to design a PDF document in a WYSIWYG environment such as Adobe Designer, and define some dynamic fields within the document. Your code can load this PDF form, set the value of each of the fields, and output a flat PDF. This would enable you to, for instance, have an invoice PDF template defined, set a bunch of fields within the PDF, and output as a flat PDF document - without needing a load of code generating the PDF from scratch.
    The code itself is very straightforward. We're going to send the resulting output to the ASP.NET response stream, so we need to set the content-type and also a content-disposition header so that the result appears as a file download.
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-disposition", "attachment; filename=tokens.pdf");
    Note that we could just as easily be using a file stream, and would therefore replace the above with opening a stream to the appropriate file path. With that out of the way, we use a PdfReader object to load our existing PDF from the local file system; in this case, from ~/assets/form.pdf.
    PdfReader pdfReader = new PdfReader(Request.MapPath("~/assets/form.pdf"));
    Next we create a PdfStamper object, which will allow us to modify the form fields defined in the PDF and save the result. We pass it the PdfReader object representing the PDF file, and a stream we want the result sent to. Finally, we set the FormFlattening flag so that we get a flat PDF rather than allowing the fields to remain editable.
    PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
    pdfStamper.FormFlattening = true; // generate a flat PDF
    We can then set the dynamic fields defined within the PDF form, and close our pdfStamper object.
    AcroFields pdfForm = pdfStamper.AcroFields;
    pdfForm.SetField("InvoiceRef", "00000");
    pdfForm.SetField("DeliveryAddress", "Oxford Street, London");
    pdfForm.SetField("Email", "info@barakasoft.com");
    pdfStamper.Close();
    Closing the pdfStamper object results in the PDF being written to the stream we passed in the constructor, and we're all done. Simple!


  • #2
    تقنى جديد
    تاريخ التسجيل
    Feb 2009
    المشاركات
    1
    معدل تقييم المستوى
    0

    افتراضي رد: Dynamically Generating PDFs in .NET

    ممكن شرح اكثر
    انا اريد ان اضيف بوتون عند الضغط عليه يحفظ المقال بصيغة الpdfضروري لانه مشروع تخرجي ومو باقي غير اسبوع و انا اشتغل على asp.netو مستخدمين لغة c#
    بلييييييييييييييييييييييييييييز

  • معلومات الموضوع

    الأعضاء الذين يشاهدون هذا الموضوع

    الذين يشاهدون الموضوع الآن: 1 (0 من الأعضاء و 1 زائر)

    الكلمات الدلالية لهذا الموضوع

    مواقع النشر (المفضلة)

    ضوابط المشاركة

    • لا تستطيع إضافة مواضيع جديدة
    • لا تستطيع الرد على المواضيع
    • لا تستطيع إرفاق ملفات
    • لا تستطيع تعديل مشاركاتك
    •  
    "وَقُل رَّبِّ زِدْنِي عِلْمًا"
    أعلانات نصية أستضافة , ريسيلر - Best Hosting | BarakaSoft Web Solutions

    BarakaSoft PageRank RSS RSS 2.0 XML MAP HTML 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 C/C++ | Java | C# | Network | Database | OS | Linux | Windows | Hacker & Security | Photoshop | Flash | Web Development | Free Programs | Mobile App | Free Java Course | Latest Technical News | Internet Programs | Antiviurse Programs | Graphics Programs | Network Programs | Portable Programs | vb Forums Development | Forums Development | CMS(Joomla-nuke-wordpress-mkportal...) | Photo | Anime |