Setting Viewer Preferences for a PDF
The code below shows you how to set viewer preferences for a PDF
It is recommended that you have already reviewed the Getting Started sample, since that includes Licensing and Framework initialization code required to make this sample run.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using SolidFramework.Pdf;
namespace CSharp_Tutorials
{
public static partial class Tutorials
{
public static bool SetViewerPreferences(string pdfPath, string outputPath)
{
// Create a PdfDocument and open the PDF file
using (var document = new PdfDocument(pdfPath))
{
document.Open();
// Get the documents catalog object
var catalog = document.Catalog as Catalog;
// Get the catalogs ViewerPreferences object (creating it if it doesn't exist)
var viewerPreferences = catalog.GetViewerPreferences(true);
// Modify the viewer preferences to your liking
viewerPreferences.HideMenuBar = true;
viewerPreferences.HideToolBar = true;
viewerPreferences.HideWindowUI = true;
viewerPreferences.FitWindow = true;
Console.WriteLine("Updating viewer preferences for " + outputPath);
// Save the updated document
document.SaveAs(outputPath, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite);
}
Console.WriteLine("Successfully updated viewer preferences for " + outputPath);
Console.WriteLine();
return true;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Imports System
Imports SolidFramework.Pdf
Namespace VBNet_Tutorials
Partial Module Tutorials
Function SetViewerPreferences(ByVal pdfPath As String, ByVal outputPath As String) As Boolean
' Create a PdfDocument And open the PDF file
Using document As PdfDocument = New PdfDocument(pdfPath)
document.Open()
' Get the documents catalog object
Dim catalog As Catalog = TryCast(document.Catalog, Catalog)
' Get the catalogs ViewerPreferences object (creating it if it doesn't exist)
Dim viewerPreferences As ViewerPreferences = catalog.GetViewerPreferences(True)
' Modify the viewer preferences to your liking
viewerPreferences.HideMenuBar = True
viewerPreferences.HideToolBar = True
viewerPreferences.HideWindowUI = True
viewerPreferences.FitWindow = True
Console.WriteLine("Updating viewer preferences for " & outputPath)
' Save the updated document
document.SaveAs(outputPath, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite)
End Using
Console.WriteLine("Successfully updated viewer preferences for " & outputPath)
Console.WriteLine()
Return True
End Function
End Module
End Namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "Tutorials.h"
using namespace SolidFramework::Pdf;
bool SetViewerPreferences(const wstring & pdfPath, const wstring & outputPath)
{
// Create a PdfDocument and open the PDF file
auto document = make_shared<PdfDocument>(pdfPath);
document->Open();
// Get the documents catalog object
auto catalog = Catalog::DynamicCast(document->GetCatalog());
// Get the catalogs ViewerPreferences object (creating it if it doesn't exist)
auto viewerPreferences = catalog->GetViewerPreferences(true);
// Modify the viewer preferences to your liking
viewerPreferences->SetHideMenuBar(true);
viewerPreferences->SetHideToolBar(true);
viewerPreferences->SetHideWindowUI(true);
viewerPreferences->SetFitWindow(true);
wcout << L"Updating viewer preferences for " << outputPath << endl;
// Save the updated document
document->SaveAs(outputPath, SolidFramework::Plumbing::OverwriteMode::ForceOverwrite);
wcout << L"Successfully updated viewer preferences for " << outputPath << endl << endl;
return true;
}