We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

New header is appeared after joining DOCX to PDF using .NET

Hello everyone,

I have a problem to remove the header, I read lot of topic here for this kind of problem and could’nt find a suitable solution unfortunately.
I have 2 documents, one .docx file and one .pdf file that I transform as a .docx file, not sure it’s relevant for my problem…

Anyway, I cleared the header and footer on my source file, I unling with the previous header from the previous document, I removed header (first, primary, even), I tried to modify the “Header” style, well, I tried lot of things but couldn’t make it right.

image.png (18.8 KB)

In this picture, we can see this header Section 4, it does not follow the style from the previous one but even if I removed it by coding, it is still there. It bothers me because this small space cut the end of the document I try to add.

Here some code I have, it would be great to have some help from you, ideas, …

        var stream = new MemoryStream();
        Aspose.Pdf.Document doc = new Aspose.Pdf.Document(input); // path document .pdf
        doc.Save(stream, Aspose.Pdf.SaveFormat.DocX);
        stream.Seek(0, SeekOrigin.Begin);
        Aspose.Words.Document srcDoc =  new Aspose.Words.Document(stream);

        // .... //

        foreach (Section s in srcDoc.Sections)
         {
                s.ClearHeadersFooters();
          }

          srcDoc.FirstSection.HeadersFooters.LinkToPrevious(false);
         
         destDoc.AppendDocument(srcDoc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);

Blockquote

I tried also to remove the header style :

SrcDoc.Styles[“Header”].Remove();

But I got an error that I can’t remove a built-in style.
All the borders from any paragraph from the headers have LineStyle = None.

I hope you can help me with this one.

Have a great one. Cheers

Alex.

@abillot

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hello Tahir

Okay thanks for your reply.
Here the .zip with all files needed.

Aspose.Words.Document destDoc = = new Aspose.Words.Document(pathDstFile);

    var stream = new MemoryStream();
    Aspose.Pdf.Document doc = new Aspose.Pdf.Document(pathSrcFile); // path document .pdf
    doc.Save(stream, Aspose.Pdf.SaveFormat.DocX);
    stream.Seek(0, SeekOrigin.Begin);
    Aspose.Words.Document srcDoc =  new Aspose.Words.Document(stream);

    // Here some tries to remove the header or get the appended document the closest from the top edge of the page.

    
                        foreach (Section s in srcDoc.Sections)
                        {
                            s.ClearHeadersFooters();
                        }

                        srcDoc.FirstSection.PageSetup.HeaderDistance = 0;
                        srcDoc.FirstSection.HeadersFooters.LinkToPrevious(false);

                        destDoc.AppendDocument(srcDoc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);

                        Aspose.Words.HeaderFooter header = destDoc.LastSection.HeadersFooters[HeaderFooterType.HeaderFirst];

                        if (header != null)
                        {
                            header.Remove();
                        }

                        header = destDoc.LastSection.HeadersFooters[HeaderFooterType.HeaderEven];

                        if (header != null)
                        {
                            header.Remove();
                        }

                        header = destDoc.LastSection.HeadersFooters[HeaderFooterType.HeaderPrimary];

                        if (header != null)
                        {
                            header.Remove();
                        }

                        destDoc.LastSection.HeadersFooters.LinkToPrevious(false);

Hope it’s enough for you. Maybe the problem is not the header in the appended document but I want the “picture”/appended document to stick at the top edge of the page to be able to see the whole page.

Aspose_HeaderRemoval_PictureFull.zip (497.4 KB)

Best regards,

Alex.

@abillot

Please note that you can import PDF file into Aspose.Words’ DOM and perform document processing task.

We suggest you please use the latest version of Aspose.Words for .NET 20.6 and following code example to get the desired output. Hope this helps you.

Document destDoc = new Document(MyDir + "Src_file.pdf");
Aspose.Words.Document srcDoc = new Aspose.Words.Document(MyDir + "Dst_file.docx");

srcDoc.AppendDocument(destDoc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);
                
srcDoc.LastSection.PageSetup.HeaderDistance = 0;
srcDoc.LastSection.PageSetup.TopMargin = 0;
srcDoc.LastSection.HeadersFooters.Clear();
srcDoc.LastSection.HeadersFooters.LinkToPrevious(false);
srcDoc.LastSection.HeadersFooters[HeaderFooterType.HeaderPrimary].FirstParagraph.ParagraphBreakFont.Size = 1;
srcDoc.Save(MyDir + "20.6.docx");

@tahir.manzoor

Thank you for you help.
We are talking with the client to get the last version, ours is 6 years old.

@abillot

We always encourage our customers to use the latest version of Aspose components as it contains newly introduced features, enhancements and fixes to the issues that were reported earlier.