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-08-15

How to create PDF documents using MONO and Apitron PDF Kit

Introduction


To demonstrate the cross-platform nature of Apitron PDF Kit library we decided to create a small sample showing how one can programmatically create a PDF document using MONO and our pdf .NET component. You’ll be able to run it on any MONO powered system, and we did it on OS X and Windows.


Prerequisites


1) Make sure MONO is installed on all machines where you’re going to run the app. We used v.4.0.3 available from mono project website.

2) This sample was created using Xamarin Studio (former MonoDevelop), so please download and install it as well.

3) Get the latest version of the Apitron PDF Kit for .NET component, you can download it here.


Creating the project


Open Xamarin Studio and select File -> New…- >Solution… -> .NET -> Console Project, as shown on the image below:

Pic. 1 Creating new Mono.NET console project

Pic. 1 Creating new Mono.NET console project

After clicking “Next”, configure you project’s location and finish the creation.


Adding references


Add the reference to the Apitron.PDF.Kit.dll from the downloaded zip package, so your project references would look like on the image below:

Pic. 2 Main program code file

Pic. 2 Main program code file

Writing the code


The code used in this sample is provided below; it creates a simple PDF file with a few text blocks.  It shows how to use styles and markup parsing for creating several TextBlocks at once. The automatic link creation for TextBlocks is demonstrated as well.

using System;
using Apitron.PDF.Kit;
using Apitron.PDF.Kit.FlowLayout.Content;
using System.IO;
using Apitron.PDF.Kit.Styles;
using Apitron.PDF.Kit.Styles.Text;
using System.Diagnostics;
using Apitron.PDF.Kit.FixedLayout.Resources;
using Apitron.PDF.Kit.FixedLayout.Resources.Fonts;
using Font = Apitron.PDF.Kit.Styles.Text.Font;
using Apitron.PDF.Kit.Styles.Appearance;
using Apitron.PDF.Kit.FlowLayout;

namespace CreatePDFFileSample
{
    class MainClass
    {
        public static void Main (string[] args)
        {            
            // create flow layout PDF document and resource manager
            ResourceManager resourceManager = new ResourceManager ();
            FlowDocument pdfDocument = new FlowDocument () 
              {
                Margin = new Thickness(10),
                Align = Align.Justify
              };

            // register styles
            pdfDocument.StyleManager.RegisterStyle ("textblock",new Style()
                {
                    Font = new Font(StandardFonts.Helvetica, 20),
                    Color = RgbColors.Black,
                });

            pdfDocument.StyleManager.RegisterStyle ("textblock.a"new Style()
                {
                    Color = RgbColors.Blue    
                });

            // add simple text blok element
            pdfDocument.Add(
                new TextBlock ("This PDF file was created using Apitron PDF Kit for .NET component. " +
                "It's a cross-platform library that can be used to manipulate PDF in Windows, " +
                "Mac, iOS, Android and Mono apps."));

            // add text containing the block acting as an interactive link
            pdfDocument.AddItems(
                ContentElement.FromMarkup("Visit our <a href='www.apitron.com'>website</a> " +
                "if you want to learn more about our PDF components."));

            string outputFileName = "document.pdf";

            // save the PDF file
            using (Stream outputStream = File.Create (outputFileName)) 
            {                            
                pdfDocument.Write (outputStream, resourceManager);
            }

            // open file with default system viewer
            Process.Start (outputFileName);
        }
    }
}

Results


If you run the sample created above, you’ll get the results shown on the images below:

Pic. 3 Resulting PDF document ( Windows )

Pic. 3 Resulting PDF document ( Windows )

You can see that the text is justified (because we set the document’s property Align), and it also contains a web link (pointed by the small hand cursor). If you click on this link, you’ll be navigated to the URL shown by the tooltip. The link in this document is represented by the Link Annotation object.

Running the app on OS X produces the same results. Just copy it to your OS X machine and execute the following command in terminal:

mono test/CreatePDFFileSample.exe

It assumes you have created a directory named “test” in your home folder and put files there. An image below shows the resulting PDF document:

Pic. 4 Resulting PDF document ( OS X )
Pic. 4 Resulting PDF document ( OS X )

Conclusion


Creation of the cross platform PDF processing application might be easier than you think with the right tools in hands. We’ve have shown how you could create a simple MONO based pdf processing app in this article. But the Apitron PDF Kit .NET component is not limited to such simple cases; it can be used to perform complex PDF processing tasks including: content manipulation and extraction, document merging, adding digital signatures, creation of PDF forms and much more, and provides a uniform API for all supported platforms.

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

No comments:

Post a Comment