Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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
The following code snippet also work with Aspose.PDF.Drawing library.
The following code snippet shows you how to get the properties using Document class.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetDocumentWindowProperties()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "GetDocumentWindow.pdf"))
{
// Get different document properties
// Position of document's window - Default: false
Console.WriteLine("CenterWindow : {0}", document.CenterWindow);
// Predominant reading order; determines the position of page
// When displayed side by side - Default: L2R
Console.WriteLine("Direction : {0}", document.Direction);
// Whether window's title bar should display document title
// If false, title bar displays PDF file name - Default: false
Console.WriteLine("DisplayDocTitle : {0}", document.DisplayDocTitle);
// Whether to resize the document's window to fit the size of
// First displayed page - Default: false
Console.WriteLine("FitWindow : {0}", document.FitWindow);
// Whether to hide menu bar of the viewer application - Default: false
Console.WriteLine("HideMenuBar : {0}", document.HideMenubar);
// Whether to hide tool bar of the viewer application - Default: false
Console.WriteLine("HideToolBar : {0}", document.HideToolBar);
// Whether to hide UI elements like scroll bars
// And leaving only the page contents displayed - Default: false
Console.WriteLine("HideWindowUI : {0}", document.HideWindowUI);
// Document's page mode. How to display document on exiting full-screen mode.
Console.WriteLine("NonFullScreenPageMode : {0}", document.NonFullScreenPageMode);
// The page layout i.e. single page, one column
Console.WriteLine("PageLayout : {0}", document.PageLayout);
// How the document should display when opened
// I.e. show thumbnails, full-screen, show attachment panel
Console.WriteLine("PageMode : {0}", document.PageMode);
}
}
This topic explains how to set the properties of the document window, viewer application, and page display. To set these different properties:
Properties available are:
Each is used and described in the code below. The following - code snippet shows you how to set the properties using the Document class.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetDocumentWindowProperties()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "SetDocumentWindow.pdf"))
{
// Set different document properties
// Specify to position document's window - Default: false
document.CenterWindow = true;
// Predominant reading order; determines the position of page
// When displayed side by side - Default: L2R
document.Direction = Aspose.Pdf.Direction.R2L;
// Specify whether window's title bar should display document title
// If false, title bar displays PDF file name - Default: false
document.DisplayDocTitle = true;
// Specify whether to resize the document's window to fit the size of
// First displayed page - Default: false
document.FitWindow = true;
// Specify whether to hide menu bar of the viewer application - Default: false
document.HideMenubar = true;
// Specify whether to hide tool bar of the viewer application - Default: false
document.HideToolBar = true;
// Specify whether to hide UI elements like scroll bars
// And leaving only the page contents displayed - Default: false
document.HideWindowUI = true;
// Document's page mode. Specify how to display document on exiting full-screen mode.
document.NonFullScreenPageMode = Aspose.Pdf.PageMode.UseOC;
// Specify the page layout i.e. single page, one column
document.PageLayout = Aspose.Pdf.PageLayout.TwoColumnLeft;
// Specify how the document should display when opened
// I.e. show thumbnails, full-screen, show attachment panel
document.PageMode = Aspose.Pdf.PageMode.UseThumbs;
// Save PDF document
document.Save(dataDir + "SetDocumentWindow_out.pdf");
}
}
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.
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:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void EmbedFontsType1ToPdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Set EmbedStandardFonts property of document
document.EmbedStandardFonts = true;
// Iterate through each page
foreach (var page in document.Pages)
{
if (page.Resources.Fonts != null)
{
foreach (var pageFont in page.Resources.Fonts)
{
// Check if font is already embedded
if (!pageFont.IsEmbedded)
{
pageFont.IsEmbedded = true;
}
}
}
}
// Save PDF document
document.Save(dataDir + "EmbeddedFontsUpdated_out.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.
Please note the embedded font must be installed on the host machine i.e. in case of the following code ‘Univers Condensed’ font is installed over the system.
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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void EmbedFontWhileCreatingPdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Create a section in the Pdf object
var page = document.Pages.Add();
// Create a TextFragment
var fragment = new Aspose.Pdf.Text.TextFragment("");
// Create a TextSegment with sample text
var segment = new Aspose.Pdf.Text.TextSegment(" This is a sample text using Custom font.");
// Create and configure TextState
var ts = new Aspose.Pdf.Text.TextState();
ts.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
ts.Font.IsEmbedded = true;
segment.TextState = ts;
// Add the segment to the fragment
fragment.Segments.Add(segment);
// Add the fragment to the page
page.Paragraphs.Add(fragment);
// Save PDF Document
document.Save(dataDir + "EmbedFontWhileDocCreation_out.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:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetDefaultFontOnDocumentSave(string documentName, string newName)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var fs = new FileStream(dataDir + "GetDocumentWindow.pdf", FileMode.Open))
{
using (var document = new Aspose.Pdf.Document(fs))
{
// Create PdfSaveOptions and specify Default Font Name
var pdfSaveOptions = new Aspose.Pdf.PdfSaveOptions
{
DefaultFontName = newName
};
// Save PDF document
document.Save(dataDir + "DefaultFont_out.pdf", pdfSaveOptions);
}
}
}
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:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetAllFontsFromPdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Get all fonts used in the document
var fonts = document.FontUtilities.GetAllFonts();
// Iterate through each font and print its name
foreach (var font in fonts)
{
Console.WriteLine(font.FontName);
}
}
}
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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void NotificationFontSubstitution()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Attach the FontSubstitution event handler
document.FontSubstitution += OnFontSubstitution;
// You can use lambda
// (oldFont, newFont) => Console.WriteLine(string.Format("Font '{0}' was substituted with another font '{1}'",
// oldFont.FontName, newFont.FontName));
// Save PDF document
document.Save(dataDir + "NotificationFontSubstitution_out.pdf");
}
}
The OnFontSubstitution method is as listed below.
private static void OnFontSubstitution(Aspose.Pdf.Text.Font oldFont, Aspose.Pdf.Text.Font newFont)
{
// Handle the font substitution event here
Console.WriteLine(string.Format("Font '{0}' was substituted with another font '{1}'",
oldFont.FontName, newFont.FontName));
}
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.
Following code snippet shows how to set FontSubsetStrategy:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetFontSubsetStrategy()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// All fonts will be embedded as subset into document in case of SubsetAllFonts.
document.FontUtilities.SubsetFonts(Aspose.Pdf.FontSubsetStrategy.SubsetAllFonts);
// Font subset will be embedded for fully embedded fonts but fonts which are not embedded into document will not be affected.
document.FontUtilities.SubsetFonts(Aspose.Pdf.FontSubsetStrategy.SubsetEmbeddedFontsOnly);
// Save PDF document
document.Save(dataDir + "SetFontSubsetStrategy_out.pdf");
}
}
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.
The following code snippet shows how to set the zoom factor of a PDF file.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetZoomFactor()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "SetZoomFactor.pdf"))
{
// Create GoToAction with a specific zoom factor
var action = new Aspose.Pdf.Annotations.GoToAction(new Aspose.Pdf.Annotations.XYZExplicitDestination(1, 0, 0, 0.5));
document.OpenAction = action;
// Save PDF document
document.Save(dataDir + "ZoomFactor_out.pdf");
}
}
The following code snippet shows how to get a PDF file’s zoom factor.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetZoomFactor()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "Zoomed_pdf.pdf"))
{
// Create GoToAction object
if (document.OpenAction is Aspose.Pdf.Annotations.GoToAction action)
{
// Get the Zoom factor of PDF file
if (action.Destination is Aspose.Pdf.Annotations.XYZExplicitDestination destination)
{
System.Console.WriteLine(destination.Zoom); // Document zoom value;
}
}
}
}
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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetPrintDialogPresetProperties()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
document.Pages.Add();
// Set duplex printing to DuplexFlipLongEdge
document.Duplex = Aspose.Pdf.PrintDuplex.DuplexFlipLongEdge;
// Save PDF document
document.Save(dataDir + "SetPrintDlgPresetProperties_out.pdf", Aspose.Pdf.SaveFormat.Pdf);
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetPrintDialogPresetPropertiesUsingPdfContentEditor()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
string inputFile = dataDir + "input.pdf";
using (var ed = new Aspose.Pdf.Facades.PdfContentEditor())
{
// Bind PDF document
ed.BindPdf(inputFile);
// Check if the file has duplex flip short edge
if ((ed.GetViewerPreference() & Aspose.Pdf.Facades.ViewerPreference.DuplexFlipShortEdge) > 0)
{
Console.WriteLine("The file has duplex flip short edge");
}
// Change the viewer preference to duplex flip short edge
ed.ChangeViewerPreference(Aspose.Pdf.Facades.ViewerPreference.DuplexFlipShortEdge);
// Save PDF document
ed.Save(dataDir + "SetPrintDlgPropertiesUsingPdfContentEditor_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.