Setting Viewer Preferences for a PDF

The code below shows you how to set viewer preferences for a PDF

This sample requires a Tools license.

C#
using System;
using SolidFramework.Plumbing;
using SolidFramework.Pdf;
using SolidFramework.Pdf.Plumbing;

namespace PDFViewPref
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Call your Solid Documents License
            SolidFramework.License.Import(@"C:\MyFolder\license.xml");

            // Set the location of your the file you want to convert
            String pdfPath = @"C:\YourFolder\yourpdf.pdf";

            //*PDF Viewer Preferences*//

            // Create a new PdfDocument Object called document and assign the selected PDF file to it 
            using (PdfDocument document = new PdfDocument(pdfPath))
            {
                // Open the file
                document.Open();

                // Get the catelogue object which contains viewer preferences options
                SolidFramework.Pdf.Catalog catalogue = (SolidFramework.Pdf.Catalog)document.Catalog;

                // Assign or read any of the ViewerPreferences values
                if (catalogue.ViewerPreferences == null)
                {
                    PdfDictionary viewerPreferences = new PdfDictionary(document);
                    document.CatalogDictionary["ViewerPreferences"] = viewerPreferences;
                }

                catalogue.ViewerPreferences.HideMenuBar = true;
                catalogue.ViewerPreferences.HideToolBar = true;  

                // If the document has been modified, save it
                if (document.IsModified)
                {
                    document.Save(OverwriteMode.ForceOverwrite);
                }
            }
        }
    }
}
C++
#include "stdafx.h"
#include "SolidFramework.h"

using namespace std;

int main()
{
    // Call your Solid Documents License
    SolidFramework::License::Import(L"c:\\MyFolder\\license.xml");

    // Add the PDF file to convert
    wstring pdfPath(L"c:\\yourfolder\\yourpdf.pdf");

    //*PDF Viewer Preferences*//

    // Create a new PdfDocument Object called document and assign the selected PDF file to it 
    SolidFramework::Pdf::PdfDocumentPtr document(new SolidFramework::Pdf::PdfDocument(pdfPath));
    // Open the file
    document->Open();

    // Get the catelogue object which contains viewer preferences options
    SolidFramework::Pdf::CatalogPtr catalogue = SolidFramework::Pdf::Catalog::DynamicCast(document->getCatalog());

    // Assign or read any of the ViewerPreferences values
    if (catalogue->getViewerPreferences() == NULL)
    {
        SolidFramework::Pdf::ViewerPreferencesPtr viewerPreferences(new SolidFramework::Pdf::ViewerPreferences());
        catalogue->setViewerPreferences(viewerPreferences);
    }

    SolidFramework::Pdf::ViewerPreferencesPtr viewerPreferences = catalogue->getViewerPreferences();

    viewerPreferences->setHideMenuBar(true);
    viewerPreferences->setHideToolBar(true);

    catalogue->setViewerPreferences(viewerPreferences);

    // If the document has been modified, save it
    if (document->getIsModified())
    {
        document->Save(SolidFramework::Plumbing::OverwriteMode::ForceOverwrite, SolidFramework::PageRange::Parse(L"All"));
    }

    return 0;
}
VB.Net
Module PDFViewPref

    Sub Main()
        'Define a variable for your source file
        Dim sPdfPath As String
        Dim sPdfCopy As String

        ' Define a PDF Document object
        Dim myDocument As SolidFramework.Pdf.PdfDocument

        ' Define a viewer preferences catelog
        Dim myCatelog As SolidFramework.Pdf.Catalog

        Dim myPreferences As SolidFramework.Pdf.Plumbing.PdfDictionary

        ' Set up the file paths 
        sPdfPath = "C:\YourFolder\filepdf.pdf"
        sPdfCopy = "C:\YourFolder\filepdf-copy.pdf"

        ' Copy the original over to a copy
        FileCopy(sPdfPath, sPdfCopy)

        ' Call your Solid Documents License
        SolidFramework.License.Import("C:\MyFolder\license.xml")

        ' Open the copied pdf file for modification
        myDocument = New SolidFramework.Pdf.PdfDocument(sPdfCopy)
        myDocument.Open(sPdfCopy)

        ' Get the document's Catalog
        myCatelog = myDocument.Catalog()

        ' If Viewer Preferences dictionary does not exists, create one
        If IsNothing(myCatelog.ViewerPreferences) Then
            myPreferences = New SolidFramework.Pdf.Plumbing.PdfDictionary(myDocument)
            myDocument.CatalogDictionary("ViewerPreferences") = myPreferences
        End If

        ' Assign or read any of the ViewerPreferences values
        myCatelog.ViewerPreferences.HideMenuBar = True
        myCatelog.ViewerPreferences.HideToolBar = True

        ' Only save the document if we have modified the preferences.
        If myDocument.IsModified Then
            myDocument.Save(SolidFramework.Plumbing.OverwriteMode.ForceOverwrite)
        End If

        ' Close the document and clean up
        myDocument.Close()
        myDocument.Dispose()

    End Sub

End Module