Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions common-information/clear-output-stream-on-export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Automatic Output Stream Clearing on Export
description: The Export methods of all format providers automatically clear the output stream before writing, preventing corrupted documents when reusing streams.
page_title: Automatic Output Stream Clearing on Export
slug: common-export-output-stream-clearing
tags: export, stream, clear, format, provider, document, processing
published: True
position: 0
---

# Automatic Output Stream Clearing on Export

|Minimum Version|Q2 2026|
|----|----|

As of Q2 2026, the **Export()** methods of all [format providers]({%slug introduction%}#supported-formats) in the Telerik Document Processing Libraries automatically clear the output stream before writing new content. When the **Export()** method is called on a seekable stream, the stream is truncated to zero length prior to writing, ensuring that no stale data remains.

## Behavioral Change

When reusing a **Stream** for multiple consecutive export operations, for example, exporting to a **MemoryStream** or a **FileStream** opened with `FileMode.OpenOrCreate`, the previously written content could remain at the tail of the stream if the new document was smaller than the old one. This caused corrupted output files because trailing bytes from the earlier export persisted after the new export completed.

The stream clearing is now performed automatically by the base classes that all format providers inherit from. The affected format providers include:

| Library | Format Providers |
|---|---|
| RadPdfProcessing | **PdfFormatProvider**, **SkiaImageFormatProvider** |
| RadWordsProcessing | **DocxFormatProvider**, **DocFormatProvider**, **RtfFormatProvider**, **HtmlFormatProvider**, **TxtFormatProvider**, **PdfFormatProvider** |
| RadSpreadProcessing | **XlsxFormatProvider**, **XlsFormatProvider**, **XlsmFormatProvider**, **CsvFormatProvider**, **TxtFormatProvider**, **PdfFormatProvider**, **JsonFormatProvider** |

>note The stream is cleared only when **Stream.CanSeek** returns `true`. Non-seekable streams (such as network streams) are not affected and continue to behave as before.

No code changes are required on the consumer side. The clearing happens automatically inside the **Export()** methods.

## Using the Export Methods with Reused Streams

#### Example 1: Reusing a MemoryStream for multiple exports

<snippet id='reuse-stream-for-multiple-exports'/>

When reusing a stream, the second **Export()** call truncates the stream to zero length before writing, so only the new document data is present.

#### Example 2: Exporting to a FileStream

<snippet id='export-to-filestream'/>

Even when opening the file with `FileMode.OpenOrCreate`, the stream is cleared automatically so no leftover data from a previous file remains.

>tip If you open a **FileStream** with `FileMode.Create`, the file is already truncated by the OS, so the automatic clearing has no additional effect in that scenario.

## Backward Compatibility

>important This is a **behavioral change** in existing **Export()** methods. Code that intentionally preserves prior stream content before calling **Export()** is affected. The stream is now always truncated before writing. If you need to prepend content, write it after the export completes, or use a separate stream and combine them afterward.

For the majority of usage scenarios, where the stream is either freshly created or being reused for a new export, this change is transparent and prevents potential file corruption.

## See Also

* [Using PdfFormatProvider in RadPdfProcessing]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%})
* [Using DocxFormatProvider in RadWordsProcessing]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%})
* [Timeout Mechanism in Document Processing Libraries]({%slug timeout-mechanism-in-dpl%})
1 change: 1 addition & 0 deletions introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Telerik Document Processing features the following libraries:
|**Timeout Mechanism**|[Timeout Mechanism]({%slug timeout-mechanism-in-dpl%}) for importing and exporting documents. The **Import** and **Export** methods of all FormatProviders have a mandatory *TimeSpan?* timeout parameter after which the operation will be cancelled.|
|**GenAI-powered Document Insights**|Enables you to easily extract insights from PDF documents using Large Language Models (LLMs). This functionality enables you to summarize document content and ask questions about it, with the AI providing relevant answers based on the document's content. [[Read More]]({%slug radpdfprocessing-features-gen-ai-powered-document-insights-overview%})|
|**AI Coding Assistant**|Provides specialized context to AI models, enabling them to produce higher-quality code samples. [[Read More]]({%slug ai-coding-assistant%})|
|**Automatic Output Stream Clearing on Export**|Automatically clears the output stream before writing new content. [[Read More]]({%slug common-export-output-stream-clearing%})|

