Generate document pages preview
Leave feedback
GroupDocs.Signature provides PreviewOptions class to specify different options to manage document pages preview generation process. The feature also supports archives previewing.
Here are the steps to generate document preview with GroupDocs.Signature:
- Create new instance of Signature class and pass source document path as a constructor parameter.
- Instantiate the PreviewOptions object with:
- delegate for each page stream creation (see event handler CreatePageStream);
- image preview format - PNG / JPG / BMP,
- page numbers to process;
- custom size of preview images (if needed).NoteStream that were created by CreatePageStream delegate will be disposed automatically once after generation of preview image. If you need to implement custom image preview stream disposing you have to pass additional argument ReleaseStream to clean up resources.
- Call GeneratePreview method of Signature class instance and pass PreviewOptions to it.
GroupDocs.Signature expects CreatePageStream delegate to obtain each page stream for image preview generation process
private static Stream CreatePageStream(int pageNumber)
{
string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
var folder = Path.GetDirectoryName(imageFilePath);
if(!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
return new System.IO.FileStream(imageFilePath, FileMode.Create);
}
private static void ReleasePageStream(int pageNumber, Stream pageStream)
{
pageStream.Dispose();
string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
Console.WriteLine("Image file {0} is ready for preview", imageFilePath);
}
public static void GetPreview()
{
using (Signature signature = new Signature("sample.pdf"))
{
// create preview options object
PreviewOptions previewOption = new PreviewOptions(GeneratePreview.CreatePageStream)
{
PreviewFormat = PreviewOptions.PreviewFormats.JPEG,
};
// generate preview
signature.GeneratePreview(previewOption);
}
}
private static Stream CreatePageStream(int pageNumber)
{
string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
var folder = Path.GetDirectoryName(imageFilePath);
if(!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
return new System.IO.FileStream(imageFilePath, FileMode.Create);
}
public static void GetPreview()
{
using (var stream = File.OpenRead("sample.pdf"))
{
using (Signature signature = new Signature("sample.pdf"))
{
// create preview options object
PreviewOptions previewOption = new PreviewOptions(GeneratePreview.CreatePageStream)
{
PreviewFormat = PreviewOptions.PreviewFormats.JPEG,
};
// generate preview
signature.GeneratePreview(previewOption);
}
}
}
private static Stream CreatePageStream(int pageNumber)
{
string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
var folder = Path.GetDirectoryName(imageFilePath);
if(!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
return new System.IO.FileStream(imageFilePath, FileMode.Create);
}
private static void ReleasePageStream(int pageNumber, Stream pageStream)
{
pageStream.Dispose();
string imageFilePath = Path.Combine("GeneratePreviewFolder", "image-" + pageNumber.ToString() + ".jpg");
Console.WriteLine("Image file {0} is ready for preview", imageFilePath);
}
To learn more about document eSign features, please refer to the advanced usage section.
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Signature for .NET examples, plugins, and showcase
- GroupDocs.Signature for Java examples, plugins, and showcase
- Document Signature for .NET MVC UI Example
- Document Signature for .NET App WebForms UI Example
- Document Signature for Java App Dropwizard UI Example
- Document Signature for Java Spring UI Example
Along with the full-featured .NET library, we provide simple but powerful free online apps.
To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.