Monthly Archives: July 2013

Creating Thumbnails of PDF Pages

A common question we receive from our customers is how do I create thumbnails of a PDF document? The best class to use for this is PDFDraw, which rasterizes* PDF pages one at a time, and provides methods for saving these images in a variety of formats. The C# code snippet below shows how to extract all of the pages from a document and save them as JPEGs with a quality of 80.

// A collection of rendering 'hints'.
ObjSet hint_set=new ObjSet();
try
{
	using (PDFDoc doc = new PDFDoc("myDocument.pdf"))
	{
		// Initialize the security handler, in case the PDF is encrypted.
		doc.InitSecurityHandler();

		// Set the output resolution is to 72 DPI.
		draw.SetDPI(72);

		// Use optional encoder parameter to specify JPEG quality.
		Obj encoder_param = hint_set.CreateDict();
		encoder_param.PutNumber("Quality", 80);

		// Traverse all pages in the document.
		for (PageIterator itr=doc.GetPageIterator(); itr.HasNext(); itr.Next())
		{
			string outname = string.Format("{0}myDocument{1:d}.jpg", output_path, itr.GetPageNumber());
			Console.WriteLine("Result saved in {0}", outname);
			draw.Export(itr.Current(), outname, "JPEG", encoder_param);
		}
	}
}
catch (PDFNetException e)
{
		Console.WriteLine(e.Message);
}

One thing of note in this sample is the use of a PageIterator. Using a PageIterator is more efficient than repeatedly calling GetPage(i), because GetPage(int pageNumber) is an O(n) operation.

For more example code on how to use PDFDraw, see the PDFDraw sample on our sample code page.

* When we speak of “rasterizing” a PDF, we mean convert it to bitmapped image data. PDF documents contain vector elements such as lines, text, gradients, etc. that are defined formulaically, and hence can be accurately viewed at any resolution. When you convert a PDF page to an image, you are converting it to raster data at a specific resolution.

PDFTron to Present at PDF Association’s Technical Conference 2013

PDFTron will be sending representatives to the PDF Association’s Technical Conference North America 2013 in Seattle from August 14-15. The conference will be a gathering of PDF industry professionals with educational sessions in two tracks over two days. We will be giving a talk on PDF rendering and viewing on mobile devices and the web. We will also attend PDF/A day on August 16th.

See you there!

Getting Started with the iOS Static Framework

NOTE: This is the getting started document for static framework of PDFNet for iOS. We strongly recommend using the dynamic framework.

Introduction

This tutorial shows the minimum steps needed to open a PDF using PDFNet. It also shows how to add support for annotations, and opening encrypted documents. This document assumes this use of PDFNet V 6.5.3 or greater. Note that a completed project can be found on our GitHub repository. You should use the latest versions of the PDFNet Framework and the tools source code, available by request on our website. The tutorial is divided into four parts:

  • Part 1: Showing a PDF.
  • Part 2: Adding support for text selection, annotation creation and editing, link following and form filling.
  • Part 3: Adding support for encrypted PDF documents.
  • Part 4: Next Steps

Continue reading

Document Collaboration With PDFNet

Introduction

PDFNet makes sharing annotations easy by offering an simple way to import and export annotations. The annotations are saved in a standardized XML format, XFDFThis makes the annotations easy to share across PDFTron’s product line, as well as third-party applications such as Acrobat. The XML annotation files are typically much smaller than a PDF file, making it an ideal format to use when transmitting over a network. The XML format is also well suited for conflict resolution algorithms. This tutorial will demonstrate how to use PDFNet’s APIs to export and import annotations saved in the XFDF format.

Exporting Annotations

The following code snippet shows how to extract annotation data from a PDF, and save it as an XFDF XML file. It is written in Objective-C, but the same API applies for all supported languages.

In Objective-C:

// Create a new PDF Doc
PDFDoc *pdfDoc = [[PDFDoc alloc] initWithFilepath: @"file_with_annotations.pdf"];
[pdfDoc InitSecurityHandler];

// Extract the annotations
FDFDoc *doc_annots = [pdfDoc FDFExtract: e_annots_only];

// This is an optional field that specifies the PDF docuemnt that the FDF file
// was exported from or is intended to be imported into
[doc_annots SetPDFFileName: @"file_with_annotations.pdf"];

// Export the annotations in XFDF format
[doc_annots SaveAsXFDF: @"annotations.xfdf"];

In C#:

// Create a new PDF Doc
PDFDoc in_doc = new PDFDoc("file_with_annotations.pdf");
in_doc.InitSecurityHandler();

// Extract the annotations
FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only);

// This is an optional field that specifies the PDF docuemnt that the FDF file
// was exported from or is intended to be imported into
doc_annots.SetPdfFileName("file_with_annotations.pdf");

// Export the annotations in XFDF format
doc_annots.SaveAsXFDF(output_path + "annotations.xfdf");

Importing Annotations

The following code snippet shows how to import annotations data from an XFDF XML file, and merge them into a PDF file.

In Objective-C:

// load XFDF annotations
FDFDoc *fdfDoc = [FDFDoc CreateFromXFDF: @"annotations.xfdf"];

// load PDF with which to merge the annotations
PDFDoc *pdfDoc = [[PDFDoc alloc] initWithFilepath: @"file_without_annotations.pdf"];
[pdfDoc InitSecurityHandler];

// merge in the annotations
[pdfDoc FDFMerge: fdfDoc];

// save the PDF with the annotations
[pdfDoc SaveToFile: @"file_with_annotations.pdf" flags: e_remove_unused];

[pdfDoc Close];

In C#:

// load XFDF annotations
FDFDoc fdfDoc = new FDFDoc(FDFDoc.CreateFromXFDF("annotations.xfdf"));

// load PDF with which to merge the annotations
PDFDoc pdfDoc = new PDFDoc("file_without_annotations.pdf");
in_doc.InitSecurityHandler();

// merge in the annotations
pdfDoc.FDFMerge(fdfDoc);

// save the PDF with the annotations
pdfDoc.Save("pdfDoc_with_annotations.pdf");

pdfDoc.Close();

XFDF & WebViewer

A nice thing about XFDF is that once annotations are exported to XML they can be easily loaded across all platforms supported by PDFNet (Android, iOS, WinRT, Linux, Mac, etc). Besides PDFNet SDK, the same annotations can be loaded & saved in the WebViewer via CoreControls.AnnotationManager.ImportAnnotations.

The net effect is that a single XML data representation is consistently used across all mobile & desktop platforms as well as the Web.

For more information about XFDF in conjunction with the WebViewer, as well as sample code showing how to implement XFDF processing on the server side, please see section “Processing Annotations and Forms” in WebViewer Developer Guide.