We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set Locale Independent Heading Style Identifiers in Word Document & Detect Built in Heading Paragraph Styles Java

I have a problem that creates a document with two new styles. Each line used a different header style. The document is created correctly but there is a problem. There is no way to tell which line has which style. This is not a problem for small files but if you have 4k lines the problem appears quickly.

When you open the new document in MSWord, the lines Aspose instered highlight the style information at the top of the string. The two lines I placed in the file do not show any style used.

How do you set a Style to be a Header? Style has an isHeader method but know way to set it.

private void testStyle(){

    Document doc = null;
    try {
        doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Create a paragraph style and specify some formatting for it.
        Style style = doc.getStyles().add(StyleType.PARAGRAPH, "Header-1");
        style.getFont().setSize(24);
        style.getFont().setName("Verdana");
        style.getParagraphFormat().setSpaceAfter(12);
        style.getParagraphFormat().setAlignment(0);
        style.getFont().setColor(Color.BLUE);

        // Change to a paragraph style that has no list formatting.
        builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
        builder.writeln("Hello World: Normal.");

        // Change the paragraph style to header 1 and write text.
        builder.getParagraphFormat().setStyle(style);
        builder.getParagraphFormat().setStyle(doc.getStyles().get("Header-1"));
        builder.writeln("Hello World: Header-1.");

        Style style2 = doc.getStyles().add(StyleType.PARAGRAPH,"Header-2");
        style2.getFont().setSize(14);
        style2.getFont().setName("Verdana");
        style2.getParagraphFormat().setSpaceAfter(12);
        style2.getParagraphFormat().setAlignment(0);
        style2.getFont().setColor(Color.RED);

        builder.getParagraphFormat().setStyle(doc.getStyles().get("Header-2"));
        builder.writeln("Hello World: Header-2.");

        Iterator<Style> stylesEnum = doc.getStyles().iterator();
        while (stylesEnum.hasNext()) {
            Style curStyle = stylesEnum.next();
            System.out.println(MessageFormat.format("Style name:\t\"{0}\", of type \"{1}\"", curStyle.getName(), curStyle.getType()));
            System.out.println(MessageFormat.format("\tSubsequent style:\t{0}", curStyle.getNextParagraphStyleName()));
            System.out.println(MessageFormat.format("\tIs heading:\t\t\t{0}", curStyle.isHeading()));
            System.out.println(MessageFormat.format("\tIs QuickStyle:\t\t{0}", curStyle.isQuickStyle()));

            Assert.assertEquals(curStyle.getDocument(), doc);
        }

        builder.getDocument().save( "/Users/admin/IdeaProjects/AsposeStyleTest/Header_1_format.doc");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@hoppyjr,

The following code will return true as the value of curStyle.isHeading() for the HEADING 1 and HEADING 2 styles.

Document doc = null;
try {
    doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
    Style style = builder.getParagraphFormat().getStyle();
    style.getFont().setSize(24);
    style.getFont().setName("Verdana");
    style.getParagraphFormat().setSpaceAfter(12);
    style.getParagraphFormat().setAlignment(0);
    style.getFont().setColor(Color.BLUE);

    // Change to a paragraph style that has no list formatting.
    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
    builder.writeln("Hello World: Normal.");

    // Change the paragraph style to header 1 and write text.
    builder.getParagraphFormat().setStyle(style);
    builder.writeln("Hello World: Header-1.");

    builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
    Style style2 = builder.getParagraphFormat().getStyle();
    style2.getFont().setSize(14);
    style2.getFont().setName("Verdana");
    style2.getParagraphFormat().setSpaceAfter(12);
    style2.getParagraphFormat().setAlignment(0);
    style2.getFont().setColor(Color.RED);

    builder.getParagraphFormat().setStyle(style2);
    builder.writeln("Hello World: Header-2.");

    Iterator<Style> stylesEnum = doc.getStyles().iterator();
    while (stylesEnum.hasNext()) {
        Style curStyle = stylesEnum.next();
        System.out.println(MessageFormat.format("Style name:\t\"{0}\", of type \"{1}\"", curStyle.getName(), curStyle.getType()));
        System.out.println(MessageFormat.format("\tSubsequent style:\t{0}", curStyle.getNextParagraphStyleName()));
        System.out.println(MessageFormat.format("\tIs heading:\t\t\t{0}", curStyle.isHeading()));
        System.out.println(MessageFormat.format("\tIs QuickStyle:\t\t{0}", curStyle.isQuickStyle()));

        System.out.println(curStyle.getDocument() == doc);
    }

    builder.getDocument().save("E:\\Temp\\Header_1_format.doc");

} catch (Exception e) {
    e.printStackTrace();
}

Hope, this helps in achieving what you are looking for.