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.

Fri Oct 30, 2020 11:11 am

Hello,
I am not experienced with spire.doc, maybe answer is easy, but I can´t figure it out.
After opening existing main.docx and starting on a new page (append pageBrake)
Firstly I need to Insert a simple text line with the name of the sometext.rtf file
before I insert its content.
So the new clean page at the end of the main.docx
should contain one text line right above the inserted file content.
But the problem is, that the one text line is on the page all alone,
and the sometext.rtf content is placed - jumped on the another page.
I need them to be placed together on one page.
How to insert file content without jumping on the next page?

My code is like this so far:
Code: Select all
  Dim document As New Document
        document.LoadFromFile("D:\main.docx", FileFormat.Docx, "password1")

        document.LastParagraph.AppendBreak(BreakType.PageBreak)
        document.LastParagraph.AppendText("Content of: sometext.rtf")
        document.LastParagraph.AppendBreak(BreakType.LineBreak)

        document.InsertTextFromFile("D:\sometext.rtf", FileFormat.Rtf)

        document.SaveToFile("D:\output.docx", FileFormat.Docx)
        Process.Start("D:\output.docx", FileFormat.Docx)

bt777_azet.sk
 
Posts: 12
Joined: Thu Apr 23, 2020 8:19 am

Mon Nov 02, 2020 3:46 am

Hello,

Thanks for your inquiry and sorry for the late reply as weekend.
Please refer to the following code to achieve your requirement. If there are any questions, please provide your input files as well as your desired output. Thanks in advance.
Code: Select all
        Dim document As New Document
        document.LoadFromFile("input.docx", FileFormat.Docx, "password1")

        document.LastParagraph.AppendBreak(BreakType.PageBreak)
        document.LastParagraph.AppendText("Content of: sometext.rtf")
        document.LastParagraph.AppendBreak(BreakType.LineBreak)

        Dim additionalDoc As New Document()
        additionalDoc.LoadFromFile("sometext.rtf", FileFormat.Rtf)

        For Each sec As Section In additionalDoc.Sections
            For Each obj As DocumentObject In sec.Body.ChildObjects
                document.Sections(0).Body.ChildObjects.Add(obj.Clone())
            Next obj
        Next sec

        document.SaveToFile("output.docx", FileFormat.Docx)
        Process.Start("output.docx", FileFormat.Docx)


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon Nov 02, 2020 11:54 am

Thank you very much. The initial problem is solved.
But if I might ask you for further tunnig,
Is it possible to "(obj.Clone())" also the LineSpacing and Paragraph indent from original rtf/docx file?
...or at least how to set them after Cloning?

I mean, how to set indent/spacing for whole actually cloned section.

(Spacing Before=0, Spacing After=0, Line Spacing=Single, original Indent settings)

bt777_azet.sk
 
Posts: 12
Joined: Thu Apr 23, 2020 8:19 am

Tue Nov 03, 2020 4:36 am

Hello,

Thanks for your feedback.
Please refer to the following code to set indent/spacing for whole cloned section. If this does not meet your needs well, please provide your input files and your desired output so that we could provide you with the corresponding solution.
Code: Select all
        Dim document As New Document
        document.LoadFromFile("input.docx", FileFormat.Docx, "password1")

        document.LastParagraph.AppendBreak(BreakType.PageBreak)
        document.LastParagraph.AppendText("Content of: sometext.rtf")
        document.LastParagraph.AppendBreak(BreakType.LineBreak)

        Dim additionalDoc As New Document()
        additionalDoc.LoadFromFile("sometext.rtf", FileFormat.Rtf)

        ''method 1
        'For Each paragraph As Paragraph In additionalDoc.Sections(0).Paragraphs
        '     paragraph.Format.FirstLineIndent=0
        '    paragraph.Format.BeforeSpacing = 0
        '    paragraph.Format.AfterSpacing = 0
        '    paragraph.Format.LineSpacing = 12
        'Next
        'method 1

        Dim oldCount As Integer = document.Sections(0).Paragraphs.Count
        For Each sec As Section In additionalDoc.Sections
            For Each obj As DocumentObject In sec.Body.ChildObjects
                document.Sections(0).Body.ChildObjects.Add(obj.Clone)
            Next
        Next

        'method 2
        Dim newCount As Integer = document.Sections(0).Paragraphs.Count
        Dim i As Integer = 0
        Do While (i < newCount)
            If (i >= oldCount) Then
                document.Sections(0).Paragraphs(i).Format.FirstLineIndent = 0
                document.Sections(0).Paragraphs(i).Format.AfterSpacing = 0
                document.Sections(0).Paragraphs(i).Format.BeforeSpacing = 0
                document.Sections(0).Paragraphs(i).Format.LineSpacing = 12
            End If
            i = (i + 1)
        Loop
        'method 2

        document.SaveToFile("output.docx", FileFormat.Docx)


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Nov 04, 2020 7:44 am

Thanks a lot!
It works fine. It meets my needs :D

bt777_azet.sk
 
Posts: 12
Joined: Thu Apr 23, 2020 8:19 am

Wed Nov 04, 2020 9:55 am

Hello,

Glad to hear that!
If you encounter any issues related to our products in the future, please feel free to contact us.
Wish you all the best!

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc

cron