Issue in copying Header

Hi

I am trying to copy header from source document to target , it there is a shape inline followed by the text then the direction of the shape is miss aligned

Please See the screenshot below

Below is the code i am using to copy headers and footers

public void CopyHeaderandFooter(ref Document doc, Document template)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    DocumentBuilder template_builder = new DocumentBuilder(template);
    //if (doc.Sections.Count != template.Sections.Count)
    //{
    //    builder.MoveToDocumentStart();
    //    builder.Write(ControlChar.CrLf);
    //   //builder.InsertBreak(BreakType.SectionBreakContinuous);
    //}
    foreach (Section template_Section in template.Sections)
    {
        template_Section.GetChildNodes(NodeType.Any, true);
        Section section = null;
        int sectionIndex = template.Sections.IndexOf(template_Section);
        //Check whether document A conteins section with same index
        if (doc.Sections.Count > sectionIndex)
        {
            //Get correcponding section
            section = doc.Sections[sectionIndex];
        }
        else
        {
            //Insert empty section
            builder.MoveToDocumentEnd();
            //builder.Writeln("Break");
            builder.InsertBreak(BreakType.SectionBreakContinuous);
            section = builder.CurrentSection;
        }
        //Loop throught all Headers/Footers in the B document
        foreach (HeaderFooter hf_template in template_Section.HeadersFooters)
        {
            //Check whether current section from docA conteins 
            //Header/Footer with the same type as h/f from docB
            if (section.HeadersFooters[hf_template.HeaderFooterType] != null)
            {
                section.HeadersFooters[hf_template.HeaderFooterType].RemoveAllChildren();
                //Append content from h/f B to h/f A
                foreach (Node childB in hf_template.ChildNodes)
                {
                    //Import node
                    Node childA = doc.ImportNode(childB, true, ImportFormatMode.KeepSourceFormatting);
                    //Appent node to h/f
                    section.HeadersFooters[hf_template.HeaderFooterType].AppendChild(childA);
                }
            }
            else
            {
                //Copy whole h/f
                Node hf_doc = doc.ImportNode(hf_template, true, ImportFormatMode.KeepSourceFormatting);
                var hfType = hf_template.HeaderFooterType;
                section.HeadersFooters.Add(hf_doc);
                foreach (Paragraph para in hf_template.GetChildNodes(NodeType.Paragraph, true))
                {
                    int idx = hf_template.ChildNodes.IndexOf(para);

                    if (hf_doc.NodeType == NodeType.Paragraph)
                    {
                        Paragraph hf_para = (Paragraph)((HeaderFooter)hf_doc).ChildNodes[idx];
                        hf_para.ParagraphFormat.Style.Font.Name = para.ParagraphFormat.Style.Font.Name;
                        hf_para.ParagraphFormat.Style.Font.NameAscii = para.ParagraphFormat.Style.Font.NameAscii;
                        hf_para.ParagraphFormat.Style.Font.NameBi = para.ParagraphFormat.Style.Font.NameBi;
                        hf_para.ParagraphFormat.Style.Font.NameFarEast = para.ParagraphFormat.Style.Font.NameFarEast;
                        hf_para.ParagraphFormat.Style.Font.NameOther = para.ParagraphFormat.Style.Font.NameOther;
                    }
                }
            }
        }
    }
}

Input.docx (386.6 KB)

@cyrusdaru1 Unfortunately, I cannot reproduce the problem on my side. I have used the following simple code for testing:

Document template = new Document(@"C:\Temp\in.docx");
Document doc = new Document();
CopyHeaderandFooter(ref doc, template);
doc.Save(@"C:\Temp\out.docx");

Could you please create a simple console application, which will allow us to reproduce the problem and attach it here? We will check it and provide you more information.

Hi

You can download the console app an the file from the link below

https://1drv.ms/u/s!Ak4rIGP9ItYzhqc1fOhlmYv-79uXdQ?e=GCeXGZ

@cyrusdaru1 Thank you for additional information. The problem occurs not in the CopyHeaderandFooter routine. The problem occurs after running CopyShapes method. If comment this method, the header in the output document looks correct.

helper.CopyHeaderandFooter(ref dstDoc, template);
helper.CopyPageSetup(ref dstDoc, template);
// helper.CopyShapes(ref dstDoc, template); // <-- if comment this header in the output document looks correct

This occurs because in the CopyShapes method, the shape is always added at the end of the parent paragraph:

para.AppendChild(docShape_FromTemplate);

You should use this line instead the above one:

para.InsertBefore(docShape_FromTemplate, docShapeNode);

Hi , How can i know the shape is inside header or footer. Tried ParentNode.NodeType did not work.

@cyrusdaru1 You can use code like the following to check whether the node is in Header/Footer or not:

bool isInHeaderFooter = para.GetAncestor(NodeType.HeaderFooter) != null;