Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> How to create and use styles
The following example shows you how to create and use styles.
import com.independentsoft.office.ExtendedBoolean; import com.independentsoft.office.word.BottomBorder; import com.independentsoft.office.word.LineSpacingRule; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.Spacing; import com.independentsoft.office.word.StandardBorderStyle; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.fonts.ThemeFont; import com.independentsoft.office.word.styles.Style; import com.independentsoft.office.word.styles.StyleDefinitions; import com.independentsoft.office.word.styles.StyleType; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); Style style1 = new Style(); style1.setID("Heading 1"); style1.setName("Heading 1"); style1.setType(StyleType.PARAGRAPH); style1.setPrimary(ExtendedBoolean.TRUE); style1.setUserInterfacePriority(9); style1.getParagraphProperties().setKeepNext(ExtendedBoolean.TRUE); style1.getParagraphProperties().setKeepLines(ExtendedBoolean.TRUE); style1.getParagraphProperties().setOutlineLevel(0); Spacing spacing1 = new Spacing(); spacing1.setAfter(0); spacing1.setBefore(480); style1.getParagraphProperties().setSpacing(spacing1); style1.getRunProperties().getFonts().setAsciiThemeFont(ThemeFont.MAJOR_HIGH_ANSI); style1.getRunProperties().getFonts().setEastAsiaThemeFont(ThemeFont.MAJOR_EAST_ASIA); style1.getRunProperties().getFonts().setHighAnsiThemeFont(ThemeFont.MAJOR_HIGH_ANSI); style1.getRunProperties().getFonts().setComplexScriptThemeFont(ThemeFont.MAJOR_COMPLEX_SCRIPT); style1.getRunProperties().setBold(ExtendedBoolean.TRUE); style1.getRunProperties().setComplexScriptBold(ExtendedBoolean.TRUE); style1.getRunProperties().setFontSize(28); //14 points style1.getRunProperties().setComplexScriptFontSize(28); //14 points Style style2 = new Style(); style2.setID("Title"); style2.setName("Title"); style2.setType(StyleType.PARAGRAPH); style2.setPrimary(ExtendedBoolean.TRUE); style2.setUserInterfacePriority(9); BottomBorder bottomBorder = new BottomBorder(StandardBorderStyle.SINGLE_LINE); bottomBorder.setSpace(4); bottomBorder.setWidth(8); style2.getParagraphProperties().setBottomBorder(bottomBorder); style2.getParagraphProperties().setIgnoreSpace(ExtendedBoolean.TRUE); Spacing spacing2 = new Spacing(); spacing2.setAfter(300); spacing2.setLine(240); spacing2.setLineRule(LineSpacingRule.AUTO); style2.getParagraphProperties().setSpacing(spacing2); style2.getRunProperties().getFonts().setAsciiThemeFont(ThemeFont.MAJOR_HIGH_ANSI); style2.getRunProperties().getFonts().setEastAsiaThemeFont(ThemeFont.MAJOR_EAST_ASIA); style2.getRunProperties().getFonts().setHighAnsiThemeFont(ThemeFont.MAJOR_HIGH_ANSI); style2.getRunProperties().getFonts().setComplexScriptThemeFont(ThemeFont.MAJOR_COMPLEX_SCRIPT); style2.getRunProperties().setSpacing(5); style2.getRunProperties().setFontKern(28); style2.getRunProperties().setFontSize(52); //26 points style2.getRunProperties().setComplexScriptFontSize(52); //26 points StyleDefinitions documentStyles = new StyleDefinitions(); documentStyles.getStyles().add(style1); documentStyles.getStyles().add(style2); doc.setStyleDefinitions(documentStyles); Run run1 = new Run(); run1.addText("Text with style \"Heading 1\""); Run run2 = new Run(); run2.addText("Text with style \"Title\""); Paragraph paragraph1 = new Paragraph(); paragraph1.setStyleName("Heading 1"); paragraph1.add(run1); Paragraph paragraph2 = new Paragraph(); paragraph2.setStyleName("Title"); paragraph2.add(run2); Paragraph emptyParagraph = new Paragraph(); doc.getBody().add(paragraph1); doc.getBody().add(emptyParagraph); doc.getBody().add(paragraph2); 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*