Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Open Source Projects
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Other Open Source Projects
creating word document
bryan lim
Ranch Hand
Posts: 140
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi guys,
i followed this
http://faq.javaranch.com/java/CreateWordDocument
to create
word
document.
But the setBold and setItalic doesn't seems to work.
Anybody has any idea? Is there better solution out there to create word document?
Thanks!
Bryan
Martijn Verburg
author
Posts: 3285
13
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can you post your relevant code and the versions of the API you are using?
Cheers, Martijn,
Twitter
.
bryan lim
Ranch Hand
Posts: 140
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am using 3.6 20091214 version
import java.io.*; import org.apache.poi.hpsf.CustomProperties; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.*; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class CreateWordDoc { public static void main (String[] args) throws Exception { // POI apparently can't create a document from scratch, // so we need an existing empty dummy document POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("/Desktop/empty.doc")); HWPFDocument doc = new HWPFDocument(fs); // centered paragraph with large font size Range range = doc.getRange(); Paragraph par1 = range.insertAfter(new ParagraphProperties(), 0); par1.setSpacingAfter(200); par1.setJustification((byte) 1); // justification: 0=left, 1=center, 2=right, 3=left and right CharacterRun run1 = par1.insertAfter("one"); run1.setFontSize(2 * 18); // font size: twice the point size // paragraph with bold typeface Paragraph par2 = run1.insertAfter(new ParagraphProperties(), 0); par2.setSpacingAfter(200); CharacterRun run2 = par2.insertAfter("two two two two two two two two two two two two two"); run2.setBold(true); // paragraph with italic typeface and a line indent in the first line Paragraph par3 = run2.insertAfter(new ParagraphProperties(), 0); par3.setFirstLineIndent(200); par3.setSpacingAfter(200); CharacterRun run3 = par3.insertAfter("three three three three three three three three three " + "three three three three three three three three three three three three three three " + "three three three three three three three three three three three three three three"); run3.setItalic(true); // add a custom document property (needs POI 3.5; POI 3.2 doesn't save custom properties) DocumentSummaryInformation dsi = doc.getDocumentSummaryInformation(); CustomProperties cp = dsi.getCustomProperties(); if (cp == null) cp = new CustomProperties(); cp.put("myProperty", "foo bar baz"); dsi.setCustomProperties(cp); doc.write(new FileOutputStream("/Desktop/new.doc")); System.out.println("Completed!"); } }
Martijn Verburg
author
Posts: 3285
13
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Have you checked the examples on their website for this usage? I'm no expert in POI, but perhaps you need to re-order when you make a CharacterRun bold.
Cheers, Martijn,
Twitter
.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
empty lines in a document.
How to Create Word Document from simple java file
Writing a Microsoft Word Doc using java
Word Object in Jsp
Download to word and making word non-editable
More...