Quantcast
Channel: PDF tips & tricks
Viewing all articles
Browse latest Browse all 125

How to use background images for content elements in PDF

$
0
0

Introduction



Every content element defined in FlowLayout API subset provided by Apitron PDF Kit .NET component can have background set using solid color or an image. Background property can be used for solid backgrounds or pattern based colors and BackgroundImagefor image backgrounds. You can also use BackgroundPositionand BackgroundRepeat properties to fine tune the background tiling if needed.

Code sample


Below is the console app sample that demonstrates how to use background for Sectioncontent element:

classProgram
{
    staticvoid Main(string[] args)
    {
        // create resource manager and add image resource
        ResourceManager resourceManager = newResourceManager();           
        resourceManager.RegisterResource(newApitron.PDF.Kit.FixedLayout.Resources.
XObjects.Image("background","../../images/image.jpg"));

        // create document and define styles
        FlowDocument pdfDocument = newFlowDocument();
          
        // define style for section with id = imageBackground
        pdfDocument.StyleManager.RegisterStyle("section#imageBackground", newStyle()
            {
                BackgroundImage = newBackgroundImage("background"),
                Width = Length.FromPercentage(100),
                Height = Length.FromPercentage(100),
            });

        // define style for textblocks with class "title"
        pdfDocument.StyleManager.RegisterStyle("textblock.title", newStyle()
            {
                Color = RgbColors.White,
                Font = newFont("Arial",40),
                Margin = newThickness(70,395,0,0),
            });

        // add section containing text block
        pdfDocument.Add(newSection(newTextBlock("Background image sample"
          {Class = "title"})
            {
                Id = "imageBackground"
            });
            
        // save document
        using (Stream stream = File.Create("out.pdf"))
        {
            pdfDocument.Write(stream,resourceManager);
        }

        Process.Start("out.pdf");
    }
}

This code defines two styles: one for textblock and one for its container section (of course we could simply assign all properties directly to the elements not defining the styles at all). The section gets sized to page size and also gets an image background set. The textblock becomes positioned inside this section as a child content element.


Resulting PDF document looks as follows:


Pic. 1 Background image usage sample

Pic. 1 Background image usage sample

Conclusion


You can create impressive PDF documents using simple constructs demonstrated in this example. Content elements with image background are an easy to use and expressive way to enhance the look and feel of the new or existing documents. Download Apitron PDF Kit and try it yourself.

Viewing all articles
Browse latest Browse all 125

Trending Articles