How to Start Convert PDF Read PDF Build PDF 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 SDK
C# PDF: how to format PDF Document


C# Demo Code to format Adobe PDF Document













PDF Document Window and Page Display Properties








Retrieve viewer preferences


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.");




Update viewer preferences


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 viewer preferences


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);




Retrieve page layout setting


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 page layout setting


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);




Retrieve page mode setting


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 page mode setting


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);






Zoom Factor of PDF Document








Retrieve open action setting


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 open action setting


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);