Header and Footer Not Showing When Converting XML to PDF

I am using Aspose.Pdf in a .NET application. I am currently migrating code that was using Aspose.Pdf.Generator to use the DOM approach. I am using the most recent version of Aspose.Pdf (18.1.0).

I have the following XML that I am converting to a Pdf using the Document.BindXml function.

<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
    <Page>
        <Header>
            <TextFragment>
                <TextSegment>Header</TextSegment>
            </TextFragment>
        </Header>        
        <TextFragment>
            <TextSegment>Body</TextSegment>
        </TextFragment>
        <Footer>
            <TextFragment>
                <TextSegment>Footer</TextSegment>
            </TextFragment>
        </Footer>
    </Page>
</Document>

I would expect this code to generate a PDF with a header, body, and footer. However, the generated Pdf only displays the “Body” text. Is there something I am missing to make the header and footer show? Also, are there

@mtpurchases

Thanks for contacting support.

We have tried to test the scenario in our environment while using Aspose.PDF for .NET 18.1 and were unable to notice the issue, which you have mentioned. The output PDF document was generated with header, body and footer. For your reference, we have also attached an output PDF document, generated by below code snippet.

Document HistoryDoc = new Document();
HistoryDoc.BindXml(dataDir + "HeaderFooterXML.xml");
HistoryDoc.Save(dataDir + "outputfromXML.pdf");

outputfromXML.pdf (2.1 KB)

We will really appreciate if you can share some more details regarding your environment e.g OS Version, .NET Framework Version, VS Version, etc. This would help us in replicating the issue in our environment and address it accordingly.

Thank you for your reply.

With this information, I inspected my code and found the error on my end. I was using an incorrect extension function in my code to get the byte[] contents of the PDF. The extension function is used to combine the contents of multiple Documents into one document.

However, that raises another question. If I want to combine two or more Documents into a new Document, but keep the header/footer of each, how would I do that? We need to do this occasionally when we create a packet of PDF documents. We will create an array of Documents (some of which were from BindXml, some of which are statically saved PDFs), and combine them into a new Document.

For reference, here is my extension function:

// Combine documents into one document
var mergedDocument = new Document();

foreach (var document in documents)
{
    mergedDocument.Pages.Add(document.Pages);
}

using (var packetStream = new MemoryStream())
{
    mergedDocument.Save(packetStream, SaveFormat.Pdf);

    return packetStream.ToArray();
}

Using this code will return the PDF I described in my original post.

@mtpurchases

Thanks for getting back to us.

As per my understanding of the scenario, you are generating PDF documents from XML and later combining/merging it with other PDF documents in your program. The code snippet, which you have shared, seems fine and should perform the functionality which you require. However, if you are experiencing any issue while achieving that, please share sample input files along with complete code snippet (which is able to reproduce the issue in our environment), so that we can test the scenario in our environment and address it accordingly.