Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Nov 23, 2022 7:16 am

Hi I need help with formatting indentation of paragraph
I have code which will replace text with a paragraph
I want this to be aligned and have indent

Code: Select all
doc.Replace(item.PLACEHOLDER_NUM, value, true, true);


                if (item.PLACEHOLDER_NUM == "<19_BILL_TYPE_DESC>")
                {
                    Section sec = doc.Sections[0];
                    Paragraph para = sec.Paragraphs[0];
                    para.Format.SetLeftIndent(14);
                    para.Format.SetRightIndent(14);
                }


after text is replaced with para, i want it to be indented on both sides

how do I select the para that doc.Replace just added and add indentation to it.

suryaks2002
 
Posts: 17
Joined: Wed Nov 09, 2022 7:57 am

Wed Nov 23, 2022 9:56 am

Hello,

Thanks for your inquiry.
According to the message you provided, I can’t exactly investigate your issue. To help us reproduce your issue and work out a solution for you, please offer the following message. Thanks for your assistance in advance.

1) Your input word document, you can attach here or send it to us via email ([email protected]).
2) Your full test code that can reproduce your issue.
3) The word document you expected after replacing.
4) Application type, such as Console App, .NET Framework 4.8.
5) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Nov 23, 2022 10:55 am

Can you share the code which I can use to access the section of page that was added recently. From that I want to access the paragraph and format it.

suryaks2002
 
Posts: 17
Joined: Wed Nov 09, 2022 7:57 am

Wed Nov 23, 2022 11:06 am

Have a look at this code, I have placeholders in a word doc, I use loop to replace each placeholder with text

Same way I want to replace <19_BILL_DESCRIPTION> placeholder with paragraph, once the replacement happens
I want to access the paragraph and apply right and left indent to it, to have it aligned with other parts of PDF.
I want to know how to access the paragraph and do this, once it is added to doc.
Code: Select all
  foreach (var item in fields)
            {
                int count = 0;
                string value = string.IsNullOrEmpty(item.VALUE) ? string.Empty : item.VALUE;

                //Add spaces for bill agency section to maintain alignment
                if (item.PLACEHOLDER_NUM == "<1_BA_REGION_CD>" && regionCode)
                {
                    string replaceText = value ;
                    replaceText = replaceText.PadRight(18);
                    doc.Replace(item.PLACEHOLDER_NUM, replaceText, true, true);
                    regionCode = false;
                }

                //Remove new lines and extra spaces from paragraph
                if(item.PLACEHOLDER_NUM == "<19_BILL_TYPE_DESC>")
                {
                    item.VALUE = Regex.Replace(item.VALUE, @"\s+", "");
                }

                doc.Replace(item.PLACEHOLDER_NUM, value, true, true);


                if (item.PLACEHOLDER_NUM == "<19_BILL_TYPE_DESC>")
                {
                    Section sec = doc.Sections[0];
                    Paragraph para = sec.Paragraphs[0];
                    para.Format.SetLeftIndent(14);
                    para.Format.SetRightIndent(14);
                }

                TextSelection[] selection = doc.FindAllString(value, true, true);

                if (!string.IsNullOrEmpty(value) && selection != null)
                {
                    TextRange range = selection.FirstOrDefault().GetAsOneRange();
                    if (selection.Count() > 1)
                    {
                        count = selection.Count() - 1;
                        range = selection[count].GetAsOneRange();
                    }
                    if (item.BOLD == Constants.YES)
                    {
                        range.CharacterFormat.Bold = true;
                    }
                    if (item.ITALIC == Constants.YES)
                    {
                        range.CharacterFormat.Italic = true;
                    }
                    if (item.UNDERLINE == Constants.YES)
                    {
                        range.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
                    }
                }
            }

suryaks2002
 
Posts: 17
Joined: Wed Nov 09, 2022 7:57 am

Thu Nov 24, 2022 2:48 am

Hello,

Thanks for your more sharing.
There are two methods to achieve your requirement:
1) You can first format the paragraph where the old text is, than call the “ReplaceText” method. I put the code snippet below and attach my test input file for your reference.
Code: Select all
//Load the word document
            Document doc = new Document();
            doc.LoadFromFile(@"../../data/testPlaceholder.docx");


            string replaceText = "The second paragraph: Spire.Doc for .NET is a totally independent .NET Word class library which doesn't require Microsoft Office installed on system.";
            string oldText = "<19_BILL_TYPE_DESC>";


            //Find the old text
            TextSelection[] textSelections = doc.FindAllString(oldText, false, true);

            //Set the indent for the new paragraph
            foreach (TextSelection selection in textSelections)
            {
                TextRange[] textRanges = selection.GetRanges();

                foreach (TextRange range in textRanges)
                {
                    Paragraph para = range.OwnerParagraph;
                    para.Format.SetLeftIndent(14);
                    para.Format.SetRightIndent(14);
                }
            }

            //Replacing text
            doc.Replace(oldText, replaceText, true, true);

            //Save the result document
            doc.SaveToFile(@"../../output/result1.docx", Spire.Doc.FileFormat.Docx);

2) You can first call the “ReplaceText” method, than format the paragraph where the new text is. The follow is my test code:
Code: Select all
//Load the word file
             Document doc = new Document();
             doc.LoadFromFile(@"../../data/testPlaceholder.docx");


             string newText = "The second paragraph: Spire.Doc for .NET is a totally independent .NET Word class library which doesn't require Microsoft Office installed on system.";
             string oldText = "<19_BILL_TYPE_DESC>";

             //Replacing text
             doc.Replace(oldText, newText, true, true);

             //Find the new text
             TextSelection[] textSelections = doc.FindAllString(newText, false, true);

             //Set the indent for the new paragraph
             foreach (TextSelection selection in textSelections)
             {
                 TextRange[] textRanges = selection.GetRanges();

                 foreach (TextRange range in textRanges)
                 {
                     Paragraph para = range.OwnerParagraph;
                     para.Format.SetLeftIndent(14);
                     para.Format.SetRightIndent(14);
                 }
             }

             //Save the result document
             doc.SaveToFile(@"../../output/result2.docx", Spire.Doc.FileFormat.Docx);

If you have any issue, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Thu Nov 24, 2022 6:45 am

Thanks much for the Help. Spire support is so good. Saved a lot of time. :)

suryaks2002
 
Posts: 17
Joined: Wed Nov 09, 2022 7:57 am

Thu Nov 24, 2022 7:10 am

Hello,

Thanks for your feedback.
Thank you for your recognition, if you have any issue in the future, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc