Reading custom styles from .DOTX file and then applying to Text between special tags

Hi Awais Hafeez,

I did some more analysis and logically grouped the paragraph styles and char styles and then applied them. The major mistake that I did earlier was clearing the paragraph formatting while applying the char styles. It was removing all the previously applied paragraph styles… so we were seeing many differences.

Now, I am applying all the paragraph first and then followed by the char styles… most of the issues that I reported earlier are fixed now. The only issue that I see is wrt to indentation of the bullets and other paragraph texts. Please have a look at it.

Also, Let me know How can I set the page indent to 0 for all pages of a document before applying any of my paragraph/char styles. I will be testing my huge output document for any other issues, and will report the issues in detail later. You can assist me later on that. Time being you dont have to work on my initial issues except the indentaion issues.

Please find attached the modified console application with I/O files.

Hi Komal,

Thanks for your inquiry and sorry for the delayed response. The list paragraphs in question are actually formatted with ‘Bullets Heavy’ style. To apply the desired paragraph indentation, you can either specify indentation inside that style or use the direct formatting method as follows:

//7. Remove the Start and EndTags wrt to Char custom Styles, If present in the document.
foreach (String custStyleName in lstCharCustomStyleNames)
{
    //Construct the start and end tag.
    String StartTagName = "Start-" + custStyleName;
    String EndTagName = "End-" + custStyleName;
    _pAsposeDocument.Range.Replace(StartTagName, "", false, false);
    _pAsposeDocument.Range.Replace(EndTagName, "", false, false);
}
// Apply indentation to lists contained inside Tables
paragraphs = _pAsposeDocument.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
    if (para.IsListItem && para.GetAncestor(NodeType.Table) != null)
        para.ParagraphFormat.LeftIndent = 0.50 * 72;
//8. Save the modified document.
_pAsposeDocument.Save("./../../EEComOutput_Aspose.doc");

Komal:

Also, Let me know How can I set the page indent to 0 for all pages of a document before applying any of my paragraph/char styles. I will be testing my huge output document for any other issues, and will report the issues in detail later. You can assist me later on that. Time being you dont have to work on my initial issues except the indentaion issues.

Please use the following code to be able to set the distance (in points) between the left edge of the pages and the left boundary of the body text.

foreach (Section section in _pAsposeDocument.Sections)
    section.PageSetup.LeftMargin = 0;

I hope, this helps.

Best regards,

Hi Hafeez,

Thanks for the input.

  1. Please let me know how can I set the page left margin/indent similar to attached word document OR expected_Doc_Margins.png file. I want to have the first line indent OR Hangling indent and left indent at 0 position. but there should be some white space towards left the paragraph text similar to atatched doc.

When I used the below code…

foreach (Section section in _pAsposeDocument.Sections)
    section.PageSetup.LeftMargin = 0;

I am not getting the allignment correctly. Please see the attached Wrong_ErrorMargin.png

  1. Also, In the attached .doc file, I have the footer with some text. I need to search this text and then apply the custom style similar to How I am applying for other body text as explained in my earlier emails.

Issue: I am trying to read the paragraph texts that are present with-in the footer…but, I am not able to read this particular paragraph which has this text “Start-Style12-Captiva ID029420001End-Style12-Captiva ID”.

Pleaes let me know , How can I read this paragraph. If I can read this paragraph, then I can apply the style easily.

I am using the below code.

The para.ToString(SaveFormat.Text) is not returning the text that I am looking for( “Start-Style12-Captiva ID029420001End-Style12-Captiva ID”.) any sections of the footer.

if (custStyleName.Contains("HeaderFooter")) /* Header and Footer Paragrapch styling */
{
    //Apply the customStyle for headerFooter paragraphs
    foreach (Section section in _pAsposeDocument.Sections)
    {
        HeaderFooter pf = section.HeadersFooters[HeaderFooterType.FooterFirst];
        if (pf != null)
        {
            foreach (Paragraph para in pf.Paragraphs)
            {
                if (para.ToString(SaveFormat.Text).Contains(StartTagName))
                {
                    para.ParagraphFormat.ClearFormatting();
                    para.ParagraphFormat.Style = _pAsposeDocument.Styles[StyleName];
                }
            }
        }
    }
}

Hi Komal,

Thanks for your inquiry.

  1. You can achieve this by adjusting the left indentation of each Document Element e.g.Paragraphs, Tables, Lists etc. Please try run the following code snippet; this code adjusts the left indentation of Paragraphs and Tables as per your needs:
