简体   繁体   中英

how to add page footer to PDF document using iTextSharp for pages generated in landscape

From my ASP.net MVC application, I am generating PDFs using iTextSharp and XSLT as the template. The pages are supposed to be landscape oriented.

The PDF document in landscape mode by following.

new Document(PageSize.LETTER.Rotate(), marginLeft, marginRight, marginTop, marginBottom);

From the XSLT I get the HTML and construct PDF content as following:

public void ParseXhtmlContents(string xhtml)
{
    //Instantiate handler
    var elementhandler = new ElementHandler();
    //Bind a reader to text
    using (TextReader sr = new StringReader(xhtml))
    {
        //Parse
        XMLWorkerHelper.GetInstance().ParseXHtml(elementhandler, sr);
    }

    //Loop through each element
    foreach (var element in elementhandler.Elements)
    {
        var div = element as PdfDiv;
        if (div != null)
            foreach (var table in div.Content.OfType<PdfPTable>())
            {
                table.HeaderRows = 1;
            }
        _iTextDocument.Add(element);
    }
}

After generating the PDF bytes, I am trying to add page footer as following

private byte[] AddPageHeader(byte[] pdf, float marginLeft, float marginRight, float marginTop, float marginBottom)
{
    using (var stream = new MemoryStream())
    {
        stream.Write(pdf, 0, pdf.Length);
        var reader = new PdfReader(pdf);
        var totalPage = reader.NumberOfPages;
        var pageSize = reader.GetPageSize(1);
        var document = new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom);
        var writer = PdfWriter.GetInstance(document, stream);
        document.Open();
        var contentByte = writer.DirectContent;
        var pageIndex = 0;
        for (var page = 1; page <= reader.NumberOfPages; page++)
        {
            document.NewPage();
            pageIndex++;
            var importedPage = writer.GetImportedPage(reader, page);
            contentByte.AddTemplate(importedPage, 0, 0);
            contentByte.BeginText();
            var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            contentByte.SetFontAndSize(baseFont, 6);
            contentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "-     " + pageIndex.ToString() + "     -", 300f, 20, 0);
            contentByte.EndText();
            contentByte.SaveState();
            contentByte.SetColorStroke(new PdfSpotColor("black", new BaseColor(0, 0, 0)), 100);
            contentByte.SetLineWidth(0.25f);
            contentByte.Rectangle(20, 45, 572, 0.25f);
            contentByte.FillStroke();
            contentByte.RestoreState();
        }
        startingPageNumber = pageIndex;
        document.Close();
        return stream.ToArray();
    }
}

If I skip the part where I add page number I am able to generate the PDF in landscape orientation with content displayed in right order, however when I add footer I am getting the following result, the rendered page is in portrait.

Please refer the attached image for output:

在此处输入图片说明

Help me out on where I am going wrong, thanks.

I found what was wrong

I changed the document creating code as following:

var document = new Document(reader.GetPageSizeWithRotation(1), marginLeft, marginRight, marginTop, marginBottom);

and added a footer template similar to the above:

contentByte.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageIndex).Height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM