Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Create simple letter
The following example shows you how to create simple business letter.
import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.WordDocument; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); Run run1 = new Run(); run1.addText("[Your Name]"); run1.addCarriageReturn(); run1.addText("[Street Address]"); run1.addCarriageReturn(); run1.addText("[City, ST ZIP Code]"); run1.addCarriageReturn(); run1.addText("September 25, 2006"); run1.setFontSize(24); //12 points Run run2 = new Run(); run2.addText("[Recipient Name]"); run2.addCarriageReturn(); run2.addText("[Title]"); run2.addCarriageReturn(); run2.addText("[Company Name]"); run2.addCarriageReturn(); run2.addText("[Street Address]"); run2.addCarriageReturn(); run2.addText("[City, ST ZIP Code]"); run2.setFontSize(24); //12 points Run run3 = new Run(); run3.addText("Dear [Recipient Name]"); run3.setFontSize(24); //12 points Run run4 = new Run(); run4.addText("Due to an oversight on our part, this month’s payment, in the amount of $[__] and due on [date], was mailed just today. Our account number is [account number]. Please forgive our inattention. If you have any questions, please contact me at [phone number]."); run4.setFontSize(24); //12 points Run run5 = new Run(); run5.addText("Sincerely,"); run5.setFontSize(24); //12 points Run run6 = new Run(); run6.addText("[Your Name]"); run6.addCarriageReturn(); run6.addText("[Title]"); run6.setFontSize(24); //12 points Paragraph emptyParagraph = new Paragraph(); Paragraph paragraph1 = new Paragraph(); paragraph1.add(run1); Paragraph paragraph2 = new Paragraph(); paragraph2.add(run2); Paragraph paragraph3 = new Paragraph(); paragraph3.add(run3); Paragraph paragraph4 = new Paragraph(); paragraph4.add(run4); Paragraph paragraph5 = new Paragraph(); paragraph5.add(run5); Paragraph paragraph6 = new Paragraph(); paragraph6.add(run6); doc.getBody().add(paragraph1); doc.getBody().add(emptyParagraph); doc.getBody().add(paragraph2); doc.getBody().add(emptyParagraph); doc.getBody().add(paragraph3); doc.getBody().add(paragraph4); doc.getBody().add(paragraph5); doc.getBody().add(emptyParagraph); doc.getBody().add(paragraph6); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*