When saving a DocumentModel to an image file, we use an ImageSaveOptions to specify various options for the resulting image.
When using a default options, the resulting image file will contain the first document's page content; this is because the default value of the ImageSaveOptions.PageNumber property is 0.
We can use this property in order to save each page as a separate image; the following is a demonstration sample that uses this "Sample.docx" as an input file:
C# code
DocumentModel document = DocumentModel.Load("Sample.docx");
ImageSaveOptions options = new ImageSaveOptions(ImageSaveFormat.Png);
int pageCount = document.GetPaginator().Pages.Count;
for (int i = 0; i < pageCount; i++)
{
options.PageNumber = i;
document.Save(string.Format("SamplePage{0}.png", i), options);
}
VB.NET code
Dim document As DocumentModel = DocumentModel.Load("Sample.docx")
Dim options As New ImageSaveOptions(ImageSaveFormat.Png)
Dim pageCount As Integer = document.GetPaginator().Pages.Count
For i As Integer = 0 To pageCount - 1
options.PageNumber = i
document.Save(String.Format("SamplePage{0}.png", i), options)
Next
The following are the resulting "SamplePageX.png" files:
Also if we want to create a single image that will contain all the document's pages, we can save each document's page into a Tiff frame, thus creating a single TIFF image file:
C# code
DocumentModel document = DocumentModel.Load("Sample.docx");
ImageSaveOptions options = new ImageSaveOptions(ImageSaveFormat.Tiff);
options.PageCount = int.MaxValue;
document.Save("Sample.tiff", options);
VB.NET code
Dim document As DocumentModel = DocumentModel.Load("Sample.docx")
Dim options As New ImageSaveOptions(ImageSaveFormat.Tiff)
options.PageCount = Integer.MaxValue
document.Save("Sample.tiff", options)
The following are the resulting "Sample.tiff" file's frames:
Comments (4)
How do I set the resolution (e.g. to 300dpi)?
Hi,
Currently GemBox.Document does not provide an API for setting the resolution, but note that internally it uses a 300 DPI.
Nevertheless if interested please feel free to create a new GemBox.Document Feature Request on our feedback page, but at this moment I cannot tell you exactly when it will be implemented.
Regards,
Mario
GemBox d.o.o.
Hi, We are also hoping to be able to produce a higher resolution TIFF image using the Document Save options - are you able to provide an update on when this feature may be implemented?
Thanks
Andy
Hi,
We have added a support for setting the DPI value through an ImageSaveOptions properties:
- DpiX: https://www.gemboxsoftware.com/document/help/html/P_GemBox_Document_ImageSaveOptions_DpiX.htm
- DpiY: https://www.gemboxsoftware.com/document/help/html/P_GemBox_Document_ImageSaveOptions_DpiY.htm
I hope this helps.
Regards,
Mario
GemBox d.o.o.