For more details about the benefits of using Telerik Document Processing, see the [Telerik Document Processing product overview page](https://www.telerik.com/document-processing-libraries).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Learn what are the limitations related to the usage of digital sign
page_title: Limitations
slug: radpdfprocessing-features-digital-signature-limitations
tags: digital, signature, limitations
position: 5
position: 7
---

# Limitations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: The digital signature feature enables you to sign and validate a PD
page_title: Signature Validation
slug: radpdfprocessing-features-digital-signature-validation
tags: digital, signature, validate
position: 4
position: 6
---

## Validating a Signature
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: Signing Existing Signature Fields
description: Learn how to sign multiple pre-existing signature fields in a PDF document using incremental updates with the PdfStreamSigner class in RadPdfProcessing.
page_title: Signing Existing Signature Fields
slug: radpdfprocessing-features-signing-existing-signature-fields
tags: pdf, processing, digital, signature, signing, incremental, update, field
published: True
position: 5
---

# Signing Existing Signature Fields

|Minimum Version|Q2 2026|
|----|----|

**RadPdfProcessing** enables you to sign multiple pre-existing signature fields in a PDF document while preserving the validity of previously applied signatures. The `PdfStreamSigner` class applies each signature through an *incremental update*, which appends data to the original PDF instead of rewriting it. This approach ensures that each subsequent signature does not invalidate earlier ones.

PDF documents often contain multiple unsigned signature fields, for example, a contract that requires approval from several parties. When you sign these fields one at a time, each signing operation must preserve the integrity of all previous signatures. The `SignExistingField` method of the `PdfStreamSigner` class addresses this requirement. It locates a pre-existing signature field by name, applies a digital signature, and writes the result as an incremental update to the output stream. You can chain multiple signing operations by feeding the output of one step as the input to the next.

## API Reference

The following table lists the `SignExistingField` method overloads available on the `PdfStreamSigner` class.

| Method signature | Description |
|---|---|
| `SignExistingField(Stream originalStream, string fieldName, Signature signature)` | Signs an existing signature field identified by `fieldName` in the PDF document from `originalStream`. Uses incremental update to preserve previously applied signatures. |
| `SignExistingField(Stream originalStream, string fieldName, Signature signature, FormSource appearance)` | Signs an existing signature field and replaces the widget visual appearance with the specified `FormSource` content. Pass `null` for `appearance` to preserve the existing appearance. |

The following table describes the parameters accepted by the `SignExistingField` method.

| Parameter | Type | Description |
|---|---|---|
| `originalStream` | `Stream` | The stream that contains the current PDF document. |
| `fieldName` | `string` | The name of the pre-existing signature field to sign. |
| `signature` | `Signature` | The digital signature to apply to the field. |
| `appearance` | `FormSource` | The visual appearance to set on the signature widget. Pass `null` to keep the existing appearance. Available only in the overload that accepts a `FormSource` parameter. |

## Signing Existing Fields

To sign multiple existing signature fields in a PDF document:

1. Import the PDF and identify the unsigned signature fields by name.
2. Create a `PdfStreamSigner` instance with an output stream.
3. Call `SignExistingField` for each field, passing the current document stream and the field name.
4. Use the output stream from each step as the input for the next signing operation.

#### Example 1: Import a PDF and Sign All Signature Fields

The following example imports an existing PDF that contains unsigned signature fields and signs each field sequentially using incremental updates.

<snippet id='pdf-signature-sign-all-signature-fields'/>

The code first imports the document to discover the names of all unsigned `SignatureField` instances. It then iterates over the field names and calls `SignExistingField` for each one. Because each call produces an incremental update, previously applied signatures remain valid in the final output.

## Signing with Custom Appearance

When a PDF contains empty (unsigned) signature fields, you can generate and apply a visual appearance during signing. Create a `FormSource`, draw content into it with `FixedContentEditor`, and pass it to the `SignExistingField` overload that accepts an `appearance` parameter. Each signing step produces an incremental update, so previously applied signatures remain valid.

#### Example 2: Sign Existing Fields with Custom Appearance

The following example signs two signature fields and applies a custom visual appearance to each widget at signing time.

<snippet id='pdf-signature-sign-signature-fields-with-appearance'/>

The helper method below builds a visual appearance that displays the signer name, role, and signing date:

<snippet id='pdf-signature-build-appearance'/>

Each call to `SignExistingField` with a `FormSource` replaces the widget appearance of the target field. Pass `null` for the `appearance` parameter to preserve the existing widget appearance.

## See Also

* [Digital Signature in RadPdfProcessing]({%slug radpdfprocessing-features-digital-signature%})
* [Interactive Forms]({%slug radpdfprocessing-model-interactive-forms-form-fields%})
* [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%})
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ The resulting document can be opened in any application that supports PDF docume
* [PdfFormatProvider API Reference](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider.html)
* [Settings]({%slug radpdfprocessing-formats-and-conversion-pdf-settings%})
* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ __Example 1__ shows how to use __TextFormatProvider__ to export __RadFixedDocume
* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Extracting Text from PDF Documents]({%slug extract-text-from-pdf%})
* [Summarizing the Text Content of PDF Documents using Text Analytics with Azure AI services]({%slug summarize-pdf-content%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ __Example 2__ demonstrates how to export an existing Workbook to a CSV file. The
* [CsvFormatProvider API Reference](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv.CsvFormatProvider.html)
* [Import/Load and Export/Save RadSpreadProcessing Workbook]({%slug import-export-save-load-workbook%})
* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ __Example 2__ demonstrates how to export an existing Workbook to an xlsx file. T
* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Resolve Exporting Corrupted Excel Files With SpreadProcessing]({%slug resolving-excel-file-corruption-warning-after-spreadprocessing-export%})
* [Opening Excel Files Locked by Another User/Process with Telerik SpreadProcessing]({%slug spreadprocessing-open-locked-files-read-only%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ __Example 2__ demonstrates how to export the contents of a __RadFlowDocument__ t

* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [[WordsProcessing PDF Export Demo](https://demos.telerik.com/document-processing/wordsprocessing/pdf_export)]
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ You can also export the document to a string and preserve it in a database.
## See Also

* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ The resulting documents can be opened in any application that supports RTF docum
* [Using HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%})
* [Using PdfFormatProvider]({%slug radwordsprocessing-formats-and-conversion-pdf-pdfformatprovider%})
* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})

Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ The resulting **RadFlowDocument** can be manipulated like any code-generated doc
## See Also

* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ The resulting documents can be opened in any application that supports DOCX docu
## See Also

* [Timeout Mechanism]({%slug timeout-mechanism-in-dpl%})
* [Automatic Output Stream Clearing on Export]({%slug common-export-output-stream-clearing%})