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

Free Support Forum - aspose.com

Document merging copies headers and footers

Hi,

I have a word document with headers and footers and I need to convert a PDF to a word document and merge the two.

The main document (attached) is DocWithHeadersFooters.docx
I then converted PDF.pdf to PDF.docx (using PdfConvert, GetNextImage, etc)
I then appended PDF.docx to DocWithHeadersFooters.docx like this:

public void Merge()
{
Document doc = new Document(“DocWithHeadersFooters.docx”);
Document appendDoc = new Document(“PDF.docx”);
appendDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
appendDoc.FirstSection.HeadersFooters.LinkToPrevious(false);
doc.AppendDocument(appendDoc, ImportFormatMode.KeepSourceFormatting);
doc.Save(“merge.docx”);
}

I found on another post how to “unlink” the header so it doesn’t print the header details, but as you can see in merge.docx, the header space is still present which pushed the PDF image down and cuts off the bottom.

Any workarounds for this?

Regards,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, in your case, you do not need to use AppendDocument method. I think it would be better directly insert images into the destination document. For instance, see the following code:

// Open destination document

Document dst = new Document(@"Test001\DocWithHeaderFooter.docx");

// Open an image, which we should insert.

Image image = Image.FromFile(@"Test001\image.jpg");

// Create document builder and insert section break at the end of the document.

DocumentBuilder builder = new DocumentBuilder(dst);

builder.MoveToDocumentEnd();

builder.InsertBreak(BreakType.SectionBreakNewPage);

builder.CurrentSection.HeadersFooters.Clear();

// We want the size of the page to be the same as the size of the image.

// Convert pixels to points to size the page to the actual image size.

PageSetup ps = builder.PageSetup;

ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);

ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

// Insert the image into the document and position it at the top left corner of the page.

builder.InsertImage(

image,

RelativeHorizontalPosition.Page,

0,

RelativeVerticalPosition.Page,

0,

ps.PageWidth,

ps.PageHeight,

WrapType.None);

// Save output.

dst.Save(@"Test001\merge.docx");

Hope this helps.

Best regards,