Back to Devexpress

How to: Set Paper Size

officefileapi-12404-spreadsheet-document-api-examples-worksheets-how-to-set-paper-size.md

latest3.3 KB
Original Source

How to: Set Paper Size

  • Sep 19, 2023
  • 2 minutes to read

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

The Spreadsheet Document API allows you to specify paper size for printed worksheets. Assign a PaperKind enumeration member to the Worksheet.ActiveView.PaperKind property to use one of the standard paper sizes (Letter, Legal, Tabloid, and so on).

The following example specifies paper size for the first worksheet:

csharp
using DevExpress.Drawing.Printing;
using DevExpress.Spreadsheet;
// ...

using(Workbook workbook = new Workbook())
{
    // Select paper size.
    workbook.Worksheets[0].ActiveView.PaperKind = DXPaperKind.Letter;
}
vb
Imports DevExpress.Drawing.Printing
Imports DevExpress.Spreadsheet
' ...

Using workbook As New Workbook()
    ' Select paper size.
    workbook.Worksheets(0).ActiveView.PaperKind = DXPaperKind.Letter
End Using

Customize Paper Size

Call the Worksheet.ActiveView.SetCustomPaperSize method to specify custom size for printed pages. Use the Workbook.Unit property to specify the measurement unit for the paper size (width and height).

The following example specifies a custom paper size for the first worksheet and exports the document to PDF:

csharp
using DevExpress.Office;
using DevExpress.Spreadsheet;
// ...

using(Workbook workbook = new Workbook())
{
    workbook.LoadDocument("Document.xlsx", DocumentFormat.Xlsx);

    // Set measurement unit to inches.
    workbook.Unit = DocumentUnit.Inch;

    // Specify custom paper size (10 inches by 12 inches).
    workbook.Worksheets[0].ActiveView.SetCustomPaperSize(10, 12);

    workbook.ExportToPdf("PdfDocument.pdf");
}
vb
Imports DevExpress.Office
Imports DevExpress.Spreadsheet
' ...

Using workbook As New Workbook()
    workbook.LoadDocument("Document.xlsx", DocumentFormat.Xlsx)

    ' Set measurement unit to inches.
    workbook.Unit = DocumentUnit.Inch

    ' Specify custom paper size (10 inches by 12 inches).
    workbook.Worksheets(0).ActiveView.SetCustomPaperSize(10, 12)

    workbook.ExportToPdf("PdfDocument.pdf")
End Using

The following image demonstrates the result:

Custom paper dimensions are saved to a file only when you export a workbook to Microsoft Office Open XML formats (XLSX, XLSM, XLTX, and XLTM). Custom size values are lost when you open and resave the document in Microsoft® Excel®.

See Also

How to: Print a Workbook

How to: Specify Print Settings