pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation
PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance

PDF Content Extraction
PDF Navigation

PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2015-12-24

How to add header and footer to PDF page

Introduction


Very often, when you generate company specific PDF documents, you have to repeatedly add blocks of information containing logos or security notices to each page, thus adding footer or header, or maybe both.

If you use the Flow layout API provided by Apitron PDF Kit for .NET PDF library for PDF generation, you can add headers and footers to each page automatically and in this article we’ll create a small app demonstrating it.

The code


We’ll add a logo and a block of text to the page header, and page number to the footer.

class Program
{
    static void Main(string[] args)
    {
        // register doc's resources first
        ResourceManager resourceManager = new ResourceManager();
        resourceManager.RegisterResource(
        new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image ("logo",
            "../../data/logo.png"));

        // create document
        FlowDocument doc = new FlowDocument(){Margin = new Thickness(10)};

        // register styles
        doc.StyleManager.RegisterStyle(".pageHeader",new Style()
            {Font = new Font(StandardFonts.TimesBold, 20)});
        doc.StyleManager.RegisterStyle(".pageFooter",new Style(){Align = Align.Right});
        doc.StyleManager.RegisterStyle("hr",new Style(){Height = 2,
            Margin = new Thickness(0,5,0,5)});
        doc.StyleManager.RegisterStyle(".content",new Style(){Align = Align.Justify,
            Display = Display.InlineBlock});
           
        // fill the header section
        doc.PageHeader.Class = "pageHeader";
        doc.PageHeader.Add(new Image("logo"){Width = 100, Height = 50});
        doc.PageHeader.Add(new TextBlock("This document is intended for internal use
            only"){TextIndent = 20});
        doc.PageHeader.Add(new Hr());
           
        // fill the footer section
        doc.PageFooter.Class = "pageFooter";
        doc.PageFooter.Add(new Hr());
        doc.PageFooter.Add(new TextBlock((ctx)=>string.Format("Page {0}
            from ",ctx.CurrentPage+1)));
        doc.PageFooter.Add(new PageCount(3){Display = Display.Inline});

        // add pages
        for (int i = 0; i < 2; ++i)
        {
            doc.Add(new TextBlock(strings.LoremIpsum) {Class = "content"});
            doc.Add(new PageBreak());
        }           

        // generate PDF
        using (Stream stream = File.Create("out.pdf"))
        {
            doc.Write(stream, resourceManager);
        }
        Process.Start("out.pdf");
    }
}

This code adds two pages with dummy text. Please also note the use of PageCount element in the footer section. It gets combined with page index to generate the “Page N from M” text.

The resulting document looks as follows:

Pic. 1 Resulting PDF document

Pic. 1 Resulting PDF document

Conclusion


Using the Flow layout you can automate insertion of page headers and footers to PDF documents with a few lines of code. The Apitron PDF Kit for .NET is a powerful PDF library offering you many unique features which are unlikely to be found anywhere else. Contact us if you have any questions or browse our GitHub repo to check out the described code sample.

Downloadable version of this article can be found by the following link [PDF].

No comments:

Post a Comment