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.

Sat May 25, 2019 12:03 am

Please see the attached files.

Code: Select all
Document document = new Document()
document.InsertTextfromFile("9190 Magnesium Sulfate.docx");
document.InsertTextfromFile("4040 Seizure.docx");

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


See attached input and output files. Several problems:

1) Line spacing on page 2 is 1.15 instead of 1.0
2) The paragraph spacing on page 2 isn't quite right--look right above "Document:" and "Pregnancy and Seizure:" under "Universal Seizure Precautions:"
3) The header from the first file become a global header--it should be a heading just for the pages from that file.

owurman
 
Posts: 20
Joined: Tue Apr 04, 2017 8:43 pm

Mon May 27, 2019 8:51 am

Hello,

Thanks for your inquiry and below are my answers to your questions.
1) and 2), after parsing your original files, we found there isn't data for line spacing in "4040 Seizure.docx" but there is default data of line spacing in "9190 Magnesium Sulfate.docx". I attached a screenshot for your better reference. Thus, when merging the first word document, it will apply the default style of the target file ("9190 Magnesium Sulfate.docx"). Sorry that there is no better way to get rid of this issue at present. As a workaround, you can set a line spacing for the merged document like the following sample code. My output file also has been attached.
3), Please add Doc.Sections[0].HeadersFooters.LinkToPrevious = false; to achieve it.
Code: Select all
string filePath_1 = @"9190 Magnesium Sulfate.docx";
string filePath_2 = @"4040 Seizure.docx";
Document OriginalDoc = new Document();
OriginalDoc.LoadFromFile(filePath_2);
Document TargetDoc = new Document();
TargetDoc.LoadFromFile(filePath_1);
foreach (Section sec in OriginalDoc.Sections)
{
    TargetDoc.Sections.Add(sec.Clone());
}
//not keep the header
TargetDoc.Sections[1].HeadersFooters.LinkToPrevious = false;
foreach (DocumentObject obj in TargetDoc.Sections[1].Body.ChildObjects)
{
    if (obj is Paragraph)
    {
        Paragraph para = obj as Paragraph;
        foreach (DocumentObject obj2 in para.ChildObjects)
        {
            if (obj2 is ShapeGroup)
            {
                DocumentObjectCollection objCollection= (obj2 as ShapeGroup).ChildObjects;
                for (int i=0; i< (obj2 as ShapeGroup).ChildObjects.Count;i++)
                {
                    if (objCollection[i] is TextBox)
                    {
                        foreach (DocumentObject textBoxChild in (objCollection[i] as TextBox).Body.ChildObjects)
                            if (textBoxChild is Paragraph)
                            {
                                Paragraph textBoxChildPara = textBoxChild as Paragraph;
                                textBoxChildPara.Format.LineSpacing = 12;
                            }
                    }
                    if (objCollection[i] is ShapeObject)
                    {
                        foreach (DocumentObject shapeObjectChild in (objCollection[i] as ShapeObject).ChildObjects)
                            if (shapeObjectChild is Paragraph)
                        {
                            Paragraph shapeObjectChildPara = shapeObjectChild as Paragraph;
                            shapeObjectChildPara.Format.LineSpacing = 12;
                        }
                    }
                }
            }
        }
    }
}
TargetDoc.SaveToFile(@"myoutput.docx", Spire.Doc.FileFormat.Docx);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Mon Jun 03, 2019 8:07 am

Hello,

Greetings from E-iceblue.
Did my solution work for you? Your feedback will be greatly appreciated.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc