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.

2017-08-31

PDF 2.0 specification overview

As you probably know the PDF 2.0 specification has been recently released and it introduced many long awaited features as well as deprecated some stuff. Here we would like to highlight the changes, so you could have an overview of what is going to happen in PDF world soon. Our dev team is working on adding support for all the new features and some of them are already implemented, but nevertheless it will take some time to align the products with the new spec.

So here are the features:
  • "Unencrypted wrapper document" – the way to use custom encryption algorithms for encrypting string and streams
  • "Use of black point compensation" – related to rendering intents and color conversion using ICC
  • "Projection annotations" – projection annotations provide a way to save 3D and other specialized measurements and comments as markup annotations. These measurements and comments then persist in the document
  • "CAdES signatures as used in PDF" – adds new subfilter to PDF signatures
  • "Long term validation of signatures" along with the "Document Security Store (DSS)" and "Document timestamp (DTS) dictionary" specifies the way to store the information needed to validate signatures on a long-term basis (CRL, OCSP and Timestamp data)
  • "Geospatial features" – new way to store geospatial data
  • "Rich media" annotations – common framework for video, audio and animated content
  • "Namespaces" for tagged PDF – new way for preserving logical structure of docs converted from other formats
  • "Pronunciation hints" – a way to aid text-to-speech processors with correct pronunciation
  • "Document parts" – specifies the way the to divide the document into logical parts with different purposes, simply speaking, defining subdocuments
  • "Associated files" – a way to indicate the relationship between the PDF objects in the document and content in other formats
  • Support for ISO 14739-1:2014 Product Representation Compact (PRC) file format
  • Support for UTF-8


Existing PDF features were updated by adding the following capabilities:
  • Transparency and blend mode attributes for the annotations
  • Stamp Annot intent
  • Polygon and Polyline real paths
  • 256-bit AES encryption
  • ECC-based certificates
  • Unicode based passwords
  • Document requirement extensions
  • New value for tab order of fields and annotations
  • Page-level OutputIntents
  • Referenced (external) OutputIntents
  • Thumbnails for embedded files
  • Halftone Origin (HTO)
  • Measurement and Point Data for image and form XObjects
  • L (length) key for inline image data
  • Viewer preferences enforcement (for print scaling)
  • 3D measurements
  • GoToDp action
  • RichMediaExecute action
  • Extension for GoTo and GoToR supporting linking to specific structure elements
  • Extension for Signature Field Locks and Signature Seed Values
  • Extensions for 3D viewing conditions, including transparency
  • Ref (reference) structure elements
  • PageNum and Bates artifact types
  • New list types for structured lists
  • Short” (short name) attribute for table header cells
  • Extensions to OutputIntents (MixingHints and SpectralData)


And of course, many things were deprecated, see the list below:
  • XFA (finally!) including NeedsRendering
  • Movie, Sound and TrapNet annotations
  • Movie and Sound actions
  • Info dictionary
  • Assistive technology restrictions via DRM
  • ProcSet
  • Operation system specific file specifications
  • Operation system specific additions to Launch actions
  • Names for XObjects
  • Names for Fonts
  • Arrays of Blend Modes
  • Alternate Presentations
  • Open prepress interface
  • CharSet (For Type 1 fonts)
  • CIDSet (for CID fonts)
  • Prepress viewer preferences (ViewArea, ViewClip and so on)
  • NeedAppearances
  • adbe.pkcs7.sha1 subfilter, as SHA1 was considered weak
  • adbe.x509.rsa_sha1 subfilter, same as above
  • Encryption of FDF files
  • Suspects flag in MarkInfo dictionary
  • UR signatures

If we missed something just let us know and we’ll add it asap. New releases will be available soon, so keep an eye on Apitron PDF Kit and Apitron PDF Rasterizer pages and nuget packages.

2017-07-31

Government issued smart cards and PDF documents signing

Many EU and non-EU governments and organizations are now issuing so-called e-legitimations, e.g. SuisseID / MobileID in Switzerland, BankID / MobileID / National ID in Nordic countries, ID cards in Italy and Germany, Dutch copy of BankID called iDin, Belgian National ID and so on. Some of them are produced in the form of smartcards carrying necessary digital certificates which can be used for documents signing. 

There are a few ways to work with these cards from the development perspective and it depends on the provided driver and, consequently, supported interfaces. If the target OS is Windows you can either use CryptoAPI, CNG (CryptoAPI Next Generation) or PKCS#11 if supported by the device. Most vendors supply drivers for both, so it shouldn't be the problem. In case of .NET there is a great wrapper for PKCS#11 called Pkcs11Interop that is compliant with v.2.20 of the specification. Working with CryptoAPI often requires PInvokes to be defined and mastered and, honestly, may become quite challenging task.

With macOS it's a bit foggy, nevertheless you can use tokend drivers provided by various vendors either open source or commercial. Apple's own signing API works for this purpose however it may require dancing around to get things working.

Whichever way you choose, we can provide you with a reliable PDF processing library so you'd be able to sign documents without any problems, once you try our API defined to handle various signing scenarios. Apitron PDF Kit sets high standards in PDF processing and our team is well known for the outstanding customer support. Please don't hesitate to contact us if you have any questions, we'll be happy to help you.

2017-05-31

Apitron PDF Kit Extensions Update: Text Watermarks


Introduction

 

In our previous post we announced the release of the open source extensions library for Apitron PDF Kit. We continue making code contributions to this project, so recently a pack of changes related to text watermark support was added and now you can promptly add watermarks using text strings in addition to images.

For those thinking about keeping doc’s size as small as possible, textual watermarks seem to have a clear advantage over images as they usually need less bytes to store. The source code for the library can be found in our github repo by the following link. See the demo of new functionality and samples below.

Code samples



Adding text watermark is super easy:


using (Stream inputStream = File.Open("../../data/letter_unsigned.pdf", FileMode.Open))
{
    string watermarkText = "TOP secret -for INTERNAL use - TOP secret - for
       INTERNAL use - TOP secret - for INTERNAL use - TOP secret";

   
using (FixedDocument doc = new FixedDocument(inputStream))
    {
        doc.WatermarkText(watermarkText, outputFileName);
    }
}

The result is below:
 

Pic.1 Text watermark
Pic.1 Text watermark

Summary


By releasing the Apitron PDF Kit Extensions we wanted to show you how to use the base API and at the same time make things simpler by implementing some tricky things in very a customizable way. Just get the base code and adapt it to your needs or add you own extensions. Don’t forget to share nice snippets with others.

2017-04-30

Open source Apitron PDF Kit extensions library

Introduction



Apitron PDF Kit is an outstanding library for all kinds of tasks related to PDF, it offers well-designed and balanced API that follows the specification as much as possible at the same time making your life easier by automating many routine operations. 

We decided to take it one step further and introduced Apitron PDF Kit open source extensions library which would automate most frequent pdf manipulation activities, and started with adding digital signatures and watermarks. 

The project pursues two main goals:

1. Make PDF processing more intuitive and straightforward
2. Show how the base Apitron PDF Kit API works using real-life examples

The source code for the library can be found in our github repo by the following link. We will probably wrap it as a nuget package later, but for now you can just checkout the code and compile it yourself.


Code samples



Signing is very simple and consist of a one line call of the method Sign, you provide the certicate, password, signature image and its boundaries and a few optional params if needed.


using (Stream inputStream = File.Open("../../data/letter_unsigned.pdf", FileMode.Open))
{
     using (FixedDocument doc = new FixedDocument(inputStream))
     {
         doc.Sign("../../data/johndoe.pfx", "password",
                    "../../data/signatureImage.png",
         new Boundary(100, 140, 190, 180), outputFileName);
     }
}



And the result:

Pic. 1 Signed PDF Document
Pic. 1 Signed PDF Document

 
Same applies to stamping a document with a transparent watermark, just add a one-line call and voilĂ :

using (Stream inputStream = File.Open("../../data/letter_unsigned.pdf", FileMode.Open))
{
     using (FixedDocument doc = new FixedDocument(inputStream))
     {
         doc.Watermark("../../data/watermarkTransparent.png", outputFileName);
     }
}



The result is below:

Pic. 2 Stamped PDF document
Pic. 2 Stamped PDF document
 

Summary



If you need to incorporate the basic PDF processing functionality into your applications, then the open-source Apitron PDF Kit extensions library we’ve published a few weeks ago could be the right choice. It saves time keeping things simple, and you can always fallback to the main API if you need something going beyond its capabilities. 

Please let us know what you think about it or suggest new functions to implement. If you want your code to be added to the library just send us a patch for review.

2017-03-31

Signing pdf documents with smartcards using cryptoapi or PKCS#11 cryptographic token interface

Signing with smartcards might be challenging as they usually restrict private key export and only allow signing via own custom api, integrate with existing cryptoserviceproviders (windows cryptoapi) or provide an implementation of PKCS#11 cryptographic token interface.

If you need to implement an advanced signing workflow using smart cards, we can help you on the way as our products are fully compatible and provide extensible interfaces for these purposes. Should you have any questions, just let us know and we'll be happy to help you.

2017-02-28

How to programmatically delete, edit or replace content in PDF documents

Introduction


Replacing, editing or deleting content from PDF documents programmatically is not a trivial task and requires expert knowledge of the format and internal structures to be implemented from scratch. Luckily, we made it much easier for you by introducing native support for these operations. You can examine document’s content page by page and change the things you need without any significant efforts. In this article we’ll demonstrate how to implement text and image replacement or editing, removing contents from the desired area or region, resources replacement, graphics paths alteration, and getting content elements’ boundaries.



Replacing text and images


Let’s assume you’re developing a web-based solution for a real estate agency and you need to process advertisements stored as PDF documents. One of them could look as below:


Pic. 1 Sample advertisement stored as PDF

Pic. 1 Sample advertisement stored as PDF

But the complete listing should only be accessible to the logged in customers, while you still want the ad to be viewable by other users but with some restrictions that include price and the photo of the object. One of solutions is to generate it dynamically. Here is the code: 

static void Main(string[] args)
{
       ReplaceTextAndImages("../../../data/advertisement.pdf", "$","Price: contact us",
              "../../../data/replacement.png");
}

private static void ReplaceTextAndImages(string inputFilePath, string oldText, 
      string newText, string replacementImagePath)
{
    using (Stream inputStream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read))
    {
        using (FixedDocument doc = new FixedDocument(inputStream))
        {
            // add the replacement image to document's resources
            doc.ResourceManager.RegisterResource(new Image("replacement_image",
                  replacementImagePath, true));

            // enumerate content elements found on document's first page
            foreach (IContentElement element in doc.Pages[0].Elements)
            {
                // handle the text element case
                if (element.ElementType == ElementType.Text)
                {
                    TextContentElement textElement = element as TextContentElement;
                    if (textElement != null)
                    {
                        // go thought all the text segments and replace 
                        // the segment that contains the sample text
                        foreach (TextSegment textSegment in textElement.Segments)
                        {
                            if (textSegment.Text.Contains(oldText))
                            {
                                TextObject newTextObject = 
                                new TextObject(textSegment.FontName,textSegment.FontSize);
                                newTextObject.AppendText(newText);
                                textSegment.ReplaceText(0, textSegment.Text.Length, newTextObject);
                            }
                        }
                    }
                } // handle image case
                else if (element.ElementType == ElementType.Image)
                {
                    ImageContentElement imageElement = element as ImageContentElement;

                    if (imageElement != null)
                    {
                        // just replace the image with new one using
                        // registered resource, removing old one
                        imageElement.Replace("replacement_image", true);
                    }
                }
            }

            // save modified file
            using (Stream outputStream = File.Create(outputFileName))
            {
                doc.Save(outputStream);
            }
        }
    }

    Process.Start(outputFileName);
} 


And the resulting file produced by this code is shown below:


Pic. 2 Edited PDF document

Pic. 2 Edited PDF document




Content deletion


Let’s say you have a document shown below and would like to remove all content that intersects with an arbitrary rectangular region.


Pic. 3 Sample document for content removal

Pic. 3 Sample document for content removal

Here is the code that does the job, it also highlights the elements that were removed using their calculated boundaries:

static void Main(string[] args)
{
    RemoveContentInRect("../../../data/apitron_pdf_kit_in_action_excerpt.pdf",
          new Boundary(70, 200, 330, 450));
}

private static void RemoveContentInRect(string inputFilePath, Boundary redactionRect)
{
    using (Stream inputStream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read))
    {
        using (FixedDocument doc = new FixedDocument(inputStream))
        {
            doc.ResourceManager.RegisterResource(
               new GraphicsState("myGraphicsState") {CurrentNonStrokingAlpha = 0.3});

            // enumerate content elements found on document's first page
            Page firstPage = doc.Pages[0];

            firstPage.Content.SaveGraphicsState();
            firstPage.Content.SetDeviceStrokingColor(new []{1.0,0,0});

            foreach (IContentElement element in firstPage.Elements)
            {
                // remove elements falling into the deletion region
                // even if they just overlap
                if (element.ElementType == ElementType.Text)
                {
                    TextContentElement textElement = (TextContentElement) element;

                    foreach (TextSegment segment in textElement.Segments)
                    {
                        if (RectsOverlap(redactionRect, segment.Boundary))
                        {
                            firstPage.Content.StrokePath(Path.CreateRect(segment.Boundary));
                            segment.Remove();
                        }
                    }
                }
                else if (!RectsOverlap(redactionRect, element.Boundary))
                {
                    firstPage.Content.StrokePath(Path.CreateRect(element.Boundary));
                    element.Remove();
                }
            }
                
            // highlight deletetion region
            firstPage.Content.SetGraphicsState("myGraphicsState");
            firstPage.Content.SetDeviceStrokingColor(new []{0.0});
            firstPage.Content.SetDeviceNonStrokingColor(new []{0.0});
            firstPage.Content.FillAndStrokePath(Path.CreateRect(redactionRect));
            firstPage.Content.RestoreGraphicsState();

            // save modified file
            using (Stream outputStream = File.Create(outputFileName))
            {
                doc.Save(outputStream);
            }
        }
    }
}

public static bool RectsOverlap(Boundary a, Boundary b)
{
    return (a.Left < b.Right && a.Right> b.Left && a.Bottom<b.Top && a.Top>b.Bottom);
}


Resulting document is demonstrated below:


Pic. 4 Document with partially removed content

Pic. 4 Document with partially removed content



Changing existing drawings or graphics paths


If you have a drawing you would like to alter there is an API for that as well. You can also prepend or append PDF content to it, scale, translate or delete. Here is our sample file:


Pic. 5 PDF document with vector drawing

Pic. 5 PDF document with vector drawing


And our code that changes it a bit by altering non stroking colors for all found paths:


static void Main(string[] args)
{
    ReplacePaths("../../../data/graphics.pdf");
}

private static void ReplacePaths(string inputFilePath)
{
    using (Stream inputStream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read))
    {
        using (FixedDocument doc = new FixedDocument(inputStream))
        {
            double colorComponent = 0;
            double colorDelta = 0.1;

            // enumerate content elements found on document's first page
            foreach (IContentElement element in doc.Pages[0].Elements)
            {
                // change the fill color of each found drawing
                if (element.ElementType == ElementType.Drawing)
                {
                    DrawingContentElement drawingElement = (DrawingContentElement) element;
                    drawingElement.SetNonStrokingColor(
                          new double[] { Math.Min(colorComponent,1),0, 0});
                    colorComponent += colorDelta;
                }
            }

            // save modified file
            using (Stream outputStream = File.Create(outputFileName))
            {
                doc.Save(outputStream);
            }
        }
    }

    Process.Start(outputFileName);
}

You can set stroking or non-stroking colors, examine drawing rule or operation type used, even examine the path or add some content by using AddContent method if you need.

The resulting document produced by the code is shown below:

Pic. 6 Edited graphics paths

Pic. 6 Edited graphics paths


Replacing resources in PDF documents


You probably know that PDF documents can contains various resources like fonts, tiling patterns, images, FormXObjects, colorprofiles etc. Whenever you need to replace a resource you can use a special API created for that.

Every FixedDocument (our name for PDF document) has its own resource manager accessible by the property of the same name. So in order to change the resource you can use the following code (relevant part is highlighted):

static void Main(string[] args)
{
    using (Stream inputStream = File.Open("../../../data/patternFill.pdf",
         FileMode.Open, FileAccess.Read))
    {
        using (FixedDocument doc = new FixedDocument(inputStream))
        {
            // create a new tiling pattern
            TilingPattern pattern = new TilingPattern("myNewPattern", 
      new Boundary(0, 0, 20, 20), 25, 25);
            pattern.Content.SetDeviceNonStrokingColor(new double[] { 0.1, 0.5, 0.7 });
            pattern.Content.FillAndStrokePath(Path.CreateCircle(10, 10,9));

            // register new pattern as a resource
            doc.ResourceManager.RegisterResource(pattern);

            // replace the old pattern with new one
            doc.ResourceManager.RegisterReplacement("myPattern","myNewPattern");

            //save modified file
            using (Stream outputStream = File.Create(outputFileName))
            {
                doc.Save(outputStream);
            }
        }
    }

    Process.Start(outputFileName);
}

In this example we replaced the old tiling pattern resource with the new one. Using this technique you can change the appearance of the PDF documents just by changing resources used by drawing operations.


Summary


In this article we demonstrated a few possible scenarios for content editing, removal and replacement in PDF. The topic is quite extensive, so probably we didn’t cover your particular case or maybe you have a specific question. If you need any help with the API or a professional advice just drop us an email, and we’ll be happy to assist you. All samples used in this article can be found in our github repo as well.

2017-01-31

Long-term validation of signatures in PDF documents or LTV enabled signatures

Introduction 

 

Long-term validation of signatures in PDF documents is a mechanism to check that the signature and all related certificates are still valid at the time of checking (opening the doc) without making any requests to the signing authorities. That is all required information including OCSP responses, CRLs and timestamp should be present into the document.

Checking long term validation state


So, how can you check if the file you're viewing has LTV enabled signatures? See the image below that demonstrates that:

LTV-enabled signature  
Pic.1 LTV-enabled signature

Basically PDF reader makes the check for you and displays the "Signature is LTV enabled" message. It also says that the signature is timestamped and it's very important because it proves that the file was signed at specific time and the clock couldn't be manipulated to produce the signature. Another way to check if the signature is LTV enabled is by examining whether the certificates' revocation info is embedded into the document. To do this, open signature properties window and check certs from the certificate chain one by one, you should see the similar picture:

Checking certificate revocation info status
Pic.2 Checking certificate revocation info status

Additionally, you can take a look at advanced signature properties to find the timestamping authority details, hash algorithm used and producer info.

Advanced signature properties
Pic.3 Advanced signature properties

Conclusion

 

While LTV can be tricky to implement sometimes, we have invaluable experience in that area and our products are ready to process LTV enabled documents as well.  So if you need any help with implementing digital signatures workflow, just let us know and we'll be happy to come up with a solution for you.