---
title: ViewDocumentService
summary: API reference for ViewDocumentService.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# ViewDocumentService

Implements `IViewDocumentService`, retrieving the raw PDF bytes for a single output document or an entire document packet from the imaging and PDF merge subsystems, after enforcing per-policy authorization. It depends on `IPolicyDocumentsService` to look up document metadata, `IContextValidator` to confirm the current user may view the owning policy, `IImagingClient` to fetch individual page images, and `IPdfClient` to merge multiple documents into one packet.

## Purpose
- Retrieves a single document's PDF bytes by image ID, or a merged multi-document packet's PDF bytes, enforcing policy-level authorization first.
- Translates imaging/PDF-merge failures and missing documents into `FileNotFoundException`, and unauthorized policy access into `UnauthorizedAccessException`.

## Context
- Namespace: `SAIF.Services.OutputDocuments`
- Assembly/package: `SAIF.Services` (packaged inside the `SAIF.SAIFCode.Core` NuGet package)

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| ViewDocumentService(...) | `public ViewDocumentService(IPolicyDocumentsService policyDocumentsService, IContextValidator contextValidator, IPdfClient pdfClient, IImagingClient imagingClient)` | Wires in the document lookup, context validation, PDF merge, and imaging collaborators used to serve document/packet requests. |
| GetImageFile | `public byte[] GetImageFile(long imageId, string packetId)` | Looks up the document by `imageId`, validates the requesting user is authorized for its policy, then returns either the single page's PDF bytes or (when `packetId` is supplied) the merged packet's PDF bytes; throws `FileNotFoundException` if the document or image is missing and `UnauthorizedAccessException` if policy validation fails. |

## Behavioral notes
- Wraps `_contextValidator.ValidatePolicyNumber` in a try/catch that converts any `ContextException` into an `UnauthorizedAccessException`, hiding the specific validation reason from the caller.
- Retries the imaging/PDF fetch a second time if the first attempt returns a null byte array before giving up*.
- Packet retrieval orders the packet's documents by `DocumentType.SortOrder`, loads each page's PDF via `_imagingApiClient.GetPdf`, and merges them with `_pdfApiClient.Merge`; any exception during this process is reported as `FileNotFoundException("Packet is not available")`, discarding the original error detail.

> **Note:** Statements marked with * were inferred from code structure, naming conventions, or
> contextual evidence where direct confirmation was not available in source artifacts. Statements
> marked with ** could not be confirmed; treat as unknown until verified.

## Sources
- Services/SAIF.Services/OutputDocuments/ViewDocumentService.cs
