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 BackgroundImage
for image backgrounds. You can also use BackgroundPosition
and 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 Section
content element:
class Program
{
static void Main(string[] args)
{
//
create resource manager and add image resource
ResourceManager resourceManager = new ResourceManager();
resourceManager.RegisterResource(new
Apitron.PDF.Kit.FixedLayout.Resources.
XObjects.Image("background","../../images/image.jpg"));
//
create document and define styles
FlowDocument pdfDocument = new FlowDocument();
//
define style for section with id = imageBackground
pdfDocument.StyleManager.RegisterStyle("section#imageBackground", new Style()
{
BackgroundImage = new BackgroundImage("background"),
Width = Length.FromPercentage(100),
Height = Length.FromPercentage(100),
});
//
define style for textblocks with class "title"
pdfDocument.StyleManager.RegisterStyle("textblock.title",
new Style()
{
Color = RgbColors.White,
Font = new Font("Arial",40),
Margin = new Thickness(70,395,0,0),
});
// add
section containing text block
pdfDocument.Add(new Section(new TextBlock("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
|
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.
Downloadable version of this article can be found by the following link [PDF].
Downloadable version of this article can be found by the following link [PDF].
No comments:
Post a Comment