// Adjust left indentation of Paragraphs that are outside the Tables
foreach (Paragraph para in paragraphs)
    if (!para.IsListItem && para.GetAncestor(NodeType.Table) == null)
        para.ParagraphFormat.LeftIndent = -0.5 * 72;
// Adjust left indentation of Tables
foreach (Aspose.Words.Tables.Table table in _pAsposeDocument.GetChildNodes(NodeType.Table, true))
    table.LeftIndent = -0.5 * 72;
  1. The problem occurs because the Paragraph in question is not an immediate child of FooterFirst node; instead, it is contained inside a Table. Please see the following code:
// .................
// .................
HeaderFooter pf = section.HeadersFooters[HeaderFooterType.FooterFirst];
if (pf != null)
{
    foreach (Paragraph para in pf.GetChildNodes(NodeType.Paragraph, true))
    {
        if (para.ToString(SaveFormat.Text).Contains("Start-Style12"))
        {
        }
// ...
// ...

I hope, this helps.

Best regards,

Hi,

I want to apply Black color to all the content or texts in the document. This is similar to pressing Ctrl+A and choosing black font in MS WORD.

How can I do it? Please let me know.

I am using the below code, but it is not working.

// Adjust left indentation of Paragraphs that are outside the TablesNodeCollection paragraphs = _pAsposeDocument.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
    if (!para.IsListItem && para.GetAncestor(NodeType.Table) == null)
        para.ParagraphFormat.Style.Font.Color = Color.Black;
}

Hi Komal,

Thanks for your inquiry. If you would like to change font of the entire document, you should loop through all nodes and change font. You can use DocumentVisitor to achieve this. Here is code example:

Document doc = new Document(@"C:\Temp\in.doc");
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save(@"C:\Temp\out.doc");
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)
    {
        font.Name = mNewFont;
        font.Color = Color.Blue;
    }
    //Font by default
    private string mNewFont = "Arial";
}

I hope, this helps.

Best regards,

Hi,

The workaround suggested for WORDSNET-8413 issues is not reliable in all scenarios for us. The reason is in some of our input document, the Left indent and First line indent will not be at 0.0 position. So, We need to preserve the position for left indent and first line indent when we actually open this document in aspose.

We observered that, The left Indent value for the headings is coming correctly. But, the First line indent value is incorrect. Because of this we are seeing the misallignment in the output content.

Also, The paragraph marker is added to the document when we opened in the Aspose, But this was not present in the input .rtf file.

For eg: For the attached document AA_WithoutChanges.rtf, when we open it in Aspose, The below line should have left indent as 6.0 and first indent as 6.0 similar to original document. But, when debugged it’s giving left indent as 6.0 and first line indent as -36.0 which is wrong. I hope that this is the reason for misallignment when we apply the styles.

Start-Style01-Cust-AA-Heading1PaasN INFORMATIONEnd-Style01-Cust-AA-Heading1

/*Construct the start and end tag. */
startTagName = "Start-" + custStyleName; //eg: **Style01-Cust-AA-Heading1**
endTagName = "End-" + custStyleName;
/* Get the actual customStyle name. Remove the "Style*-" or "HeaderFooter" from the customStyleName. */
int firstDashIndex = custStyleName.IndexOf('-');
styleName = custStyleName.Substring(firstDashIndex + 1);
/*Apply the customStyle for document paragraphs */
foreach (Paragraph para in paragraphs)
{
    if (para.ToString(SaveFormat.Text).Contains(startTagName))
    {
        //para.ParagraphFormat.ClearFormatting(); // I dont want to use this statement.Instead want to retain the original formatting present for the paragraph
        //debugging 
        //double leftIndent = para.ParagraphFormat.LeftIndent;
        //double FirstLineIndent = para.ParagraphFormat.FirstLineIndent;
        para.ParagraphFormat.Style = _pAsposeDocument.Styles[styleName];
    }
}
_pAsposeDocument.Range.Replace(startTagName, "", false, false);
_pAsposeDocument.Range.Replace(endTagName, "", false, false);

Hi,

Any updates on the 517788 in reply to 474732 which I requested on 01/06/2014.

Hi Komal,

Thanks for your inquiry. To ensure a timely and accurate response, it would be great if you please create a standalone runnable simple application (for example a Console Application project) that helps me reproduce your problem on my end and attach it here for testing. As soon as you get this simple application ready for me, I’ll start investigation into your issue and log an issue in our issue tracking system. Also, please attach the Aspose.Words generated output Word document showing the undesired behavior here for our reference. Thanks for your cooperation.

Best regards,