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-05-20

Advanced text flow effects in PDF documents using floating elements

Introduction


Floating elements in CSS can be defined using CSS float property which can have the following values: none, left, right, initial, inherit. Such elements should be docked to one of the page sides pushing other elements to the opposite side. The element that follows floating element can either respect the “floating” nature of the preceding element or ignore it using the clear property. This property can have values: none, left, right, both, initial, inherit – which indicate the side where floating elements should be ignored and new line created.
Flow layout API provided by Apitron PDF Kit implements similar approach to floating elements, and provides you with an ability to define floating blocks in the same way CSS does. Such elements can be used to create text flow effects e.g. it can flow around the image, other text elements and so on. The visual example is provided in the next section of the article.

Floating image and text sample


Consider the following code that creates three sections with floating image elements docked to different sides of the section. The last section has two images docked to both sides, and its text flows in-between. All sections use the same content and describe the well-known flower called Viola Tricolor.

The code:

public void CreateFloatingImageWithText()
{
    // prepare info blocks
    string paragraph1 = "Viola tricolor, known as heartsease, heart's ease, heart's delight, tickle-my-fancy, Jack-jump-up-and-kiss-me, come-and-cuddle-me, three faces in a hood, or love-in-idleness, is a common European wild flower, growing as an annual or short-lived perennial. It has been introduced into North America, where it has spread widely, and is known as the johnny jump up (though this name is also applied to similar species such as the yellow pansy). It is the progenitor of the cultivated pansy, and is therefore sometimes called wild pansy; before the cultivated pansies were developed, \"pansy\" was an alternative name for the wild form.";

    string paragraph2 = "Viola tricolor is a small plant of creeping and ramping habit, reaching at most 15 cm in height, with flowers about 1.5 cm in diameter. It grows in short grassland on farms and wasteland, chiefly on acid or neutral soils. It is usually found in partial shade. It flowers from April to September (in the northern hemisphere). The flowers can be purple, blue, yellow or white. They are hermaphrodite and self-fertile, pollinated by bees.";
    
    // prepare resources
    ResourceManager resourceManager = new ResourceManager();
    resourceManager.RegisterResource(new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("viola","viola.jpg"));

    // create and fill document
    FlowDocument doc = new FlowDocument(){Margin = new Thickness(5,5,5,5)};

    // register style for image floating to the left
    doc.StyleManager.RegisterStyle("image.thumbnail_left",
        new Style()
        {
           Float = Float.Left,
           Width = 160, 
           Height = 160, 
           Margin = new Thickness(0,0,10,5)
        });

    // register style for image floating to the right
    doc.StyleManager.RegisterStyle("image.thumbnail_right"
        new Style()
        {
           Float = Float.Right,
           Width = 160,
           Height = 160,
           Margin = new Thickness(10,0,0,5)
        });

    // register style for content section
    doc.StyleManager.RegisterStyle("section"
        new Style()
        {
           Align = Align.Justify,
           Border = new Border(1), 
           BorderColor = RgbColors.Black, 
           Margin = new Thickness(0,5,0,5),
           Padding = new Thickness(5,5,5,5)
        });   
    // register style for first level text blocks used as captions
    doc.StyleManager.RegisterStyle("flowdocument > textblock"
        new Style(){Color = RgbColors.Red});
   
    // add first section
    doc.Add(new TextBlock("Image with Float=left"));
    Section description1 = new Section();
    description1.Add(new Apitron.PDF.Kit.FlowLayout.Content.Image("viola")
        {Class = "thumbnail_left"});       

    description1.Add(new TextBlock(paragraph1));
    description1.Add(new Br());
    description1.Add(new TextBlock(paragraph2));
    doc.Add(description1);

    // add second section
    doc.Add(new TextBlock("Image with Float=right"));
    Section description2 = new Section();
    description2.Add(new Apitron.PDF.Kit.FlowLayout.Content.Image("viola")
        {Class="thumbnail_right" });
    description2.Add(new TextBlock(paragraph1));
    description2.Add(new Br());
    description2.Add(new TextBlock(paragraph2));
    doc.Add(description2);
   
    // add third section
    doc.Add(new TextBlock("Images with Float=left and Float=Right"));
    Section description3 = new Section();
    description3.Add(new Apitron.PDF.Kit.FlowLayout.Content.Image("viola")
        {Class = "thumbnail_left"});
    description3.Add(new Apitron.PDF.Kit.FlowLayout.Content.Image("viola")
        {Class="thumbnail_right" });
    description3.Add(new TextBlock(paragraph1));
    description3.Add(new Br());
    description3.Add(new TextBlock(paragraph2));
    doc.Add(description3);

    // save document
    using (Stream stream = File.Create("floating_elements.pdf"))
    {
        doc.Write(stream,resourceManager);
    }
}

Each image becomes styled via appropriate style defined using class selector. Section objects are being styled using type selector, and texblocks using child elements selector.

The resulting image produced by this code sample is shown below:

Pic. 1 Floating image and text pdf

Pic. 1 Floating image and text

Ignoring the floats: Clear


What if we need to ignore the floating element? Clear setting lets the layout engine know that element should ignore the preceding floating elements and continue from the new line.
If we used this setting for the first section of our document, it would like that:
    ...
    // set the clear value for this text block
    description1.Add(new TextBlock(paragraph2){Clear = Clear.Left});
    ...

And the resulting image:

Pic. 2 Usage of clear setting, second paragraph uses clear = left, pdf

Pic. 2 Usage of clear setting, second paragraph uses clear = left


Conclusion


In this post we described how to use floating elements for achieving advanced text effects with the help of Flow layout API provided by our PDF library. Read more about Apitron PDF Kit for .NET pdf component on its product page. Try it and let us know what you think. We regularly post new samples and articles in our blog and welcome any feedback from you.

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

No comments:

Post a Comment