Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java and PDF > Java Create PDF and Generate PDF files (PDFBox)



Clound SSD Virtual Server

Java Create PDF and Generate PDF files (PDFBox)

Java Create PDF and Generate PDF files ในการสร้าง PDF ด้วยภาษา Java ในกรณีที่ต้องการสร้าง PDF จะต้องหา Library ข้างนอกมาใช้ เพราะใน JDK ที่จะไม่มี Pacakge และ Class มาให้ใช้ และ Library ที่อยากจะแนะนำให้ใช้ก็คือ Apache PDFBox ซึ่งเป็น Library ที่ได้รับความนิยมและสามารถ Download ใช้งานได้ฟรี

Java Create PDF and Generate PDF files

Java Create PDF and Generate PDF files


ก่อนการใช้งานจะต้องทำการ Download ตัว Library ของ Apache PDFBox มาซะก่อน โดยสามารถ Download ใช้งานได้ฟรี ได้ที่

Apache PDFBox - A Java PDF Library


Java Create PDF and Generate PDF files

หลังจาก Download แล้วเราจะได้ไฟล์ jar ให้ Copy ไปไว้ในโฟเดอร์ lib ของ Project

Java Create PDF and Generate PDF files

คลิกขวาที่ Project เลือก Properties

Java Create PDF and Generate PDF files

ในส่วนของ Java Build Path ให้เลือก Add JARs...

Java Create PDF and Generate PDF files

เลือก Library ของไฟล์

Java Create PDF and Generate PDF files

ตอนนี้เราได้ Library ที่ร้องใช้เรียบร้อยแล้ว








Example ตัวอย่างการสร้าง PDF แบบง่าย ๆ ด้วย Java

MyClass.java
package com.java.myapp;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;


public class MyClass {

	public static void main(String[] args) {
		
		try {
	
			PDDocument document = new PDDocument();
			PDPage page = new PDPage();
			document.addPage( page );

			// Create a new font object selecting one of the PDF base fonts
			PDFont font = PDType1Font.HELVETICA_BOLD;

			// Start a new content stream which will "hold" the to be created content
			PDPageContentStream contentStream = new PDPageContentStream(document, page);

			// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
			contentStream.beginText();
			contentStream.setFont( font, 12 );
			contentStream.moveTextPositionByAmount( 100, 700 );
			contentStream.drawString( "Welcome to ThaiCreate.Com" );
			contentStream.endText();

			// Make sure that the content stream is closed:
			contentStream.close();

			// Save the results and ensure that the document is properly closed:
			document.save( "C:\\java\\myPDF.pdf");
			document.close();
			
			System.out.println("PDF Created Done.");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
        
	}
	
	
}

Output

Java Create PDF and Generate PDF files

ไฟล์ PDF ถูกสร้าง

Java Create PDF and Generate PDF files

ทดสอบการเขียนข้อความง่าย ๆ

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-26 11:51:40 / 2017-03-27 14:36:20
  Download : No files
 Sponsored Links / Related

 
Java Example PDF Report from Database
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่