Codecs: How to set layout settings of XLSX document?
In This Topic
SDK allows to specify layout settings (page size, page padding, content scale) of XLSX document. Page layout settings can be specified for all pages, even and/or odd pages, pages specified by indices.
Here is C#/VB.NET code that shows how to set layout settings of XLSX document:
/// <summary>
/// Class shows how to convert XLSX document to PDF document using specified layout settings.
/// </summary>
public class XlsxConverter
{
/// <summary>
/// Converts XLSX document to PDF document using specified layout settings.
/// </summary>
public static void ConvertXlsxToPdf(string xlsxFileName, string pdfFileName)
{
// create image collection
using (Vintasoft.Imaging.ImageCollection imageCollection = new Vintasoft.Imaging.ImageCollection())
{
// subscribe to the ImageCollection.LayoutSettingsRequest event
imageCollection.LayoutSettingsRequest += ImageCollection_LayoutSettingsRequest;
// add XLSX document to the image collection
imageCollection.Add(xlsxFileName);
// create PDF encoder that will create new PDF file
using (Vintasoft.Imaging.Codecs.Encoders.PdfEncoder pdfEncoder =
new Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(true))
{
// save images of image collection to PDF document using PDF encoder
imageCollection.SaveSync(pdfFileName, pdfEncoder);
}
// dispose images
imageCollection.ClearAndDisposeItems();
}
}
/// <summary>
/// Handles the LayoutSettingsRequest event of the ImageCollection object.
/// </summary>
private static void ImageCollection_LayoutSettingsRequest(object sender, Vintasoft.Imaging.DocumentLayoutSettingsRequestEventArgs e)
{
// if XLSX codec is used
if (e.Codec.Name == "Xlsx")
{
// change layout settings of XLSX document
Vintasoft.Imaging.Codecs.Decoders.XlsxDocumentLayoutSettings layoutSettings = (Vintasoft.Imaging.Codecs.Decoders.XlsxDocumentLayoutSettings)e.LayoutSettings;
// set page width to worksheet width
layoutSettings.PageLayoutSettingsType |= Vintasoft.Imaging.Codecs.Decoders.XlsxPageLayoutSettingsType.UseWorksheetWidth;
// set page height to A4
layoutSettings.PageLayoutSettings = new Vintasoft.Imaging.Codecs.Decoders.PageLayoutSettings(Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4));
}
}
}
''' <summary>
''' Class shows how to convert XLSX document to PDF document using specified layout settings.
''' </summary>
Public Class XlsxConverter
''' <summary>
''' Converts XLSX document to PDF document using specified layout settings.
''' </summary>
Public Shared Sub ConvertXlsxToPdf(xlsxFileName As String, pdfFileName As String)
' create image collection
Using imageCollection As New Vintasoft.Imaging.ImageCollection()
' subscribe to the ImageCollection.LayoutSettingsRequest event
AddHandler imageCollection.LayoutSettingsRequest, AddressOf ImageCollection_LayoutSettingsRequest
' add XLSX document to the image collection
imageCollection.Add(xlsxFileName)
' create PDF encoder that will create new PDF file
Using pdfEncoder As New Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(True)
' save images of image collection to PDF document using PDF encoder
imageCollection.SaveSync(pdfFileName, pdfEncoder)
End Using
' dispose images
imageCollection.ClearAndDisposeItems()
End Using
End Sub
''' <summary>
''' Handles the LayoutSettingsRequest event of the ImageCollection object.
''' </summary>
Private Shared Sub ImageCollection_LayoutSettingsRequest(sender As Object, e As Vintasoft.Imaging.DocumentLayoutSettingsRequestEventArgs)
' if XLSX codec is used
If e.Codec.Name = "Xlsx" Then
' change layout settings of XLSX document
Dim layoutSettings As Vintasoft.Imaging.Codecs.Decoders.XlsxDocumentLayoutSettings = DirectCast(e.LayoutSettings, Vintasoft.Imaging.Codecs.Decoders.XlsxDocumentLayoutSettings)
' set page width to worksheet width
layoutSettings.PageLayoutSettingsType = layoutSettings.PageLayoutSettingsType Or Vintasoft.Imaging.Codecs.Decoders.XlsxPageLayoutSettingsType.UseWorksheetWidth
' set page height to A4
layoutSettings.PageLayoutSettings = New Vintasoft.Imaging.Codecs.Decoders.PageLayoutSettings(Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4))
End If
End Sub
End Class