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 Apr 06, 2015 7:27 pm

I'd like to replace a token (e.g. ::include::) with the contents of another RTF document.

So far, I'm able to append the subdocument to the end of the first one with
Code: Select all
foreach (Section section in subDoc.Sections)
{
    parentDoc.Sections.Add(section.Clone());
}


I can insert sections but can't figure out how to determine the section the token is.

kbaley
 
Posts: 6
Joined: Mon Apr 06, 2015 7:21 pm

Tue Apr 07, 2015 10:06 am

Hello,

Thanks for your inquiry. We are investigating, if there are any processes, we will let you know immediately.

Best Regards,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Wed Apr 08, 2015 5:53 am

Hello,

Sorry for delay.
Here are the codes for your reference:
Code: Select all
                    Document doc = new Document();
                    doc.LoadFromFile("sample.docx");
                    TextSelection selection = doc.FindString("::include::", true, true);
                    TextRange range = selection.GetAsOneRange();
                    int index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                    Document another = new Document();
                    another.LoadFromFile("another.rtf");
                    foreach (Section section in another.Sections)
                    {
                        foreach (Paragraph p in section.Paragraphs)
                        {
                            foreach (DocumentObject obj in p.ChildObjects)
                            {
                                range.OwnerParagraph.ChildObjects.Insert(index++, obj.Clone());
                            }
                        }
                    }
                    range.OwnerParagraph.ChildObjects.Remove(range);
                    doc.SaveToFile("result.docx", FileFormat.Docx);

If there are any questions, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Wed Apr 08, 2015 3:14 pm

Hi Betsy,

Thank you for this. It works mostly. But all of the subdocuments contents are merged into a single paragraph in the main document. Also, tables are removed completely. I've been trying to figure out how to build these into the main document but haven't had any success yet.

-- Kyle

kbaley
 
Posts: 6
Joined: Mon Apr 06, 2015 7:21 pm

Thu Apr 09, 2015 2:11 am

Dear Kyle,

Please provide your main document and subdocuments for us, and describe your requirement in detail. We will provide a demo for you ASAP.

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu Apr 09, 2015 3:02 pm

Betsy,

The files are attached. document5 is to be inserted into document3 where it says ::include::

One separate question: In order to save docx as pdf, do we need a license for Spire.PDF? Or will Spire.Doc suffice?

-- Kyle

kbaley
 
Posts: 6
Joined: Mon Apr 06, 2015 7:21 pm

Fri Apr 10, 2015 4:01 am

Dear Kyle,

Thanks for sharing your document.
Q1:There are the codes for your reference:
Code: Select all
Document doc = new Document();
            doc.LoadFromFile("document3.docx", FileFormat.Docx);
            TextSelection[] selections = doc.FindAllString("::include::", true, true);
            foreach (TextSelection selection in selections)
            {
                Paragraph p = selection.GetAsOneRange().OwnerParagraph;
                Body body1 = p.OwnerTextBody;
                int index = body1.ChildObjects.IndexOf(p);
                Document another = new Document();
                another.LoadFromFile("document5.docx", FileFormat.Docx);
                foreach (Section section in another.Sections)
                {
                    Body body = section.Body;
                    foreach (DocumentObject obj in body.ChildObjects)
                    {
                        body1.ChildObjects.Insert(index++, obj.Clone());
                    }
                }
                body1.ChildObjects.Remove(p);
            }
            doc.SaveToFile("result.docx", FileFormat.Docx);
Q2:
You only need a license for Spire.Doc:
Code: Select all
docx.SaveToFile("result.pdf", FileFormat.PDF);

If there are any questions, welcome to get it back to us.

Best Regards,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Apr 10, 2015 12:43 pm

Thanks Betsy. I'll review your code shortly but it looks like it will do the trick. And thank you for the clarification on the licensing. The reason I asked about Spire.PDF is because when you save a document as PDF, I get an evaluation warning that says the document was created by Spire.PDF.

kbaley
 
Posts: 6
Joined: Mon Apr 06, 2015 7:21 pm

Mon Apr 13, 2015 3:19 am

Hello,

Thanks for your response.
The reason is that the conversion process will call the Spire.PDF.dll, which needs also license verification. Nevertheless, you only need a license for Spire.Doc.
If there are any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.Doc