Skip to content

nut077/PdfFreedom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Tutorial PdfFreedom

  • maven
<!-- https://mvnrepository.com/artifact/com.github.nut077/pdf-freedom -->
<dependency>
    <groupId>com.github.nut077</groupId>
    <artifactId>pdf-freedom</artifactId>
    <version>1.0.2</version>
</dependency>
  • รูปแบบการใช้งาน
    <table></table> คือ 1 ตาราง <tr></tr> คือ 1 แถว <td></td> คือ 1 column

    StringBuilder table = new StringBuilder();
    table.append("<table>");
      table.append("<tr>");
        table.append("<td>A</td>");
        table.append("<td>B</td>");
        table.append("<td>C</td>");
      table.append("</tr>");
    table.append("</table>");
    
    StringBuilder header = new StringBuilder();
    header.append("<table>");
      header.append("<tr>");
        header.append("<td>Header A</td>");
        header.append("<td>Header B</td>");
        header.append("<td>Header CC</td>");
      header.append("</tr>");
    header.append("</table>");
    
    /* ใช้งานแบบ offline
    parameter ตัวแรกคือที่อยู่ของไฟล์ที่จะสร้าง 
    ตัวที่สองคือชื่อของไฟล์ pdf 
    ตัวที่สามคือที่อยู่ของฟ้อนต์ ตัวที่สี่คือชื่อฟ้อนต์ 
    ตัวที่ห้าคือชื่อของ title
    ตัวที่หกคือข้อมูลที่จะเขียนลงในไฟล์ pdf 
    และตัวสุดท้ายคือ header ที่จะออกมาทุกแผ่นของไฟล์ จะมีหรือไม่มีก็ได้ */
    
    PdfFreedom pdfFreedom = new PdfFreedom("D://", "pdf", "D://", "THSarabun", "example pdf", table.toString(), header.toString());
    
    /* ใช้งานแบบ servlets ไฟล์ jsp
    parameter ตัวแรกคือ application 
    ตัวที่สองคือ response 
    ตัวที่สามคือ out
    ตัวที่สี่คือชื่อของฟอนต์ ฟ้อนต์ต้องอยู่ใน path -> FONTS/
    ตัวที่ห้าคือชื่อของไฟล์ pdf 
    ตัวที่หกคือข้อมูลที่จะเขียนลงในไฟล์ pdf 
    และตัวสุดท้ายคือ header ที่จะออกมาทุกแผ่นของไฟล์ จะมีหรือไม่มีก็ได้ */
    
    PdfFreedom pdfFreedom = new PdfFreedom(application, response, out, "tahoma", "example pdf", table.toString(), header.toString());
    
    pdfFreedom.write(); // สั่งให้เขียนไฟล์ 
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • กำหนดตำแหน่งของ header
    ใช้ tag <table-margin-top>ความสูงที่ห่างจากระยะขอบกระดาษ</table-margintop> โดยวางไว้ต่อจาก tag <table> ค่าเริ่มต้นคือ 40

    StringBuilder table = new StringBuilder();
    table.append("<table>");
      table.append("<tr>");
        table.append("<td>A</td>");
        table.append("<td>B</td>");
        table.append("<td>C</td>");
      table.append("</tr>");
    table.append("</table>");
    
    StringBuilder header = new StringBuilder();
    header.append("<table>");
      header.append("<table-margin-top>10</table-margintop>");
      header.append("<tr>");
        header.append("<td>Header A</td>");
        header.append("<td>Header B</td>");
        header.append("<td>Header CC</td>");
      header.append("</tr>");
    header.append("</table>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • กำหนดความกว้างของตาราง
    ใช้ tag <table-width>ขนาด 1-100</table-width> โดยวางไว้ต่อจาก tag <table> ค่าเริ่มต้นคือ 100

    table.append("<table>");
        table.append("<table-width>50</table-width>");
        table.append("<tr>");
            table.append("<td>width 50</td>");
        table.append("</tr>");
    table.append("</table>");
    table.append("<table>");
        table.append("<table-width>100</table-width>");
        table.append("<tr>");
            table.append("<td>width 100</td>");
        table.append("</tr>");
    table.append("</table>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ผสานเซลล์ แนวนอน
    ใช้ tag <colspan>ตัวเลขที่ต้องการ</colspan>

    table.append("<tr>");
        table.append("<td><colspan>2</colspan>colspan 2</td>");
        table.append("<td><colspan>3</colspan>colspan 3</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td>1</td>");
        table.append("<td>2</td>");
        table.append("<td>3</td>");
        table.append("<td>4</td>");
        table.append("<td>5</td>");
    table.append("</tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ผสานเซลล์ แนวตั้ง
    ใช้ tag <rowspan>ตัวเลขที่ต้องการ</rowspan>

    table.append("<tr>");
        table.append("<td><rowspan>2</rowspan>rowspan 2</td>");
        table.append("<td>A</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td>B</td>");
    table.append("</tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ขยายขนาดความกว้างของคอลัมน์
    ใช้ tag <width>ตัวเลขที่ต้องการ</width>

    table.append("<td><width>10</width>width 10</td>");
    table.append("<td><width>20</width>width 20</td>");
    table.append("<td><width>30</width>width 30</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ขยายขนาดความสูงของคอลัมน์
    ใช้ tag <height>ตัวเลขที่ต้องการ</height>

    table.append("<tr>");
        table.append("<td><height>500</height>height 500</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td><height>600</height>height 600</td>");
    table.append("</tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • การจัดรูปแบบ
    ใช้ tag <format>รูปแบบตาม list ข้างล่าง</format> ค่าเริ่มต้นคือ border-center

    • left
    • left-middle
    • left-top
    • center
    • center-middle
    • center-top
    • right
    • right -middle
    • right -top
    • border-left
    • border-left-middle
    • border-left-top
    • border-center
    • border-center-middle
    • border-center-top
    • border-right
    • border-right-middle
    • border-right-top
    table.append("<tr>");
        table.append("<td><format>left</format>left</td>");
        table.append("<td><format>left-middle</format>left-middle</td>");
        table.append("<td><format>left-top</format>left-top</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td><format>center</format>center</td>");
        table.append("<td><format>center-middle</format>center-middle</td>");
        table.append("<td><format>center-top</format>center-top</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td><format>right</format>right</td>");
        table.append("<td><format>right-middle</format>right-middle</td>");
        table.append("<td><format>right-top</format>right-top</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td><format>border-left</format>border-left</td>");
        table.append("<td><format>border-left-middle</format>border-left-middle</td>");
        table.append("<td><format>border-left-top</format>border-left-top</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td><format>border-center</format>border-center</td>");
        table.append("<td><format>border-center-middle</format>border-center-middle</td>");
        table.append("<td><format>border-center-top</format>border-center-top</td>");
    table.append("</tr>");
    table.append("<tr>");
        table.append("<td><format>border-right</format>border-right</td>");
        table.append("<td><format>border-right-middle</format>border-right-middle</td>");
        table.append("<td><format>border-right-top</format>border-right-top</td>");
    table.append("</tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    กำหนดเส้นขอบให้แต่ละมุม
    ใช้ tag <border-left>true or false</border-left> สำหรับเส้นขอบซ้าย ค่าเริ่มต้นคือ false
    ใช้ tag <border-right>true or false</border-right> สำหรับเส้นขอบขวา ค่าเริ่มต้นคือ false
    ใช้ tag <border-top>true or false</border-top> สำหรับเส้นขอบบน ค่าเริ่มต้นคือ false
    ใช้ tag <border-bottom>true or false</border-bottom> สำหรับเส้นขอบล่าง ค่าเริ่มต้นคือ false

    table.append("<tr><td><border-left>true</border-left>border-left</td></tr>");
    table.append("<tr><td><border-right>true</border-right>border-right</td></tr>");
    table.append("<tr><td><border-top>true</border-top>border-top</td></tr>");
    table.append("<tr><td><border-bottom>true</border-bottom>border-bottom</td></tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    ถ้าต้องการให้ขอบเป็นเส้นจุดให้เพิ่ม -dotted ต่อท้าย

    table.append("<tr><td><border-left-dotted>true</border-left-dotted>border-left-dotted</td></tr>");
    table.append("<tr><td><border-right-dotted>true</border-right-dotted>border-right-dotted</td></tr>");
    table.append("<tr><td><border-top-dotted>true</border-top-dotted>border-top-dotted</td></tr>");
    table.append("<tr><td><border-bottom-dotted>true</border-bottom-dotted>border-bottom-dotted</td></tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    ถ้าต้องการให้ขอบเป็นเส้นปะให้เพิ่ม -dashed ต่อท้าย

    table.append("<tr><td><border-left-dashed>true</border-left-dashed>border-left-dashed</td></tr>");
    table.append("<tr><td><border-right-dashed>true</border-right-dashed>border-right-dashed</td></tr>");
    table.append("<tr><td><border-top-dashed>true</border-top-dashed>border-top-dashed</td></tr>");
    table.append("<tr><td><border-bottom-dashed>true</border-bottom-dashed>border-bottom-dashed</td></tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • กำหนดระยะห่างระหว่างข้อความกับขอบของคอลัมน์
    ใช้ tag ตามรูปแบบด้านล่าง โดยระยะห่างอิงกับการจัดรูปแบบด้วย ตัวอย่างใช้รูปแบบ border-center ขนาดเริ่มต้นคือ 5

    • <padding>ขนาด</padding> // ระยะห่างทั้งหมด บน ขวา ล่าง ซ้าย
    • <padding-left>ขนาด</padding-left> // ระยะห่างระหว่างข้อความกับด้านซ้ายของคอลัมน์
    • <padding-right>ขนาด</padding-right> // ระยะห่างระหว่างข้อความกับด้านขวาของคอลัมน์
    • <padding-top>ขนาด</padding-top> // ระยะห่างระหว่างข้อความกับด้านบนของคอลัมน์
    • <padding-bottom>ขนาด</padding-bottom> // ระยะห่างระหว่างข้อความกับด้านล่างของคอลัมน์
    table.append("<tr><td><padding>50</padding>padding 50</td></tr>");
    table.append("<tr><td><padding-left>100</padding-left>padding left 100</td></tr>");
    table.append("<tr><td><padding-right>100</padding-right>padding right 100</td></tr>");
    table.append("<tr><td><padding-top>10</padding-top>padding top 10</td></tr>");
    table.append("<tr><td><padding-bottom>2</padding-bottom>padding bottom 2</td></tr>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • เปลี่ยนขนาด font
    ใช้ tag <font-size>ขนาด</font-size> ค่าเริ่มต้นคือ 14

    table.append("<td><font-size>14</font-size>font default</td>");
    table.append("<td><font-size>20</font-size>font size 20</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ตัวอักษรตัวหนา
    ใช้ tag <b>true or false</b> ค่าเริ่มต้นคือ false

    table.append("<td>font normal</td>");
    table.append("<td><b>true</b>font bold</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ตัวอักษรตัวเอียง
    ใช้ tag <i>true or false</i> ค่าเริ่มต้นคือ false

    table.append("<td>font normal</td>");
    table.append("<td><i>true</i>font italic</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ขีดเส้นใต้
    ใช้ tag <u>true or false</u> ค่าเริ่มต้นคือ false

    table.append("<td>font normal</td>");
    table.append("<td><u>true</u>font underline</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • ขีดเส้นใต้สองเส้น
    ใช้ tag <double-line>true or false</double-line> ค่าเริ่มต้นคือ false

    table.append("<td>font normal</td>");
    table.append("<td><double-line>true</double-line>font 2 underline</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • สี
    ใช้ tag <background-color>รูปแบบตาม list ข้างล่าง</background-color> เมื่อต้องการเปลี่ยนสีพื้นหลัง ค่าเริ่มต้นคือ ไม่มีสี
    ใช้ tag <font-color>รูปแบบตาม list ข้างล่าง</font-color> เมื่อต้องการเปลี่ยนสีตัวอักษร ค่าเริ่มต้นคือ black

    • white
    • light_gray
    • gray
    • dark_gray
    • black
    • red
    • pink
    • orange
    • yellow
    • green
    • magenta
    • cyan
    • blue

    Capture.png

    เปลี่ยนสีพื้นหลัง

    table.append("<td><background-color>orange</background-color>orange</td>");
    table.append("<td><background-color>green</background-color>green</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    เปลี่ยนสีพื้นหลังแบบกำหนดสีเอง
    ใช้ tag <background-color-new>กำหนดเลขสี red,green,blue</background-color-new>

    table.append("<td><background-color-new>0,204,255</background-color-new>new color 0,204,255</td>");
    table.append("<td><background-color-new>204,255,51</background-color-new>new color 204,255,51</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    เปลี่ยนสีตัวอักษร

    table.append("<td><font-color>red</font-color>red</td>");
    table.append("<td><font-color>blue</font-color>blue</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    เปลี่ยนสีตัวอักษรแบบกำหนดสีเอง
    ใช้ tag <font-color-new>กำหนดเลขสี red,green,blue</font-color-new>

    table.append("<td><font-color-new>0,204,255</font-color-new>new color 0,204,255</td>");
    table.append("<td><font-color-new>102,0,204</font-color-new>new color 102,0,204</td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • เพิ่มรูปภาพ
    ใช้ tag <img>ที่อยู่ของรูปภาพ</img>

    table.append("<td><img>D://image.jpg</img></td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    ปรับขนาดรูปภาพแบบเปอร์เซ็นต์
    ใช้ tag <img-width-percent>1-100</img-width-percent> ค่าเริ่มต้นคือ 100
    ตัวอย่างปรับเป็น 50 เปอร์เซ็นต์

    table.append("<td><img>D://image.jpg</img><img-width-percent>50</img-width-percent></td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    ปรับขนาดความกว้างความสูง
    ใช้ tag <img-width>ขนาดความกว้าง</img-width>
    ใช้ tag <img-height>ขนาดความสูง</img-height>
    ตัวอย่างเช่น กำหนดความกว้างเท่ากับ 250 และความสูงเท่ากับ 150

    table.append("<td><img>D://image.jpg</img><img-width>250</img-width><img-height>150</img-height></td>");
    ผลลัพธ์ที่ได้คือ

    Capture.png

  • กำหนดค่าเริ่ม

    PdfFreedom pdfFreedom = new PdfFreedom("D://", "pdf", "D://", "THSarabun", "example pdf", table.toString(), header.toString());
    // เรียกใช้ก่อน method write()
    pdfFreedom.write();
    • กำหนดกระดาษเป็นแนวนอน setHorizontal(); ตัวอย่าง pdfFreedom.setHorizontal();
    • กำหนดระยะห่างระหว่างขอบกระดาษ setMarginDocument(36, 36, 36, 36); บน,ขวา,ล่าง,ซ้าย
      ตัวอย่าง pdfFreedom.setMarginDocument(36, 36, 36, 36);
      หรือจะกำหนดแยก
    pdfFreedom.setMarginLeftDocument(36);
    pdfFreedom.setMarginRightDocument(36);
    pdfFreedom.setMarginTopDocument(36);
    pdfFreedom.setMarginBottomDocument(36);
    • กำหนดระยะห่างระหว่างข้อความกับขอบของคอลัมน์ setPadding(ขนาด); ตัวอย่าง pdfFreedom.setPadding(10);
    • กำหนดขนาดตัวอักษร setFontSize(ขนาด); ตัวอย่าง pdfFreedom.setFontSize(20);
    • กำหนดการจัดรูปแบบ setDefaultFormat(รูปแบบ); ตัวอย่าง pdfFreedom.setDefaultFormat("left");
    • กำหนดให้แสดงจำนวนหน้า setPageNumberShow(); ต้องใส่ข้อมูลที่ header ด้วย ตัวเลขถึงจะแสดง
      ถ้าไม่มีข้อมูลที่ header สามารถใส่เป็นค่าเปล่าๆได้ เช่น
    StringBuilder header = new StringBuilder();
    header.append("<table>");
    header.append("<tr>");
    header.append("<td><format>left</format></td>");
    header.append("</tr>");
    header.append("</table>");

    ตัวอย่าง pdfFreedom.setPageNumberShow();

    StringBuilder table = new StringBuilder();
    table.append("<table>");
        table.append("<tr>");
            table.append("<td>show page number</td>");
        table.append("</tr>");
    table.append("</table>");
    
    StringBuilder header = new StringBuilder();
    header.append("<table>");
        header.append("<tr>");
            header.append("<td><format>left</format></td>");
        header.append("</tr>");
    header.append("</table>");
    
    PdfFreedom pdfFreedom = new PdfFreedom("D://", "pdf", "D://", "THSarabun", "example pdf", table.toString(), header.toString());
    pdfFreedom.setPageNumberShow();
    pdfFreedom.write();
    ผลลัพธ์ที่ได้คือ

    Capture.png

    เปลี่ยนคำว่า “หน้าที่” กับ “/” setPageNumberText("แทนคำว่าหน้าที่", "แทนคำว่า /");
    ตัวอย่าง

    pdfFreedom.setPageNumberText("page", "of");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    เปลี่ยนตำแหน่งของการแสดงจำนวนหน้า setPageNumberAlign("left or center or right"); ค่าเริ่มต้นคือ right

    pdfFreedom.setPageNumberAlign("left");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    pdfFreedom.setPageNumberAlign("center");
    ผลลัพธ์ที่ได้คือ

    Capture.png

    เปลี่ยนตำแหน่งของการแสดงจำนวนหน้าไปไว้ตำแหน่งล่างสุด setPageNumberToBottom() ตัวอย่าง pdfFreedom.setPageNumberToBottom();

    ผลลัพธ์ที่ได้คือ

    Capture.png

    เปลี่ยนขนาดตัวอักษร setPageNumberFontSize(ขนาด) ตัวอย่าง pdfFreedom.setPageNumberFontSize(20);

    ผลลัพธ์ที่ได้คือ

    Capture.png

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages