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.

2016-01-15

How to flatten PDF form fields

Introduction


Many of you became familiar with PDF forms either as developers or as citizens needed to report taxes or request some service from the government. PDF forms are everywhere now and in this article we’ll talk about them. A great example of using this PDF feature is our Universal App code sample available by this link.

A PDF form is a collection of objects called “fields” and these fields get stored inside the PDF file when you create or modify them. Some fields can be displayed on PDF page, while others are fully internal; it depends on how the field was created. In order to display the field on page you need to create a “view” for it, in PDF terms it’s called a Widget Annotation. See the section 12.5.6.19 Widget Annotations of the PDF specification for the details.


While the field’s view is a dynamic object, allowing you to change the value of the field for example (if the read-only flag is not set), sometimes you may need to make it static, turning it to a regular PDF content and unlinking from the parent field. This process is called flattening of the PDF fields and in this article we’ll show how to do it using Apitron PDF Kit API. 

The code


class Program
{
    // creates test PDF document with text field
    private static void CreateTestDocument()
    {
        FixedDocument doc = new FixedDocument();

        // create text field and add to doc's field collection
        TextField textField = new TextField("textField", "Text field content");
        doc.AcroForm.Fields.Add(textField);

        // create new page and add text view to it
        Page page = new Page();

        TextFieldView fieldView = new  TextFieldView(textField, 
            new Boundary(10,800,150,820));
        fieldView.BorderColor = RgbColors.Red.Components;

        page.Annotations.Add( fieldView);
        // add page to document
        doc.Pages.Add(page);

        // save document
        using (Stream stream = File.Create("documentWithField.pdf"))
        {
            doc.Save(stream);
        }
    }

    static void Main(string[] args)
    {
        CreateTestDocument();

        using (Stream inputStream = File.Open("documentWithField.pdf",FileMode.Open))
        {
            FixedDocument doc = new FixedDocument(inputStream);
               
            // we can either flatten all fields or enumerate the collection
            // and flatten specific field by calling its Flatten() method
            doc.AcroForm.FlattenFields();

            // save document
            using (Stream outputStream = File.Create("fieldsFlattening.pdf"))
            {
                doc.Save(outputStream);
            }
        }

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

This code creates a test PDF form containing a text field, saves it and later loads and flattens containing fields. The complete sample can be found in our github repo.


Results look as follows:

Pic. 1 Test PDF form

Pic. 1 Test PDF form


Pic. 2 Flattened PDF Form

Pic. 2 Flattened PDF Form


On the first picture you can see the highlighted PDF field that can be edited and saved. On the second picture the flattened PDF form is shown. The view became the part of the page’s content and is not linked to the underlying field anymore.


Conclusion


Working with PDF forms is simple and convenient using Apitron PDF Kit for .NET component. Create cross-platform apps targeting desktop, web, and mobile platforms including iOS and Android (via Xamarin) and enjoy the easy to use API and our highly-qualified support. Read more about the component here or just email us if you have any questions about the library and its features.

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

No comments:

Post a Comment