Formatting PDF Document

Get Document Window and Page Display Properties

This topic helps you understand how to get properties of the document window, viewer application, and how pages are displayed. To set these properties:

Open the PDF file using the Document class. Now, you can set the Document object’s properties, such as

  • CenterWindow – Center the document window on the screen. Default: false.
  • Direction – Reading order. This determines how pages are laid out when displayed side by side. Default: left to right.
  • DisplayDocTitle – Display the document title in the document window title bar. Default: false (the title is displayed).
  • HideMenuBar -– Hide or display the document window’s menu bar. Default: false (menu bar is displayed).
  • HideToolBar -– Hide or display the document window’s toolbar. Default: false (toolbar is displayed).
  • HideWindowUI -– Hide or display document window elements like scroll bars. Default: false (UI elements are displayed).
  • NonFullScreenPageMode -– How the document when it’s not displayed in full-page mode.
  • PageLayout -– The page layout.
  • PageMode -– How the document is displayed when first opened. The options are show thumbnails, full-screen, show attachment panel.

The following code snippet shows you how to get the properties using Document class.

Set Document Window and Page Display Properties

This topic explains how to set the properties of the document window, viewer application, and page display. To set these different properties:

  1. Open the PDF file using the Document class.
  2. Set the Document object’s properties.
  3. Save the updated PDF file using the Save method.

Properties available are:

  • CenterWindow
  • Direction
  • DisplayDocTitle
  • FitWindow
  • HideMenuBar
  • HideToolBar
  • HideWindowUI
  • NonFullScreenPageMode
  • PageLayout
  • PageMode

Each is used and described in the code below. The following code snippet shows you how to set the properties using the Document class.

Embedding Fonts in an existing PDF file

PDF readers support a core of 14 fonts so that documents can be displayed the same way regardless of the platform the document is displayed on. When a PDF contains a font that is not one of the 14 core fonts, embed the font to the PDF file to avoid font substitution.

Aspose.PDF for .NET supports font embedding in existing PDF files. You can embed a complete font or a subset of the font. To embed the font, open the PDF file using the Document class. Then use the Aspose.Pdf.Text.Font class to embed the font into the PDF file. To embed the full font, use the Font class’ IsEmbeded property; to use a subset of the font, use the IsSubset property.

The following code snippet shows how to embed a font in a PDF file.

Embedding Standard Type 1 Fonts

Some PDF documents have fonts from a special Adobe font set. Fonts from this set are called “Standard Type 1 Fonts”. This set includes 14 fonts and embedding this type of fonts requires using of special flags i.e Aspose.Pdf.Document.EmbedStandardFonts. Following is the code snippet which can be used to get a document with all fonts embedded including Standard Type 1 Fonts:

Embedding Fonts while creating PDF

If you need to use any font other than the 14 core fonts supported by Adobe Reader, you must embed the font description while generating the Pdf file. If font information is not embedded, Adobe Reader will take it from the Operating System if it’s installed over the system, or it will construct a substitute font according to the font descriptor in the Pdf.

We use the property IsEmbedded of Font class to embed the font information into Pdf file. Setting the value of this property to ‘True’ will embed the complete font file into the Pdf, knowing the fact that it will increase the Pdf file size. Following is the code snippet that can be used to embed the font information into Pdf.

Set Default Font Name while Saving PDF

When a PDF document contains fonts, which are not available in the document itself and on the device, API replaces these fonts with the default font. When font is available (is installed on the device or is embedded into the document), output PDF should have the same font (should not be replaced with default font). The value of the default font should contain the name of the font (not the path to the font files). We have implemented a feature to set default font name while saving a document as PDF. Following code snippet can be used to set default font:

Get All Fonts from PDF Document

In case you want to get all fonts from a PDF document, you can use FontUtilities.GetAllFonts() method provided in Document class. Please check following code snippet in order to get all fonts from an existing PDF document:

Get Warnings for Font Substitution

Aspose.PDF for .NET provides methods to get notifications about font substitution for handling font substitution cases. The code snippets below show how to use corresponding functionality.

 The OnFontSubstitution method is as listed below.

Improve Fonts Embedding using FontSubsetStrategy

The feature to embed the fonts as a subset can be accomplished by using the IsSubset property, but sometimes you want to reduce a fully embedded font set to only subsets that are used in the document. Aspose.Pdf.Document has property FontUtilities which includes method SubsetFonts(FontSubsetStrategy subsetStrategy). In the method SubsetFonts(), the parameter subsetStrategy helps to tune the subset strategy. FontSubsetStrategy supports two following variants of font subsetting.

  • SubsetAllFonts - This will subset all fonts, used in a document.
  • SubsetEmbeddedFontsOnly - This will subset only those fonts which are fully embedded into the document.

Following code snippet shows how to set FontSubsetStrategy:

Get-Set Zoom Factor of PDF File

Sometimes, you want to determine what a PDF document’s current zoom factor is. With Aspose.Pdf, you can find out the current value as well as set one.

The GoToAction class’ Destination property allows you to get the zoom value associated with a PDF file. Similarly, it can be used to set a file’s zoom factor.

Set Zoom factor

The following code snippet shows how to set the zoom factor of a PDF file.

Get Zoom Factor

The following code snippet shows how to get a PDF file’s zoom factor.

Trim Whitespace Around a Page

A PDF file is comprised of text, images, graphics and various other objects. To remove or trim the white space around a PDF page, set the CropBox for that particular page. To determine the correct value of the CropBox coordinates, first you need to identify the position of objects on the page.

The graphics primitives boundary detection is not a reliable approach. It is highly probable that some objects can’t be found in facades and provide the API to get their rectangle. A better, reliable approach is to render the PDF page to an image and then determine the margins. The following code snippets show how to:

  1. Render a PDF page to PNG,
  2. determine the objects’ location, and
  3. use those same values to set a CropBox for PDF file.

Setting Print Dialog Preset Properties

Aspoose.PDF allows setting the Print Dialog Preset properties of a PDF document. It allows you to change the DuplexMode property for a PDF document which is set to simplex by default. This can be achieved using two different methodologies as shown below.

Setting Print Dialog Preset Properties using PDF Content Editor