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

Free Support Forum - aspose.com

Creating PDF from XML using XSLT in c#

I am using the below piece of code to create a PDF from XML using XSLT, in doing so I would like to add some Margin at the top and bottom of the PDF, the reason behind trying to add margins is to later stamp Header and footer to the PDF by using ImageStamp class. (Please let me know if there is a better way to add header and footer to PDF when creating PDF from XML using XSTL).
I tried to use “pdf.PageInfo.Margin.Top = 30;” but it doesn’t work.
Actually none of the below pdf.PageInfo properties are making any difference to the end PDF. Please advice.

public ReadOnlySpan BindDocument(string xml, string xslt, object documentInfo = null)
XslFoLoadOptions xslFoLoad = new XslFoLoadOptions(new MemoryStream(Encoding.UTF8.GetBytes(xslt)));

        //Create pdf document
        //Bind FO document with XML source data
        using Document pdf = new Document(new MemoryStream(Encoding.UTF8.GetBytes(xml)), xslFoLoad);
        pdf.PageInfo.Margin.Top = 30;
        pdf.PageInfo.Height = PageSize.PageLetter.Height;
        pdf.PageInfo.Width = PageSize.PageLetter.Width;
        pdf.Pages.ToList().ForEach(page => page.SetPageSize(PageSize.PageLetter.Width, PageSize.PageLetter.Height));
        pdf.AllowReusePageContent = true;
        PdfDocumentInfoProperties(pdf, documentInfo);
        pdf.OptimizeSize = true;
        pdf.IsLinearized = true;

        if (!pdf.Pages.Any())
            return new ReadOnlySpan<byte>(Array.Empty<byte>());

        if (_options.MustSecure)

        // save the PDF document
        using MemoryStream ms = new MemoryStream();
        return new ReadOnlySpan<byte>(ms.ToArray());


Can you please also share the sample source files that you are using in the above-shared code snippet? We will test the scenario in our environment and address it accordingly.

Sample.zip (331.0 KB)
Attached sample source files and also the end result I am getting, I am trying to append an image at the end of each page and don’t have space for that.
Note: I would like to append an image at the top and bottom of all pages.DrivingRecords.pdf (123.2 KB)
Basically, I would like for the data to be appended between header and footer in the above attached DrivingRecords.pdf template. Please let me know if you have any questions.


The XslFoLoadOptions Class does not offer any property to set the page margins. Neither setting them on Page Level effects the output because the values are overridden with the ones supplied in input XSLT style sheet. You need to set the page margins in the XSLT and once PDF is created with desired margins, you can add header footer image into it. Please check below modified XSLT and use it to generate the PDF with increased margins: xmlANDxslt.zip (5.4 KB)

That worked, thank you…!!! I appreciate the quick response.

@asad.ali Is there a way to add pagination numbers to the PDF?

I am currently having an option to use “https://docs.aspose.com/pdf/net/add-page-number/”, unless there is an better alternative, please advice.


A neat solution would be to handle page numbers inside XSLT sheet. You can check on internet how to further edit XSLT to display page numbers e.g. this SO post.

@asad.ali Thanks for the advise.

Final question, is there a way to move contents on to next page, if the source xml data is having some kind of delimiter, for example if the source xml is having “continue on to next page”, I would like to move the next xml element to next page.


You can please try adding page-break in the XSLT template to force the content get rendered on the new page.