Applying margins to entire word document after opening using Aspose

Hi,
When I generate the .rtf file from BI publisher, By default it is shifting all the contents by 0.08 inches towards right. This is a bug with BI publisher.
I want to adjust the formating for entire document when I open this .rtf file using Aspose by shifting all the contents towards left by 0.08 inches. How can I do that?
Please note that I want to shift back all the content relatively by 0.08 inches towards left so that the relative position of each and every paragraph, lists, tables etc is still maintained similar to original .rtf file. Please assist.

Hi there,

Thanks for your inquiry. Please use PageSetup.LeftMargin property as shown in following code snippet to achieve your requirements. This property returns or sets the distance (in points) between the left edge of the page and the left boundary of the body text.

Document doc = new Document(MyDir + "in.rtf");

foreach (Section section in doc.Sections)
{
    // section.PageSetup.LeftMargin = 50;
    section.PageSetup.LeftMargin = section.PageSetup.LeftMargin - 5.759;// 0.08 inch
}

If you still face any issue, please share following detail for investigation purposes.

  • Please attach your input RTF document.
  • Please attach your target output document showing the desired behavior. You can
    use Microsoft Word to create your target document.

I will investigate as to how you are expecting your final document be generated like.

Hi Manzoor,
The suggested solution is not producing the output as expected. Please find attached the input .rtf file and expected output .doc file. Please verify and assist.
In the InputFile.rtf file , all the contents are shifted to right by 0.083 inches.
In the expectedOutput.doc, All the contents are shifted back to left relatively by 0.083 inches.

Hi there,

Thanks for your inquiry. In your case, I suggest you please read the members of PageSetup and ParagraphFormat class from here:
https://reference.aspose.com/words/net/aspose.words/pagesetup/
https://reference.aspose.com/words/net/aspose.words/paragraphformat/

Please use the following code to set the left/right margins of page and left indent for paragraphs. Hope this helps you.

var doc = new Document(MyDir + "Input_File.rtf");

foreach (Section section in doc.Sections)
{
    section.PageSetup.LeftMargin = 48.23;
    section.PageSetup.RightMargin = 53.99;
}
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    // sets the value (in points) that represents the left indent for paragraph. 
    para.ParagraphFormat.LeftIndent = para.ParagraphFormat.LeftIndent - 5.759;
    if (para.ParagraphFormat.RightIndent > 0)
        para.ParagraphFormat.RightIndent = para.ParagraphFormat.RightIndent - 5.759;
    // Deletes all tab stop positions. 
    if (para.ParagraphFormat.TabStops.Count > 0)
        para.ParagraphFormat.TabStops.Clear();
}
doc.Save(MyDir + "Out.rtf");

Moreover, please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/applying-formatting/

Hi Manzoor,
Thanks for your inputs. The suggested solution helped me to set the page left/right margins and each paragraphs left/right indents correctly.
But, Still I have one outstanding issue with tables. Similar to normal paragraphs, All the tables and texts content in each of the table cells is also get shifted to right by 5.759 points ie, 0.08 inches.
So, For all the tables in the document, I need to make following changes after opening it using Aspose. How can I do that.

  1. Move all the tables to left by 0.08 inches. ie, The Table’s Indent from left: property should be reduced by 5.579 points from its current value.
  2. Move all the texts in each of the table cells to left by 0.08 inches. Ie, I need to reduce the left Indent for the table cells txts by 5.759 points from its current value.

Hi there,

Thanks for your inquiry.

Please use the Table.LeftIndent to get or set the value that represents the left indent of the table.

Use CellFormat.LeftPadding property get or set the amount of space (in points) to add to the left of the contents of cell.

Please let us know if you have any more queries.

Hi Manzoor,
I have a rtf document in which all the paragraphs have a left indent of 0.08 inches which is equal to 5.759 points. Input file is attached.
However, When I open this document in Aspose, the left indent values are not coming accurately.
For eg:
In the below code, The para.ParagraphFormat.LeftIndent value is coming as 5.5 Points which is 0.0763888888888889 inch instead of 5.759 points which is 0.08 inches. Due to this I am not able apply the indentation correctly.

private const double INDENT_SIZE = 5.759; //0.08 inches 
Document _pAsposeDocument = new Document("EG_Input.rtf");
foreach (Paragraph para in _pAsposeDocument.GetChildNodes(NodeType.Paragraph, true))
{
    // sets the value (in points) that represents the left indent for paragraph. 
    if (para.ParagraphFormat.LeftIndent >= INDENT_SIZE) //Issue: the left indent value is coming as 5.5 instead of 5.579
        para.ParagraphFormat.LeftIndent -= INDENT_SIZE;
    if (para.ParagraphFormat.RightIndent >= INDENT_SIZE)
        para.ParagraphFormat.RightIndent -= INDENT_SIZE;
}

Why is this difference? How can I get the correct value.

Hi there,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.1.0) from here and let us know how it goes on your side.

Please execute the following code example at your side. The left indent value is correct. MS Word and Aspose.Words return 0.2 cm value for first paragraph (having text) and 0.19 cm for second paragraph. Please check the attached images for these value for first two paragraphs.

Document _pAsposeDocument = new Document(MyDir + "EG_Input.rtf");

