How to Start Convert PDF Read PDF Edit PDF PDF Report Builder Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

Using C# PDF Property Library
How to format PDF viewing preferences using C# code


C# demo code to format Adobe PDF document viewing preferences





In this C# tutorial, you will learn how to read, update PDF document viewer preferences using C# in ASP.NET MVC web app, and Windows Forms application.

How to read, update PDF document viewer preferences using C#

  1. Download XDoc.PDF Property Manager C# library
  2. Install C# library to set PDF document properties
  3. Step by Step Tutorial










When you view a PDF document using PDF Reader software, such as Acrobat. The initial view of the PDF depends on what is the document property "viewer preferences". For example, a document may open at a particular page or magnification.







PDF Viewer Preferences


About C# class PDFViewerPreferences

  • HideToolbar: Specify whether to hide the viewer application's tool bars when the document is open. Default: false
  • HideMenubar: Specify whether to hide the viewer application's menu bar when the document is open. Default: false
  • HideWindowUI: Specify whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed. Default: false
  • FitWindow: Specify whether to resize the document's window to fit the size of the first displayed page. Default: false
  • CenterWindow: Specify whether to position the document's window in the center of the screen. Default: false.
  • DisplayDocTitle: Specify whether the window's title bar should display the document title. If false, the title bar should display the file name of the document. Default: false
  • NonFullScreenPageMode: Specify how to display the document on exiting full-screen mode. This entry is meaningful only if the value of the PageMode entry in the Catalog dictionary is FullScreen; otherwise it is ignored. Valid Values: UseNone, UseOutlines, UseThumbs, UseOC. Default: PDFPageMode.UseNone
  • IsDirectionR2L: The pre-dominant reading order for text. Default: false (Left to Right)
  • ViewArea: The name of the page boundary representing the area of a page to be displayed when viewing the document on the screen. If the specified page boundary is not defined in the page object, its default value is used. Default value: CropBox
  • ViewClip: The name of the page boundary to which the contents of a page are to be clipped when viewing the document on the screen. If the specified page boundary is not defined in the page object, its default value is used. Default value: CropBox
  • PrintArea: The name of the page boundary representing the area of a page to be rendered when printing the document. If the specified page boundary is not defined in the page object, its default value is used. Default value: CropBox
  • PrintClip: The name of the page boundary to which the contents of a page are to be clipped when printing the document. If the specified page boundary is not defined in the page object, its default value is used. Default value: CropBox
  • UsePrintScaling: The page scaling option to be selected when a print dialog is displayed for this document. False: indicates that the print dialog should reflect no page scaling. True: indicates that applications should use the current print scaling. Default: true
  • Duplex: The paper handling option to use when printing the file from the print dialog.
  • PickTrayByPDFSize: A flag specifying whether the PDF page size is used to select the input paper tray. This setting influences only the preset values used to populate the print dialog presented by a PDF viewer application. If PickTrayByPDFSize is true, the check box in the print dialog associated with input paper tray is checked.
  • PrintPageRange: The page numbers used to initialize the print dialog box when the file is printed. The first page of the PDF file is denoted by 1. Each pair consists of the first and last pages in the sub-range. Note: Any odd number of integers or negative numbers causes this entry to be ignored. SDK would not validate the input, it would save the setting to the PDF file.
  • NumCopies: The number of copies to be printed when the print dialog is opened for this file. Supported values are the integers 2 through 5. Values outside this range are treated as default value 1. Default: 1


About C# Enum PDFPageMode

  • UseNone: Neither document outline nor thumbnail images visible. (Default Value)
  • UseOutlines: Document outline visible.
  • UseThumbs: Thumbnail images visible.
  • FullScreen: Full-screen mode, with no menu bar, window controls, or any other window visible.
  • UseOC: Optional content group panel visible.
  • UseAttachments: Attachments panel visible.


About C# Enum PDFPageBoundaryType

  • MediaBox: Defines the boundaries of the physical medium on which the page is to be printed.
  • CropBox: Defines the region to which the contents of the page are to be clipped (cropped) when displayed or printed.
  • BleedBox: Defines the region to which the contents of the page should be clipped when output in a production environment.
  • TrimBox: Defines the intended dimensions of the finished page after trimming.
  • ArtBox: Defines the extent of the page's meaningful content as intended by the page's creator.


About C# Enum PDFPrintDuplexMode

  • None: Not specified.
  • Simplex: Print single-sided.
  • DuplexFlipShortEdge: Duplex and flip on the short edge of the sheet.
  • DuplexFlipLongEdge: Duplex and flip on the long edge of the sheet.






Read, extract PDF file viewer preferences using C#


String inputFilePath = Program.RootPath + "\\" + "1.pdf";

//  Opne file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Get viewer preferences from the document.
PDFViewerPreferences vp = doc.GetViewerPreferences();
if (vp != null)
{
    Console.WriteLine("Hide Toolbar:              {0}", vp.HideToolbar);
    Console.WriteLine("Hide Menubar:              {0}", vp.HideMenubar);
    Console.WriteLine("Hide Window UI:            {0}", vp.HideWindowUI);
    Console.WriteLine("Fit Window:                {0}", vp.FitWindow);
    Console.WriteLine("Center Window:             {0}", vp.CenterWindow);
    Console.WriteLine("Display Doc Title:         {0}", vp.DisplayDocTitle);
    Console.WriteLine("Non Full Screen Page Mode: {0}", vp.NonFullScreenPageMode);
    Console.WriteLine("Direction:                 {0}", vp.IsDirectionR2L ? "R2L" : "L2R");
    Console.WriteLine("View Area:                 {0}", vp.ViewArea);
    Console.WriteLine("View Clip:                 {0}", vp.ViewClip);
    Console.WriteLine("Print Area:                {0}", vp.PrintArea);
    Console.WriteLine("Print Clip:                {0}", vp.PrintClip);
    Console.WriteLine("Use Current Print Scaling: {0}", vp.UsePrintScaling);
    Console.WriteLine("Duplex:                    {0}", vp.Duplex);
    Console.WriteLine("Checkbox in the print dialog associated with input paper tray is checked: {0}", vp.PickTrayByPDFSize);
    Console.WriteLine("Print Page Range:          {0}", vp.GetPrintPageRangeString());
    Console.WriteLine("Number of Copies:          {0}", vp.NumCopies);
}
else
    Console.WriteLine("No ViewerPreferences entry in the document.");






Modify, update PDF file viewer preferences in C# code


String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);

//  Initial a new viewer preferences with default settings.
PDFViewerPreferences vp = new PDFViewerPreferences();
//  Hide toolbar when the document is open.
vp.HideToolbar = true;
//  Hide menubar when the document is open.
vp.HideMenubar = true;
//  Set default print area to MediaBox.
vp.PrintArea = PDFPageBoundaryType.MediaBox;
//  Set the default print copies to 3 in the print dialog.
vp.NumCopies = 3;
//  Update the Viewer Preferences
doc.SetViewerPreferences(vp);

//  Save file
doc.Save(outputFilePath);






Remove, delete PDF file viewer preferences using C# code


String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Remove Viewer Preferences in the document. 
doc.SetViewerPreferences(null);
//  Save file
doc.Save(outputFilePath);






PDF Page Layout


Define page mode to be used when the document is opened.

About C# enum PDFPageMode

  • UseNone: Neither document outline nor thumbnail images visible. (Default Value)
  • UseOutlines: Document outline visible.
  • UseThumbs: Thumbnail images visible.
  • FullScreen: Full-screen mode, with no menu bar, window controls, or any other window visible.
  • UseOC: Optional content group panel visible.
  • UseAttachments: Attachments panel visible.




Read, extract PDF page layout setting using C#


String inputFilePath = @"C:\1.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Get page layout setting
PDFPageLayout pageLayout = doc.GetPageLayout();
//  Valid Values:
//  SinglePage, OneColumn, TwoColumnLeft, TwoColumnRight, TwoPageLeft, TwoPageRight
Console.WriteLine("Page Layout: {0}", pageLayout.ToString());




Update, modify PDF page layout setting in C#


String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Display pages in 2 columns with odd numbered pages on the left.
doc.SetPageLayout(PDFPageLayout.TwoColumnLeft);
//  Save file
doc.Save(outputFilePath);






PDF Page Mode Setting


Define page mode to be used when the document is opened.

About C# Enum PDFPageMode

  • UseNone: Neither document outline nor thumbnail images visible. (Default Value)
  • UseOutlines: Document outline visible.
  • UseThumbs: Thumbnail images visible.
  • FullScreen: Full-screen mode, with no menu bar, window controls, or any other window visible.
  • UseOC: Optional content group panel visible.
  • UseAttachments: Attachments panel visible.




Read PDF page mode setting in C# code


String inputFilePath = @"C:\1.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Get page mode setting
PDFPageMode pageMode = doc.GetPageMode();
//  Valid Values:
//  UseNone, UseOutlines, UseThumbs, FullScreen, UseOC, UseAttachments
Console.WriteLine("Page Mode: {0}", pageMode.ToString());




Update PDF page mode setting in C# code


String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Show page in Full-screen mode.
doc.SetPageMode(PDFPageMode.FullScreen);
//  Save file
doc.Save(outputFilePath);






PDF Open Action Setting


Specify a destination explicitly in a PDF file.

About C# class PDFDestination

  • Mode: Destination syntax mode. All allowed syntactic forms are listed in enum PDFDestinationMode.
  • PageIdx: Get index of the target page.
  • Left: Get argument 'left' used in the destination syntax. Expressed in the default user space coordinate system.
  • Bottom: Get argument 'bottom' used in the destination syntax. Expressed in the default user space coordinate system.
  • Right: Get argument 'right' used in the destination syntax. Expressed in the default user space coordinate system.
  • Top: Get argument 'top' used in the destination syntax. Expressed in the default user space coordinate system.
  • Zoom: Get argument 'zoom' used in the destination syntax.
  • PageObjNum: Get object number of the target page.

About C# Enum PDFDestinationMode

  • XYZ: Format: [page /XYZ left top zoom] Display the page designated by page, with the coordinates (left, top) positioned at the upper-left corner of the window and the contents of the page magnified by the factor zoom.
  • Fit: Format: [page /Fit] Display the page designated by page, with its contents magnified just enough to fit the entire page within the window both horizontally and vertically.
  • FitH: Format: [page /FitH top] Display the page designated by page, with the vertical coordinate top positioned at the top edge of the window and the contents of the page magnified just enough to fit the entire width of the page within the window.
  • FitV: Format: [page /FitV left] Display the page designated by page, with the horizontal coordinate left positioned at the left edge of the window and the contents of the page magnified just enough to fit the entire height of the page within the window.
  • FitR: Format: [page /FitR left bottom right top] Display the page designated by page, with its contents magnified just enough to fit the rectangle specified by the coordinates left, bottom, right, and top entirely within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centering the rectangle within the window in the other dimension.
  • FitB: Format: [page /FitB] Display the page designated by page, with its contents magnified just enough to fit its bounding box entirely within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centering the bounding box within the window in the other dimension.
  • FitBH: [page /FitBH top] Display the page designated by page, with the vertical coordinate top positioned at the top edge of the window and the contents of the page magnified just enough to fit the entire width of its bounding box within the window.
  • FitBV: [page /FitBV left] Display the page designated by page, with the horizontal coordinate left positioned at the left edge of the window and the contents of the page magnified just enough to fit the entire height of its bounding box within the window.






Read, extract PDF open action setting in C#


String inputFilePath = @"C:\1.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Get Open Action from the document, the return is a PDF destination.
PDFDestination op = doc.GetOpenActionDestination();
if (op != null)
    Console.WriteLine("Mode: {0}; Page Index: {1}", op.Mode, op.PageIdx);
else
    Console.WriteLine("No OpenAction");




Update PDF open action setting using C#


String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Create a /FitB destination for the Open Action, which would show the 2nd page when open the document.
PDFDestination op = PDFDestination.CreateFitB(1);
//  Update open action setting
doc.SetOpenActionDestination(op);
//  Save file
doc.Save(outputFilePath);


String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\output.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);
//  Create a /XYZ destination for the Open Action with zoom is 200% and top-left point is [0,0].
int pageIndex = 1;
PointF startPoint = new PointF(0, 0);
double zoom = 2.0;
PDFDestination op = PDFDestination.CreateXYZ(pageIndex, startPoint.X, startPoint.Y, zoom);
//  Update open action setting
doc.SetOpenActionDestination(op);
//  Save file
doc.Save(outputFilePath);