I am trying to render the proper style on the new inserted text paragraph or some text character runs, I am using the StyleIdentifier to render it , the following is my testing code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// this is for rendering the style to paragraph
builder.getFont().setStyleIdentifier(StyleIdentifier.BIBLIOGRAPHY); builder.getCurrentParagraph().getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT);
builder.write("the is test text for special style");
builder.writeln();
builder.getCurrentParagraph().getParagraphFormat().clearFormatting();
// this is for rendering the style on text character runs in paragraph
builder.write("the is the ");
builder.pushFont();
builder.getFont().setStyleIdentifier(StyleIdentifier.EMPHASIS);
builder.write("test text for rendering style");
builder.popFont();
builder.write(" for text character runs");
builder.writeln();
doc.save(".../.../newdoc.doc");
My issue is when render style on paragraph, some value of StyleIdentifer can not be found like StyleIdentifier.BIBLIOGRAPHY etc,
and the same occured when I render style on character runs. could you tell me how to tell apart which constant values of StyleIdentifier are set for paragraph style and which are set for text character run ?
thanks a lot
Hi
Thanks for your request. You can get the style and check its type to determine whether style is Paragraph or Character style:
// Get some style.
Style style = doc.Styles[StyleIdentifier.Heading1];
// Style with the specified StyleIdentifier can be not defined in the template.
// In this case style is null.
if (style != null)
{
// Check type of the style.
switch (style.Type)
{
case StyleType.Paragraph:
Console.WriteLine("This is Paragraph style.");
break;
case StyleType.Character:
Console.WriteLine("This is Character style.");
break;
case StyleType.Table:
Console.WriteLine("This is Table style.");
break;
case StyleType.List:
Console.WriteLine("This is List style.");
break;
}
}
Also, you should note that style with a specified StyleIdentifier can be not defined in the template. If you need to use this style, you can create your own template where this style is defined.
Best regards,
Thanks for your clear code sample, it is quite clear to identify the style, but I still have no idea about how to render some StyleIdentifier style like StyleIdentifier.QUOTE, etc.
Here is a sample doc created by OpenOffice, I set five different style for the five paragraphs(Illustration, Quote, Addressee, Marginalia, Default), Could you provide a sample code or solution of how to render the this five styles in the new empty doc ? this is how I am trying to do it but it doesn’t work:
// for rendering the style on each paragraph in a new empty doc
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getCurrentParagraph().getParagraphFormat().setStyleIdentifier(StyleIdentifier.QUOTE);
builder.write("the is test text for special style");
builder.writeln();
builder.getCurrentParagraph().getParagraphFormat().clearFormatting();
thanks.
Hi Matthew,
Thanks for your inquiry.
Please make the change to your code below and things will work as expected.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setStyle(doc.getStyles().get("Quote");
builder.write("the is test text for special style");
builder.writeln();
builder.getParagraphFormat().clearFormatting();
Some styles in your document are “User” styles and don’t have a corresponding StyleIdentifier. This is the case for the quote style, most likely OpenOffice does not mark this as an in-built style like MS Word would which is why the StyleIdentifier approach will not work.
In the styles you have in your document you can use StyleIdentifier.Normal to retrieve the “Default” style and StyleIdentifier.EnvelopeAddress to get the “Addressee” style. The other styles must be retrieved using the string overload.
Thanks,