foreach (Paragraph para
in _pAsposeDocument.GetChildNodes(NodeType.Paragraph, true))
{
if (para.ToString(SaveFormat.Text).Trim().Length

0. // 1 point = 0.0352777778 cm


Console.WriteLine(para.ToString(SaveFormat.Text) + "
– " + (para.ParagraphFormat.LeftIndent *
0.0352777778).ToString());
}

Hi Manzoor,
I am doing the following in my program:

  1. operning the .rtf file in Aspose.word. This rtf file is having some start and end tags which needs to be replaced with correct styles in .dotx file. Since this .dotx file has list styles and it cannot be copied to another document, I am copying the .rtf content to .dotx file itslef as suggested by you in earlier posts.
  2. Applying the page margins, paragraph formatting and indent positioning to entire document.
  3. Replacing some of the texts between start and end tags with proper styles from .dotx file.
  4. Adding the TOC content to document.
    ISSUE: After updating the TOC content, The formatting of the (margins, paragraph formatting etc) TOC page is not coming correctly. Please see the the output file SPD_Aspose_output.doc
    The issue is described in TocFormattingIssue.png.
    I want the TOC content to be updated as shown in the SPD_Aspose_output_ExpectedTOC.doc.
    What is causing this issue. How can I get the expected TOC output. Please assist.
    All files are attached in SPD.console_Aspose.zip file.

Hi there,

Thanks for your inquiry. I have modified the ImportCustomStylesAndApplyForSPD method. Please check the following highlighted code below. The modified code clear the existing TabStops and add new TabStops for TOC filed.

Please let us know if you have any more queries.

public static void ImportCustomStylesAndApplyForSPD()
{
    /*STEP 1 - Apply Styles */
    // 1. Load an existing Word document in memory
    Document _pAsposeDocument = new Document("./../../SPD_Input_forAspose.rtf");
    // 2.set the full file path of the customStyles file related to the document
    Document CustomStylesDoc = new Document("./../../SPD_Styles.dotx");
    // apend the BIP document to .dox file
    _pAsposeDocument.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
    CustomStylesDoc.AppendDocument(_pAsposeDocument, ImportFormatMode.KeepSourceFormatting);
    // assign the joined document which has both the content and custom styles into main document _pAsposeDocument
    _pAsposeDocument = CustomStylesDoc;
    // 2.a Apply the margins
    ApplyMargins(_pAsposeDocument);
    // Get the custom styles for SPD document
    List<string> lstCustomStylesNames = GetCustomStylesForSPD();
    // 2.b Apply custom Styles to texts that exists between special Start and End tags. - Paragraph Styling
    NodeCollection paragraphs2 = _pAsposeDocument.GetChildNodes(NodeType.Paragraph, true);
    foreach (String custStyleName in lstCustomStylesNames)
    {
        // Construct the start and end tag.
        String StartTagName = "Start-" + custStyleName;
        String EndTagName = "End-" + custStyleName;
        // Get the actual customStyle name.
        // remove the "Style*-" from the customStyleName. 
        int firstDashIndex = custStyleName.IndexOf('-');
        String StyleName = custStyleName.Substring(firstDashIndex + 1);
        /*Apply the customStyle for document paragraphs */
        foreach (Paragraph para in paragraphs2)
        {
            if (para.GetText().Contains(StartTagName))
            {
                para.ParagraphFormat.ClearFormatting();
                para.ParagraphFormat.Style = _pAsposeDocument.Styles[StyleName];
            }
        }
    }
    // 3. Remove the Start and EndTags wrt to Paragraph custom Styles, , If present in the document.
    foreach (String custStyleName in lstCustomStylesNames)
    {
        // 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);
    }
    /*STEP 2 - Insert TOC */
    // 1.Create a document builder to insert content with into document.
    DocumentBuilder builder = new DocumentBuilder(_pAsposeDocument);
    // Insert a table of contents at the 2nd page of the document. 2nd page is already blank in the original doc
    builder.MoveTo(_pAsposeDocument.Sections[2].Body.LastParagraph);
    // Insert the TOC - up to 5 headings level is set as of now.
    builder.InsertTableOfContents("\\o \"1-1\" \\h \\z \\u");
    Style toc1 = _pAsposeDocument.Styles[StyleIdentifier.Toc1];
    Style toc2 = _pAsposeDocument.Styles[StyleIdentifier.Toc2];
    Style toc3 = _pAsposeDocument.Styles[StyleIdentifier.Toc3];
    Style toc4 = _pAsposeDocument.Styles[StyleIdentifier.Toc4];
    Style[] tocStyles = { toc1, toc2, toc3, toc4 };
    foreach (Style style in tocStyles)
    {
        // To incrise distance between right side and numbers we need to decrease right tabstop.
        // To achieve this we need to clear old tabstop and write new.
        // Also if you do not need tab leader (dots inbetween) you can specify TabLeader.None.
        style.ParagraphFormat.TabStops.Clear();
        style.ParagraphFormat.TabStops.Add(500, Aspose.Words.TabAlignment.Right, TabLeader.Dots);
    }
    _pAsposeDocument.UpdateFields();
    // 8. Save the modified document.
    _pAsposeDocument.Save("./../../SPD_Aspose_output.doc");
}