Add and remove paragraph

Hi,

I have a situation where i have to show some of data of word document. How can i achieve this.

Ex - I have a word document which contains 20 lines. Now i want to remove 5th and 7th line only.

In some another case i want to hide 10th, 11th and 18th. So i mean to say that according to our selection i want to display or hide line on word document. Which is the best possible way to implement this.

Thanks

Hi Shivam,

Thanks for your inquiry. The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

Please check “DocumentLayoutHelper” example project in Aspose.Words for .NET examples repository at GitHub. You can use this example project to get the text of specific line number.

After getting the text of specific line, please use Range.Replace method to replace the text with empty string. Please refer to the following article.

Find and Replace

Hope this helps you. Please let us know if you have any more queries.

Hi,

I checked your find and replace example. but i want to hide complete line and i don’t know what is in that line present. I am sending you a sample document in which i want to hide 3rd and 5th point.

We don’t know what text does line contains.

But we can provide some unique identity to each line like bookmark or paragraph etc.

Please check this document and tell me some feasible solution.

Thanks

Hi Shivam,

Thanks for your inquiry. If those points always belong to a List, then you can hide 3rd and 5th point Paragraphs by using the following sample code:

Document doc = new Document(MyDir + @"Sample+of+Word+document.docx");
doc.UpdateListLabels();
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.IsListItem)
    {
        if (para.ListLabel.LabelValue == 3 || para.ListLabel.LabelValue == 5)
        {
            para.Accept(new FontChanger());
        }
    }
}
doc.Save(MyDir + @"17.5.docx");
class FontChanger : DocumentVisitor
{
    /// 
    /// Called when a FieldEnd node is encountered in the document.
    /// 
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        ResetFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }

    /// 
    /// Called when a FieldSeparator node is encountered in the document.
    /// 
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        ResetFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }

    /// 
    /// Called when a FieldStart node is encountered in the document.
    /// 
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        ResetFont(fieldStart.Font);
        return VisitorAction.Continue;
    }

    /// 
    /// Called when a Footnote end is encountered in the document.
    /// 
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ResetFont(footnote.Font);
        return VisitorAction.Continue;
    }

    /// 
    /// Called when a FormField node is encountered in the document.
    /// 
    public override VisitorAction VisitFormField(FormField formField)
    {
        ResetFont(formField.Font);
        return VisitorAction.Continue;
    }

    /// 
    /// Called when a Paragraph end is encountered in the document.
    /// 
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ResetFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }

    /// 
    /// Called when a Run node is encountered in the document.
    /// 
    public override VisitorAction VisitRun(Run run)
    {
        ResetFont(run.Font);
        return VisitorAction.Continue;
    }

    /// 
    /// Called when a SpecialChar is encountered in the document.
    /// 
    public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
    {
        ResetFont(specialChar.Font);
        return VisitorAction.Continue;
    }

    private void ResetFont(Aspose.Words.Font font)
    {
        // Add your font changing code here
        font.Hidden = mHidden;
    }

    private bool mHidden = true;
}

Hope, this helps.

Best regards,