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.

Wed Mar 13, 2024 6:10 pm

Hello, I am trying to merge a Word document that has header and pagination and a PDF, to have both as one WORD document.

I am using InsertTextFromStream to perform the merge, but the problem is that when the PDF is inserted after the Word, the numbering and header of the word are added

How can I join the PDF after the WORD so that the header and pagination are not included?

camilapm
 
Posts: 11
Joined: Mon Jan 29, 2024 11:55 am

Thu Mar 14, 2024 2:54 am

Hello,

Thanks for your inquiry.
Sorry, we are not very clear about your specific needs. Can you show your specific requirements with some screenshots? Also, please provide us with the Word and PDF files you want to merge, as well as the code you are currently using to help us further investigate. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 203
Joined: Mon Dec 27, 2021 2:23 am

Thu Mar 14, 2024 9:11 am

I dont know how insert and image here, I was trying to insert an image to show the problem.

Let me try yo explain better:

Step 1) I have a Word document that I generate using the SpireDoc library, the document is generated well, and the header and footer images are placed correctly, so that they look a little transparent.

Step 2 ) Then I have a PDF document that I generate using the SpirePDF library, it generates fine.

Now I want to join both documents, the Word and the PDF, if I convert the WORD to PDF using the following method

using (MemoryStream ms3 = new())
{
try
{
Document document = new Document();
document.LoadFromStream(streamPDF, Spire.Doc.FileFormat.Docx);

document.SaveToStream(ms3, Spire.Doc.FileFormat.PDF);

var pdfToInsert = new PdfDocument(ms3);

return SaveData(pdfToInsert, pdfDocument, startIndex);

}
catch (Exception ex) { var a = ex.Message; }
}
return (pdfDocument, 0);

The resulting PDF document is well generated, only that the images of the footer and header look darker, they are no longer shown with the transparency that they appeared when it was a WORD.

Then I tried to convert the PDF from step 2 into a WORD, it converts fine but when I join it with the WORD from step 1, I am using the InsertTextFromStream method and then it generates a document in WORD where the PDF sheets take the WORD footers and headers.

I want that when I join the PDF from step 2, the PDF sheets do not take the footers or headers of the Word from step 1, which is with the document that I am inserting it.

I would like to have a WORD document with the union of the WORD and the PDF

camilapm
 
Posts: 11
Joined: Mon Jan 29, 2024 11:55 am

Fri Mar 15, 2024 3:30 am

Hello,

Thanks for your feedback.
Below is my solution based on your description. If there are any misunderstandings, please upload your test file and test code to the attachment, as shown in the attached screenshot, or you can send it to this email: support@e-iceblue.com. Thanks in advance.
Code: Select all
 // Step1: pdf to word
 PdfToWordConverter converter = new PdfToWordConverter(@"input.pdf");
 converter.SaveToDocx(@"convertedFromPdf.docx");
 // Step2: merge two files
 Document document = new Document();
 document.LoadFromFile("inputDoc1.docx");
 int secCountBefore = document.Sections.Count;
 document.InsertTextFromFile(@"convertedFromPdf.docx", Spire.Doc.FileFormat.Docx2016);
 int secCountAfter = document.Sections.Count;
 // Step3: Clear headers and footers of merged documents
 for (int i = secCountBefore; i < secCountAfter; i++)
 {
     document.Sections[i].HeadersFooters.LinkToPrevious = false;
     document.Sections[i].HeadersFooters.Header.LinkToPrevious = false;
     document.Sections[i].HeadersFooters.Footer.LinkToPrevious = false;
     document.Sections[i].HeadersFooters.Header.ChildObjects.Clear();
     document.Sections[i].HeadersFooters.Header.AddParagraph();
     document.Sections[i].HeadersFooters.Footer.ChildObjects.Clear();
     document.Sections[i].HeadersFooters.Footer.AddParagraph();
 }
 document.SaveToFile("Merged.docx", Spire.Doc.FileFormat.Docx2016);

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 203
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc