Membuat file Rich Text Format menggunakan iText RtfWriter2


Ini posting pertama ku setelah resign dari sebuah software house di Jakarta yang menggunakan ColdFusion sebagai bahasa pemrogramman utamanya. Tapi, tetap aja masih penasaran dengan beberapa fitur yang kemarin belum sempat diutak-atik.

Beberapa hari yang lalu iseng-iseng buka direktori C:\ColdFusion9\lib, dan disitu ada file iText.jar, itu buat apaan ya? Setelah googling, file iText.jar itu biasanya dipake untuk membuat file pdf dan rtf, selanjutnya bisa dilihat di situsnya http://www.itextpdf.com/, tetapi untuk versi yang sekarang dukungan untuk membuat file rtf sudah di drop, baca ini. ColdFusion versi 9 menggunakan iText 2.1.0.


Lihat contoh di bawah ini untuk membuat file Rich Text Format (*.rtf) menggunakan iText RtfWriter2.

<!--- directory to save rtf file --->
<cfset currentDirectory = GetDirectoryFromPath(GetCurrentTemplatePath())>
<!--- rtf file --->
<cfset rtfFile = currentDirectory & "tscf.rtf">

<!--- image file --->
<cfset imageFile = currentDirectory & "google.png">

<!--- rtf document object --->
<cfset document = createObject("java", "com.lowagie.text.Document").init()>
<cfoutput>#document.getVersion()#</cfoutput>

<!--- instance --->
<cfset instance = createObject("java", "com.lowagie.text.rtf.RtfWriter2").getInstance(document, createObject("java", "java.io.FileOutputStream").init(rtfFile))>

<!--- open the document object --->
<cfset document.open()>

<!--- add a paragraph --->
<cfset paragraph = createObject("java", "com.lowagie.text.Paragraph").init("Hello World!")>

<cfset document.add(paragraph)>

<!--- add another paragraph --->
<cfset paragraph = createObject("java", "com.lowagie.text.Paragraph")>
<cfset paragraph.add("another paragraph.")>

<cfset document.add(paragraph)>

<!--- paragraph with style --->
<cfset styledFont = createObject("java", "com.lowagie.text.rtf.style.RtfFont").init("Arial", 10, 1)>
<cfset paragraph = createObject("java", "com.lowagie.text.Paragraph").init("This is a paragraph with style. ", styledFont)>
<cfset document.add(paragraph)>

<cfset chunkFont = createObject("java", "com.lowagie.text.rtf.style.RtfFont").init("Courier New", 10, 1)>
<cfset chunk = createObject("java", "com.lowagie.text.Chunk").init("Another styled. ", chunkFont)>

<cfset paragraph = createObject("java", "com.lowagie.text.Paragraph")>
<cfset paragraph.add(chunk)>

<cfset chunkFont = createObject("java", "com.lowagie.text.rtf.style.RtfFont").init("Comic Sans MS", 16, 2)>
<cfset chunk = createObject("java", "com.lowagie.text.Chunk").init("Another styled with Comix Sans MS font. ", chunkFont)>
<cfset paragraph.add(chunk)>

<cfset color = createObject("java", "java.awt.Color").RED>
<cfset chunkFont = createObject("java", "com.lowagie.text.rtf.style.RtfFont").init("Times New Roman", 12, 0, color)>
<cfset chunk = createObject("java", "com.lowagie.text.Chunk").init("The Reds.", chunkFont)>
<cfset paragraph.add(chunk)>

<cfset document.add(paragraph)>

<cfset rtfFont = createObject("java", "com.lowagie.text.rtf.style.RtfFont")>

<!--- table --->
<cfset color = createObject("java", "java.awt.Color").BLACK>
<cfset chunkFont = createObject("java", "com.lowagie.text.rtf.style.RtfFont").init("Arial", 12, 0, color)>
<cfset chunk = createObject("java", "com.lowagie.text.Chunk").init("Kolom pertama baris pertama", chunkFont)>

<cfset table = createObject("java", "com.lowagie.text.Table").init(3)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init(chunk)>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom kedua baris pertama")>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom ketiga baris pertama")>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom pertama baris kedua")>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom kedua baris kedua")>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom ketiga baris kedua")>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom pertama baris ketiga")>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom kedua baris ketiga")>
<cfset table.addCell(cell)>

<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom ketiga baris ketiga")>
<cfset table.addCell(cell)>

<cfset document.add(table)>

<!--- image --->
<cfset image = createObject("java", "com.lowagie.text.Image").getInstance(imageFile)>
<cfset document.add(image)>

<cfset image = createObject("java", "com.lowagie.text.Image").getInstance(imageFile)>
<cfset image.scaleToFit(400, 100)>
<cfset document.add(image)>

<cfset paragraph = createObject("java", "com.lowagie.text.Paragraph").init("")>
<cfset document.add(paragraph)>

<cfset table = createObject("java", "com.lowagie.text.Table").init(5)>
<cfset color = createObject("java", "java.awt.Color").BLUE>


<cfloop from="1" to="3" index="indexLoopRow">
	<cfloop from="1" to="5" index="indexLoopColumn">
		<cfset cell = createObject("java", "com.lowagie.text.Cell").init("Kolom #indexLoopColumn# baris #indexLoopRow#")>
		<cfset cell.setBorderColor(color)>
		<cfset table.addCell(cell)>
	</cfloop>
</cfloop>

<cfset document.add(table)>

<!--- close the document object --->
<cfset document.close()>
tscf-rtf

tscf-rtf

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: