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.

Tue Jun 06, 2017 10:12 pm

im trying to remove line breaks between paragraph but all the methods y use dont remove them, im trying to make the entire dcument a big paragraph trying to merge all the paragraph into 1, but i dont find a method to remove the line breaks only the page breaks, can somebody help me.
Code: Select all
Document doc = new Document();
            string proof="";
            doc.LoadFromFile(FileUpload1.PostedFile.FileName.ToString());
            foreach(Section sec in doc.Sections)
            {
                for(int j = 0; j < sec.Body.ChildObjects.Count; j++)
                {
                    if (string.IsNullOrWhiteSpace((sec.Body.ChildObjects[j] as Paragraph).Text.Trim()))
                    {
                        sec.Body.ChildObjects.Remove(sec.Body.ChildObjects[j]);
                        j--;

                    }
                }
            }
            Label1.Text = proof;
            doc.SaveToFile(HttpContext.Current.Server.MapPath(".") + "/Sample.doc");

jamclucas
 
Posts: 2
Joined: Tue Jun 06, 2017 10:07 pm

Wed Jun 07, 2017 6:37 am

Dear jamclucas,

Thanks for your inquiry.
Please try to use following code to remove the line breaks.
Code: Select all
            Document doc = new Document(@"F:\Sample.docx");
            foreach (Section sec in doc.Sections)
            {
                foreach (Paragraph par in sec.Paragraphs)
                {
                    for (int i = 0; i < par.ChildObjects.Count; i++)
                    {
                        if (par.ChildObjects[i].DocumentObjectType == DocumentObjectType.Break)
                        {
                            Break Pbreak = par.ChildObjects[i] as Break;
                            if (Pbreak.BreakType == BreakType.LineBreak)
                            {
                                par.ChildObjects.Remove(Pbreak);
                            }
                        }
                    }
                }
            }
            doc.SaveToFile("10760.docx",FileFormat.Docx);

Hope this helps. If there still is the issue, please provide us with the input file here or send it to us(support@e-iceblue.com). Then we will provide the corresponding code for you.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Jun 08, 2017 9:43 pm

im having the same issue the line breaks still in the document y try the code thanks for the help, the issue start with a format document or a simple document that contains some "intros" by the text, in word i use a simple replace using "^p" then search and replace with a space, this merge me all the document in one paragraph, im still looking for a method because im trying to build a document by giving by parameter another document.
Fdsgfsdgfsdgfsd degwegweg tewtgewt
Wetgewgtwe



Wetewtewtew

Wertwetwe


awetewta

giving a word document with the quote info then i run the code and i get this
Fdsgfsdgfsdgfsd degwegweg tewtgewt
Wetgewgtwe
Wetewtewtew
Wertwetwe
awetewta

but the line breaks still in the document this is the code i use
Code: Select all
Document doc = new Document(FileUpload1.PostedFile.FileName.ToString());
            foreach (Section sec in doc.Sections)
            {
                foreach (Paragraph par in sec.Paragraphs)
                {
                    for (int i = 0; i < par.ChildObjects.Count; i++)
                    {
                        if (par.ChildObjects[i].DocumentObjectType == DocumentObjectType.Break)
                        {
                            Break Pbreak = par.ChildObjects[i] as Break;
                            if (Pbreak.BreakType == BreakType.LineBreak)
                            {
                                par.ChildObjects.Remove(Pbreak);
                            }
                        }
                    }
                }
                for (int j = 0; j < sec.Body.ChildObjects.Count; j++)
                {
                    if (string.IsNullOrWhiteSpace((sec.Body.ChildObjects[j] as Paragraph).Text.Trim()))
                    {
                        sec.Body.ChildObjects.Remove(sec.Body.ChildObjects[j]);
                        j--;

                    }
                }
            }

jamclucas
 
Posts: 2
Joined: Tue Jun 06, 2017 10:07 pm

Fri Jun 09, 2017 5:56 am

Dear jamclucas,

Thanks for your inquiry.
Please try to use following code.
Code: Select all
            Document doc = new Document(@"F:\testing\doc form\original document\10760.docx");
            foreach (Section sec in doc.Sections)
            {
                Paragraph paraFirst = sec.Paragraphs[0];
                for (int a = 0; a < paraFirst.ChildObjects.Count; a++)
                {
                    DocumentObject obj = paraFirst.ChildObjects[a];
                    if (obj.DocumentObjectType == DocumentObjectType.Break)
                    {
                        //remove the line breaks in the first paragraph.
                        paraFirst.ChildObjects.Remove(obj);
                    }
                }
                //clone the remaining paragraphes into the first paragrah, then delete remaining paragraphes.
                for (int j = 1; j < sec.Paragraphs.Count; j++)
                {
                    Paragraph p = sec.Paragraphs[j] as Paragraph;
                    for (int a = 0; a < p.ChildObjects.Count; a++)
                    {
                        DocumentObject obj = p.ChildObjects[a];
                        if (obj.DocumentObjectType == DocumentObjectType.Break)
                        {
                            p.ChildObjects.Remove(obj);
                        }
                        else
                            paraFirst.ChildObjects.Add(obj.Clone());
                    }
                    sec.Body.ChildObjects.Remove(p);
                }               
            }
            doc.SaveToFile("10760-1.docx", FileFormat.Docx);

Hope this helps. If there is still issue, please offer me the input Word document for investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Jun 13, 2017 9:29 am

Dear jamclucas,

Did you test the code I provided again?Has your issue been resolved ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc