> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/optimization/viewer-optimized.md).

# Optimizing PDF files on Server/Desktop for fast viewing

Learn how to create and open viewer optimized PDF files for fast loading and viewing. Use SaveViewerOptimized function or DocPub utility to compress and optimize PDFs efficiently. Optimize your PDFs n

Viewer optimized PDF is a unique way of near-instantaneous loading and viewing of remotely stored PDFs regardless of their size or complexity. A Viewer optimized file is an optimized PDF document that allows the Viewer to display a document preview even before all client-side components are loaded. In the past, a Viewer could stream arbitrary large documents for viewing optimization using standard [PDF linearization](https://apryse.com/blog/pdf-format/what-is-pdf-linearization), but we have extended this concept to support fast viewing of complex pages which are slow to render as well.

## Create a viewer optimized PDF

To optimize a PDF, use the `SaveViewerOptimized` function with default or custom settings on desktop or server.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
using (PDFDoc doc = new PDFDoc(filename)) {
  // Options for SaveViewerOptimized
  ViewerOptimizedOptions options = new ViewerOptimizedOptions();

  // A number from 0 (include all thumbnails) to 100
  // (include only the first thumbnail). The default value is 50.
  options.SetThumbnailRenderingThreshold(40);

  // The maximum allowed length for the thumbnail's height/width.
  // The default thumbnail size is 1024.
  options.SetThumbnailSize(1500);

  // Optimize pdf
  doc.SaveViewerOptimized(output, options);

  doc.Close(); // release resources
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);

// Options for SaveViewerOptimized
ViewerOptimizedOptions viewer_optimized_opts;

// A number from 0 (include all thumbnails) to 100
// (include only the first thumbnail). The default value is 50.
viewer_optimized_opts.SetThumbnailRenderingThreshold(40);

// The maximum allowed length for the thumbnail's height/width.
// The default thumbnail size is 1024.
viewer_optimized_opts.SetThumbnailSize(1024);

// Optimize pdf
doc.SaveViewerOptimized(output, viewer_optimized_opts);

doc.Close();  // release resources
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
doc := NewPDFDoc(filename)

// Options for SaveViewerOptimized
opts := NewViewerOptimizedOptions()

// A number from 0 (include all thumbnails) to 100
// (include only the first thumbnail). The default value is 50.
opts.SetThumbnailRenderingThreshold(40)

// The maximum allowed length for the thumbnail's height/width.
// The default thumbnail size is 1024.
opts.SetThumbnailSize(1024)

// Optimize pdf
doc.SaveViewerOptimized(outputPath + inputFileName + "_SaveViewerOptimized.pdf", opts)
doc.Close()
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc(filename);

// Options for SaveViewerOptimized
ViewerOptimizedOptions options = new ViewerOptimizedOptions();

// A number from 0 (include all thumbnails) to 100
// (include only the first thumbnail). The default value is 50.
options.setThumbnailRenderingThreshold(0);

// The maximum allowed length for the thumbnail's height/width.
// The default thumbnail size is 1024.
options.setThumbnailSize(1024);

// Optimize pdf
doc.saveViewerOptimized(output, options);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}

<pre class="language-js" data-line-numbers><code class="lang-js">const main = async () => {
  const inputFilename = 'newsletter';
  const inputFilePath = '../files/' + inputFilename + '.pdf';
  const outputFilePath = '../output/' + inputFilename + '_optimized.pdf';
  const doc = await PDFNet.PDFDoc.createFromFilePath(inputFilePath);

  const opts = new PDFNet.PDFDoc.ViewerOptimizedOptions();

  // A number from 0 (include all thumbnails) to 100
  // (include only the first thumbnail). The default value is 50.
  opts.setThumbnailRenderingThreshold(0);

  // The maximum allowed length for the thumbnail's height/width.
  // The default thumbnail size is 1024.
  opts.setThumbnailSize(512);

  // Optimize pdf
  await doc.saveViewerOptimized(outputFilePath, opts);
};

// add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, '<code class="expression">visitor.claims.serverKey || "YOUR_LICENSE_KEY"</code>')
PDFNet.runWithCleanup(main).then(function() {
  PDFNet.shutdown();
});
</code></pre>

{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];

// Options for SaveViewerOptimized
PTViewerOptimizedOptions *options = [[PTViewerOptimizedOptions alloc] init];

// A number from 0 (include all thumbnails) to 100
// (include only the first thumbnail). The default value is 50.
[options SetThumbnailRenderingThreshold: 0];

// The maximum allowed length for the thumbnail's height/width.
// The default thumbnail size is 1024.
[options SetThumbnailSize: 1500];

// Optimize pdf
[doc SaveViewerOptimized: output opts: options];
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$doc = new PDFDoc($filename);

// Options for SaveViewerOptimized
$options = new ViewerOptimizedOptions();

// The maximum allowed length for the thumbnail's height/width.
// The default thumbnail size is 1024.
$options->SetThumbnailSize(1024);

// A number from 0 (include all thumbnails) to 100
// (include only the first thumbnail). The default value is 50.
$options->SetThumbnailRenderingThreshold(40);

// Optimize pdf
$doc->SaveViewerOptimized($outputfile, $options);

$doc->close();
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
doc = PDFDoc(filename)

# Options for SaveViewerOptimized
options = ViewerOptimizedOptions()

# The maximum allowed length for the thumbnail's height/width.
# The default thumbnail size is 1024.
options.SetThumbnailSize(1024)

# A number from 0 (include all thumbnails) to 100
# (include only the first thumbnail). The default value is 50.
options.SetThumbnailRenderingThreshold(40)

# Optimize pdf
doc.SaveViewerOptimized(output, options)

doc.Close()
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc = PDFDoc.new(filename)

# Options for SaveViewerOptimized
options = ViewerOptimizedOptions.new()

# The maximum allowed length for the thumbnail's height/width.
# The default thumbnail size is 1024.
options.SetThumbnailSize(1024)

# A number from 0 (include all thumbnails) to 100
# (include only the first thumbnail). The default value is 50.
options.SetThumbnailRenderingThreshold(40)

# Save the document as a viewer optimized PDF
doc.SaveViewerOptimized("output-x.pdf", options)

doc.Close()
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Using doc As PDFDoc = New PDFDoc(filename)

  ' Options for SaveViewerOptimized
  Dim opts As New ViewerOptimizedOptions

  ' A number from 0 (include all thumbnails) to 100
  ' (include only the first thumbnail). The default value is 50.
  opts.SetThumbnailRenderingThreshold(0);

  ' The maximum allowed length for the thumbnail's height/width.
  ' The default thumbnail size is 1024.
  opts.SetThumbnailSize(1500)

  ' Optimize pdf
  doc.SaveViewerOptimized(output, opts)
End Using
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Compress & optimize PDF files](/core/get-started/samples/optimizertest.md) Full sample code shows how to use `SaveViewerOptimized` to embed thumbnails with a PDF file which will enable fast rendering in the viewer. Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.

## Create a viewer optimized PDF using DocPub

[DocPub](/cli/docpub/docpub.md) is a command-line utility which provides an alternative way of generating viewer optimized PDF files.

To create a viewer optimized PDF, use the command `docpub -f pdf --viewer_optimized file.pdf`.

{% tabs %}
{% tab title="Shell" %}
{% code lineNumbers="true" %}

```sh
./docpub -f pdf --viewer_optimized demo.pdf --fname demo.optimized.pdf
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Open a viewer optimized PDF

The opening of the optimized PDF is no different than opening regular PDF files. By default, the Viewer is able to recognize a PDF that is Viewer optimized and will utilize this enhancement.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/optimization/viewer-optimized.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
