Heading1 style at Bookmark

Hi,

I need to put Heading1 style at perticular Bookmark…

Let me know is there any property or method available or Is there any approach to apply Heading1 style in the place of Bookmark??

Regards,

Srinu Dhulipalla

Hi

Thanks for your inquiry. I think you should use code like the following:

// Open document and create DocumentBuilder
Document doc = new Document(@"Test178\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move DocumentBuilder cursor to the bookmark
builder.MoveToBookmark("mybk");
// Set style of current paragraph
builder.CurrentParagraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
// Save output document
doc.Save(@"Test178\out.doc");

Hope this helps.

Best regards.

Hi Alexey,

Thanks for your response,

but i need to handle this dynamically, like, if the paragraph style above bookmark is Heading1, i should replace bookmark with Heading1

and the paragraph style above bookmark is Heading2, i should replace the bookmark with Heading2 style… like this… it continues…

Let me know the possible ways to solve this?

Regards,

Srinu Dhulipalla

Hi

Please attach sample document and expected output here. It is not quite clear for me what you would like to achieve. If you need to determine style of the paragraph where the particular bookmark is located, you can use the same technique as I demonstrated in the code example provided in the previous answer.

Best regards.

Hi Alexey,

Thanks for your response,

PFA for my sample template, containing two bookmarks called ‘SectionNumber’ and ‘SectionNumber1’,

‘SectionNumber’ is under Heading1(Introduction), so here i need to put Heading1 style paragraph and ‘SectionNumber1’ is under Heading2(sub-introduction), so here i need to put Heading2 style…

So i need to fill all the bookmarks like this… can u pls help me out in this issue to resolve…

Regards,

Srinu Dhulipalla

Hi

Thanks for your inquiry. Maybe the following code could be useful for you:

public void Test188()
{
    // Open document
    Document doc = new Document(@"Test188\SectionTest_Aspose.dot");
    // Create document buider
    DocumentBuilder builder = new DocumentBuilder(doc);
    foreach (Bookmark bk in doc.Range.Bookmarks)
    {
        // Move docuemnt builder cursor to the bookmark
        builder.MoveToBookmark(bk.Name);
        // set new style identifier
        builder.CurrentParagraph.ParagraphFormat.StyleIdentifier = GetHeading(builder.CurrentParagraph);
    }
    // Save output docuemnt
    doc.Save(@"Test188\out.doc");
}

/// 
/// Method returns any heading style of any paragraph above the node
/// if there are no headign paragraphs above the node Normal style is returned
/// 
private StyleIdentifier GetHeading(Node node)
{
    StyleIdentifier style = StyleIdentifier.Normal;
    Node currentNode = node;
    while (currentNode != null)
    {
        // check whether current node is paragraph.
        // If so we should che its style
        // and return headign style if current paragraph is heading paragraph
        if (currentNode.NodeType.Equals(NodeType.Paragraph))
        {
            Paragraph curretnPar = (Paragraph)currentNode;
            if (IsHeading(curretnPar.ParagraphFormat.StyleIdentifier))
            {
                style = curretnPar.ParagraphFormat.StyleIdentifier;
                break;
            }
        }
        // move to the previouse node
        currentNode = currentNode.PreviousPreOrder(node.Document);
    }
    return style;
}

/// 
/// Returns true if identifier is an identifier of heading paragraph
/// 
private bool IsHeading(StyleIdentifier style)
{
    switch (style)
    {
        case StyleIdentifier.Heading1:
        case StyleIdentifier.Heading2:
        case StyleIdentifier.Heading3:
        case StyleIdentifier.Heading4:
        case StyleIdentifier.Heading5:
        case StyleIdentifier.Heading6:
        case StyleIdentifier.Heading7:
        case StyleIdentifier.Heading8:
        case StyleIdentifier.Heading9:
            return true;
        default:
            return false;
    }
}

Hope this helps.

Best regards.

Hello.

I use the code given in your post here and my text becomes heading-styled, BUT when I open MS Word, select the heading and re-press the heading1 style button (which is highlighted already) that the text is supposed to be using already, the formatting changes (font size becomes bigger as it should be from the begining).

What I do in my code is to set the builder’s style identifier and then inset the new paragraph with pBuilder.Writeln(pText). Then I restore the old style identifier.

Is there sth I must do additionally to the following code:

builder.CurrentParagraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

Why isn’t the text style what it should be? It is already marked as heading1, so why is the formatting different?

It seeems that the bold, italic attributes are copied on my new text but the font name and size not.

Thanks,

Kostas

Hello.

I have foud what was wrong. I needed to clear the formatting:

StyleIdentifier pSi = sth;
Style lStyle = pBuilder.Document.Styles[pSi];
pBuilder.ParagraphFormat.ClearFormatting();
pBuilder.ParagraphFormat.Style.Font.ClearFormatting();
pBuilder.RowFormat.ClearFormatting();
pBuilder.Font.ClearFormatting(); // This saves the day…
// pBuilder.ParagraphFormat.StyleIdentifier = pSi;
pBuilder.ParagraphFormat.Style = lStyle;

https://forum.aspose.com/t/54158

Have a nice day!

Kostas

Hi Kostas,

Thanks for your inquiry. It is nice to hear from you that you have solved your problem. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.