Render archives as HTML, PDF, and image files
Leave feedback
On this page
GroupDocs.Viewer for Python via .Net allows you to view the contents of archive files in HTML, PDF, PNG, and JPEG formats. You do not need to use third-party file archiver and compression software to display archive file contents within your .NET application (web or desktop).
To start using the GroupDocs.Viewer API, create a Viewer class instance. Pass an archive file you want to view to the class constructor. You can load the archive from a file or stream. Call one of the Viewer.view method overloads to convert the archive file to HTML, PDF, or image format.
withgv.Viewer("Documents.zip")asviewer:# Create an HTML file for the top folder and each subfolder in the archive.# {0} is replaced with the current page number in the output file name.viewOptions=gvo.HtmlViewOptions.for_embedded_resources("page_{0}.html")viewer.view(viewOptions)
The following image demonstrates the result:
Specify the number of items to render
GroupDocs.Viewer supports the HtmlViewOptions.archive_options.items_per_page option that allows you to specify the number of archive items to display on each HTML page. The default property value is 16.
The following example demonstrates how to set this option in code:
withgv.Viewer("Documents.zip")asviewer:# Create an HTML file for the top folder and each subfolder in the archive.# {0} is replaced with the current page number in the output file name.viewOptions=gvo.HtmlViewOptions.for_embedded_resources("page_{0}.html")# Specify the number of items to display on each HTML page.viewOptions.archive_options.items_per_page=10viewer.view(viewOptions)
Create a single HTML page
If you need to display the contents of an archive file on a single HTML page, enable the HtmlViewOptions.render_to_single_page option, as shown below:
withgv.Viewer("Documents.zip")asviewer:# Create an HTML file.viewOptions=gvo.HtmlViewOptions.for_embedded_resources("output.html")# Render the archive file to a single page.viewOptions.render_to_single_page=Trueviewer.view(viewOptions)
The animation below demonstrates the result. You can navigate between the archive folders. Click on a particular folder to see its contents. To go backward, click the required folder name in the navigation bar at the top of the web page.
Render archive files as PDF
Create a PdfViewOptions class instance and pass it to the Viewer.view method to convert an archive file to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file or reorder its pages. Refer to the following documentation section for details: Rendering to PDF.
withgv.Viewer("Documents.zip")asviewer:# Create a PDF file.viewOptions=gvo.PdfViewOptions("output.pdf")viewer.view(viewOptions)
withgv.Viewer("Documents.zip")asviewer:# Create a PNG image for the top folder and each subfolder in the archive.# {0} is replaced with the current page number in the image name.viewOptions=gvo.PngViewOptions("output_{0}.png")# Set width and height.viewOptions.width=800viewOptions.height=1000viewer.view(viewOptions)
withgv.Viewer("Documents.zip")asviewer:# Create a JPEG image for the top folder and each subfolder in the archive.# {0} is replaced with the current page number in the image name.viewOptions=gvo.JpgViewOptions("output_{0}.jpg")# Set width and height.viewOptions.width=800viewOptions.height=1000viewer.view(viewOptions)
Obtain information about folders in an archive file
Call the Viewer.get_view_info method, pass the ViewInfoOptions instance to this method as a parameter, and cast the returned object to the ArchiveViewInfo type.
Use the ArchiveViewInfo.folders property to obtain the lists of folders in the archive file.
importgroupdocs.viewerasgvimportgroupdocs.viewer.optionsasgvoimportgroupdocs.viewer.resultsasgvrwithgv.Viewer("Documents.zip")asviewer:info=viewer.get_view_info(gvo.ViewInfoOptions.for_html_view())print("File type:",info.file_type)print("The number of pages:",len(info.pages))print("Folders:")print(" - /")root_folder=""read_folders(viewer,root_folder)defread_folders(viewer,folder):options=gvo.ViewInfoOptions.for_html_view()options.archive_options.folder=folderview_info=viewer.get_view_info(options)archive_view_info=cast(gvr.ArchiveViewInfo,view_info)forsub_folderinarchive_view_info.folders:print(f" - {sub_folder}")read_folders(viewer,sub_folder)
The following image shows a sample console output:
GroupDocs.Viewer also allows you to list and extract all files contained in the archive. Refer to the following help topics for details:
When you convert an archive file to HTML, PDF, or image format, GroupDocs.Viewer renders items from all folders contained in the archive. If you need to render items from a specific folder, specify the ArchiveOptions.folder property for one of the following classes (depending on the output file format):
withgv.Viewer("Documents.zip")asviewer:# Create an HTML file.viewOptions=gvo.HtmlViewOptions.for_embedded_resources("output.html")options.archive_options.folder="Documents/CAD files"viewer.view(viewOptions)
Specify the archive file name
When rendering an archive file, GroupDocs.Viewer displays the archive file name in the header of each page. If you need to change or hide this name, define the archive_options.file_name property for a target view. You can set this option to one of the following values:
FileName.SOURCE— Returns the name of the source file (this name is used by default).
FileName.EMPTY—Specifies an empty name. Use this value to hide the archive file name in the output file.
A FileName instance with a custom name you want to display in the output file.
The following code snippet demonstrates how to use a custom name when rendering an archive file to HTML:
withgv.Viewer("Documents.zip")asviewer:# Create an HTML file for the top folder and each subfolder in the archive.# {0} is replaced with the current page number in the output file name.viewOptions=gvo.HtmlViewOptions.for_embedded_resources("page_{0}.html")# Specify a custom filenameoptions.archive_options.file_name=gvo.FileName("Sample Files")viewer.view(viewOptions)
The image below demonstrates the result.
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.