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;
pdf.OptimizeResources(_optimizationOptions);
PdfDocumentInfoProperties(pdf, documentInfo);
pdf.Flatten();
pdf.OptimizeSize = true;
pdf.IsLinearized = true;
pdf.Optimize();
if (!pdf.Pages.Any())
{
return new ReadOnlySpan<byte>(Array.Empty<byte>());
}
if (_options.MustSecure)
EncryptPdf(pdf);
// save the PDF document
using MemoryStream ms = new MemoryStream();
pdf.Save(ms);
return new ReadOnlySpan<byte>(ms.ToArray());
}