วันอาทิตย์ที่ 5 กรกฎาคม พ.ศ. 2558

asp.net : วันที่แบบเต็ม ภาษาไทย

แสดงวันเดือนปี ภาษาไทย


ต้องการวันที่ปัจจุบัน
เช่น     "วันที่ 06 เดือน กรกฎาคม พุทธศักราช 2558"  
 
Code
Imports System.Globalization
 
Dim cultInfo As CultureInfo = New CultureInfo("th-TH")
Dim fmt As DateTimeFormatInfo = cultInfo.DateTimeFormat
 
'ต้องต่อ string เอา 
Dim strdate As String = "วันที่ " & Date.Now.ToString("dd") & " เดือน " & Date.Now.ToString("MMMM", fmt) & " พุทธศักราช " & Date.Now.ToString("yyyy", fmt)
 
 ข้อสังเกต
 
ใช้    Date.Now.ToString("format", Culture format )
 
"format" สามารถเป็นได้หลากหลายมาก 
วันที่  :  d=6, dd=06, ddd=พ, dddd=พุธ
เดือน   : M=7, MM=07, MMM=กค., MMMM=กรกฎาคม
ปี  :  yy=58, yyyy=2558
 
ตัวอย่างเช่น
- Date.Now.ToString("d/M/yy", fmt )   =>   6/7/58
- Date.Now.ToString("dd-MM-yyyy", fmt )   =>   06-07-2558
- Date.Now.ToString("MMMM", fmt )   =>  กรกฎาคม

เพิ่มเติม
https://msdn.microsoft.com/en-us/library/az4se3k1.aspx
 
 

วันพฤหัสบดีที่ 2 กรกฎาคม พ.ศ. 2558

asp.net : Insert Text to pdf (แทรกข้อความลงใน pdf)

แทรกขอความลงใน pdf ไฟล์โดยใช้ iTextsharp

เครื่องมือ : 

  1. Visual Stodio 2010 (2005->2015 คิดว่าได้หมด)
  2. iTextSharp Library
  3. ไฟล์ font THSarabun.ttf
  4. ไฟล์ pdf  ที่ต้องการเขียนข้อความลงไป (cert.pdf)

วิธีการ :
  1. download iTextsharp lib จาก http://sourceforge.net/projects/itextsharp/
  2. แตกไฟล์ หาไฟล์ชื่อว่า iTextsharp.dll
  3. เปิด VS สร้างโปรเจค vb web เปล่า และนำเข้าไฟล์ pdf ที่จะ insert ข้อความ(cert.pdf)
  4. นำเข้าใฟล์ iTextsharp.dll ไว้ในโฟลเดอร์ bin ของโปรเจค
  5. นำเข้าใฟล์ Font THSarabun.ttf ไว้ในโฟลเดอร์ fonts ภาษาไทยใช้ font แห่งชาติ จะไม่มีปัญหาสระซ้อน
  6. Add Reference เลือกไฟล์ /bin/iTextsharp.dll 
  7. add  new WebForm1
  8. ก็อบ sub  InsertTextToPdf() ข้างล่างวางในส่วน Code ของ WebForm1
  9. เรียก InsertTextToPdf() ใน Form Load ของ WebForm1
  10. สร้าง iFrame วางใน WebForm1 ดังนี้ 
<iframe src="/certificate.pdf" style="border-style: none; border-color: inherit; border-width: 0px; width: 900px; height: 700px ; "></iframe>


ส่วน import

     Imports System
     Imports System.IO
     Imports System.Xml
     Imports iTextSharp
     Imports iTextSharp.text
     Imports iTextSharp.text.pdf
     Imports iTextSharp.text.xml


    Private Sub InsertTextToPdf()

        'ไฟล์ที่จะ insert ข้อวาม
        Dim oldFile As String = Server.MapPath("~/cert.pdf")
        'ไฟล์ใหม่
        Dim newFile As String = Server.MapPath("~/certificate.pdf")

        Using pdfStream As Stream = New FileStream(oldFile, FileMode.Open)

            Using newpdfStream As Stream = New FileStream(newFile, FileMode.Create, FileAccess.ReadWrite)
                Dim pdfReader As New PdfReader(pdfStream)
                Dim pdfStamper As New PdfStamper(pdfReader, newpdfStream)
                Dim pdfContentByte__1 As PdfContentByte = pdfStamper.GetOverContent(1)

                'กำหนด Font สีและขนาด
                Dim baseFont__2 As BaseFont = BaseFont.CreateFont(Server.MapPath("/fonts/THSarabun.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
                pdfContentByte__1.SetColorFill(BaseColor.BLACK)
                pdfContentByte__1.SetFontAndSize(baseFont__2, 16)
                pdfContentByte__1.BeginText()

                'ข้อความและตำแหน่งที่จะเขียน  
                pdfContentByte__1.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Kevin Cheng - A Hong Kong actor", 200, 600, 0)
                pdfContentByte__1.EndText()
                pdfStamper.Close()
            End Using
        End Using

    End Sub


 Run โปรแกรมก็จะเห็น ข้อความในไฟล์ pdf  ตัว pdf แสดงฝังอยู่ในหน้าเว็บ


การประยุกต์ใช้
  • พิมพ์ใบประกาศนียบัตร ต่าง ๆ เอาข้อความหรือชื่อมาจากฐานข้อมูล
  • พิมพ์ข้อความจากฐานข้อมูลลงบนแบบฟอร์ม ที่มีอยู่แล้ว ใบเสร็จรับเงิน , invoice ฯ

สรุป
     ใช้ object Stream แบบ FileStream เก็บไฟล์เข้าหน่วยความจำ แล้วอ่านไฟล์จาก stream ด้วย pdfReader แล้วใช้ pdfstumper เขียนไฟล์
     มี Code ตัวอย่างการใช้ iTextSharp lib จากการ googling จำนวนมากต้องใช้เวลาพอสมควร ที่เจอตรงกับความต้องการ Code ส่วนใหญ่เป็น C# แต่ก็สามารถแปลงเป็น vb  มีหลายเว็บที่แปลงได้เช่น http://converter.telerik.com/

   
ตัวอย่าง การใข้งาน  iTextSharp

http://www.codeproject.com/Articles/686994/Create-Read-Advance-PDF-Report-using-iTextSharp-in

http://www.mikesdotnetting.com/article/80/create-pdfs-in-asp-net-getting-started-with-itextsharp

http://www.4guysfromrolla.com/articles/030911-1.aspx

http://www.itextpdf.com/book/chapter.php?id=1

http://www.c-sharpcorner.com/UploadFile/f2e803/basic-pdf-creation-using-itextsharp-part-i/

https://simpledotnetsolutions.wordpress.com/2012/04/08/itextsharp-few-c-examples/