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.

Mon Oct 25, 2021 5:05 pm

Hello I am trying to replace a text in a document with spire.net. I was able to do this with the replacObj function i took from one of your docs. However, if the table i have has too many rows, the whole document format breaks. what should happen is the document should continue the table onto the next page rather then break everything.
I am using this to insert table into document
sectionBody.ChildObjects.Insert(replacementIndex + i , replacement[i].Clone());

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Tue Oct 26, 2021 2:01 am

Hello Ouri,

Thanks for your inquiry.
I used Replace fucntion and Insert fucntion to test an html table that has too many rows, but didn't reproduce your issue.
Here is my test code.
Replace fucntion
Code: Select all
           Document doc = new Document();
            doc.LoadFromFile("sample.docx");

            Section newSection = doc.AddSection();
            Paragraph p = newSection.AddParagraph();
            p.AppendHTML(File.ReadAllText("html.txt"));
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark("replace1");

            TextBodyPart content = navigator.GetBookmarkContent();
            content.Clear();
            for (int i = 0; i < newSection.Body.ChildObjects.Count; i++)
            {
                content.BodyItems.Add(newSection.Body.ChildObjects[i].Clone());
            }


            navigator.ReplaceBookmarkContent(content);
            doc.Sections.Remove(newSection);

            doc.SaveToFile("replace.docx", FileFormat.Docx);

Insert function
Code: Select all
           Document document = new Document();
            document.LoadFromFile("sample.docx");

            Section newSec = document.AddSection();
            Paragraph paragraph = newSec.AddParagraph();
            paragraph.AppendHTML(File.ReadAllText("html.txt"));
            BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
            bookmarksNavigator.MoveToBookmark("replace1");
            Paragraph para = bookmarksNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph;
            Body body = (Body)para.Owner;
            int index = body.ChildObjects.IndexOf(para);
            for (int i = 0; i < newSec.Body.ChildObjects.Count; i++)
            {
                body.ChildObjects.Insert(index + 1, newSec.Body.ChildObjects[i].Clone());
            }
            body.ChildObjects.Remove(para);
            document.Sections.Remove(newSec);

            document.SaveToFile("insert.docx", FileFormat.Docx);


To help me reproduce your issue, could you please share your html table?
Thanks for your assistance in advance.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Oct 26, 2021 2:24 pm

I am trying to do the replace method that you had. im trying to replace the text %2A% with html code however i can get an error see attached.

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Tue Oct 26, 2021 2:28 pm

Maybe its the way i am converting pdf to word? can you give me a good way to do that? see attached for way im doing it now

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Tue Oct 26, 2021 3:12 pm

I am uploading 4 attachments so you can get an idea of what im trying to do.
1) original Pdf
2) word doc that was converted from pdf using spire
3) using my original method replacing the text %2A% with a spire table or html table. just need to get a table doesnt matter which.
4) the code im using

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Tue Oct 26, 2021 3:14 pm

Sorry I can not upload more than 2 MB not able to upload the final doc. but here is an image of whats happening

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Wed Oct 27, 2021 8:32 am

Dear Ouri,

Thanks for providing further details.
Spire.PDF converts PDF to Word with a fixed layout rather than a flow layout like MS Word. That means every line of text would be contained within a frame. As the frame's position is fixed,it won't move down as the content above increases. Therefore, when using a table with many rows to replace the text %2A%, the content appears cluttered. In order to resolve the issue, please leave more white space in the area where the content will be replaced in your PDF document.

Hopefully you can understand.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Oct 27, 2021 12:43 pm

My issue is if I leave more white space and the table is small then I’ll have a lot of white space. Is there any way I can trim white space?

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Wed Oct 27, 2021 4:33 pm

is there any way to remove page breaks ? i have tried to do it looping through sections, paragraphs, then getting type of break but they are all column break and can not find the section break(NextPage) on document

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Thu Oct 28, 2021 2:46 am

Dear Ouri,

Thanks for your reply.
The converted Word from PDF by Spire.PDF generally does not have page breaks. After investigating, I'm sorry to say that due to its special layout, there is no way to remove the white space between this table and the contents below it.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Oct 28, 2021 4:50 am

If I use a word document without converting the do I remove section breaks? I only see a page breaktype and when running through the child objects all I see is column breaks.

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Thu Oct 28, 2021 6:06 am

Dear Ouri,

Please refer to the following code to remove section breaks of a word document.
Code: Select all
   Document doc = new Document();
            doc.LoadFromFile(@"test.docx");
            Section sec1 = doc.Sections[0];

            for (int i = 1; i < doc.Sections.Count; i++)
            {
                Section section = doc.Sections[i];
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    sec1.Body.ChildObjects.Add(obj.Clone());
                }
                doc.Sections.Remove(section);
                i--;
            }

            string output = "CloneSectionContent_out.docx";
            doc.SaveToFile(output, FileFormat.Docx2013);


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Oct 29, 2021 6:19 pm

Ok this method is not working for me. Another question, is there a way to convert xml to pdf? i am trying this but doesnt work. see attahced

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Mon Nov 01, 2021 6:55 am

Hello,

Thanks for your feedback.
Regarding the invalidity of deleting section breaks, you can provide your sample Word file to help us have an accurate investigation.
In addition, please refer to the following code to specify the FileFormat when you convert XML file to PDF. If there is still any issue, please also share us with your XML file.
Code: Select all
 Document document = new Document();
 document.LoadFromFile(xmlFile, FileFormat.Xml);
 document.SaveToFile("toPDF.pdf", FileFormat.PDF);

You could attach your files here or send them to us via email (support@e-iceblue.com).

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1647
Joined: Wed Apr 07, 2021 2:50 am

Tue Nov 02, 2021 1:49 pm

I am trying yo convert but i get this error
see attached error + XML

oalkada101
 
Posts: 11
Joined: Mon Oct 25, 2021 3:29 pm

Return to Spire.Doc