Aspose.Cells PDF Converter for .NET
Aspose.Cells PDF Converter for .NET provides a dedicated, high-performance solution to convert Excel spreadsheets (XLS, XLSX, XLTM, XLSM) into universally accessible PDF files while preserving layout fidelity and format consistency. With streamlined APIs, it ensures accurate rendering, advanced customization options, and smooth integration into .NET applications.
Installation and Setup
To integrate the converter into your project:
- Install the
Aspose.Cells
NuGet package (see Installation ). - Apply metered licensing before using the APIs (see Metered Licensing ).
Features and Functionalities
Comprehensive Format Support
- Convert from XLS, XLSX, XLTM, XLSM, XLSB, CSV, TSV, HTML, ODS, and more.
- Support for charts, pivot tables, images, shapes, and data connections.
- Round-trip accuracy when exporting complex workbooks.
High-Fidelity Layout and Formatting
- Preserve fonts, styles, colors, borders, margins, headers, and footers.
- Honor print areas, page breaks, and Excel’s print settings.
- Ensure precise alignment with original Excel print preview.
Selective Conversion
- Export the full workbook, individual sheets, or custom ranges.
- Perfect for dashboards, reports, and selective data exports.
- Minimize output file size by targeting only required sections.
Pagination Control
- One Page Per Sheet: Force each worksheet onto a single PDF page regardless of content size.
- Custom Page Breaks: Honor or override Excel’s built-in page break settings.
- Fit-to-Page Options: Scale worksheets to fit specific page dimensions.
- Multi-Page Handling: Control how large sheets split across multiple PDF pages.
Advanced PDF Options
- Define compliance targets (PDF/A-1b, PDF/A-2u, PDF/X-1a, PDF 1.4–2.0).
- Apply compression for text, images, and objects.
- Control image downsampling and quality thresholds.
- Add document metadata and properties.
Flexible Output Options
- File-Based Output: Save directly to disk with automatic directory creation.
- Stream-Based Output: Write to memory streams for in-memory processing.
- Network Streams: Output directly to network locations or cloud storage.
Encryption and Security
- Protect documents with AES encryption (40–256-bit).
- Restrict permissions for printing, copying, and editing.
- Support for digital signatures (via integration with other Aspose APIs).
Performance and Scalability
- Stream-based processing for large workbooks.
- Multi-threaded conversions for server-side scaling.
- Caching of fonts and resources to optimize throughput.
Logging and Error Handling
- Capture detailed warnings (e.g., missing fonts, unsupported features).
- Distinct exception types for licensing, format, and resource issues.
- Compatible with .NET logging frameworks for centralized diagnostics.
Usage Examples
Basic Conversion: Excel to PDF
The simplest way to convert an Excel file to PDF using a single line of code:
using Aspose.Cells.LowCode;
string src = "mytemplate.xlsx";
PdfConverter.Process(src, "result\\PluginPdf1.pdf");
This converts the entire workbook to PDF with default settings, preserving all sheets and formatting.
Advanced Conversion with Custom Options
Configure PDF-specific options for precise control over the output:
using Aspose.Cells.LowCode;
using Aspose.Cells;
string src = "mytemplate.xlsx";
// Configure load options
LowCodeLoadOptions lclopts = new LowCodeLoadOptions();
lclopts.InputFile = src;
// Configure PDF save options
LowCodePdfSaveOptions lcsopts = new LowCodePdfSaveOptions();
PdfSaveOptions pdfOpts = new PdfSaveOptions();
// Force each worksheet to fit on a single PDF page
pdfOpts.OnePagePerSheet = true;
lcsopts.PdfOptions = pdfOpts;
lcsopts.OutputFile = "result\\PluginPdf2.pdf";
// Perform conversion
PdfConverter.Process(lclopts, lcsopts);
Feature Breakdown: One Page Per Sheet
Control how worksheets are paginated in the PDF output:
PdfSaveOptions pdfOpts = new PdfSaveOptions();
// Each worksheet fits on exactly one PDF page (scaling applied if needed)
pdfOpts.OnePagePerSheet = true;
// Default behavior: respect Excel's page breaks (may create multiple pages per sheet)
pdfOpts.OnePagePerSheet = false;
Use Cases:
- Dashboard Exports: Ensure each dashboard fits on a single page for presentations
- Summary Reports: Keep overview sheets compact and readable
- Thumbnail Generation: Create single-page previews of worksheets
- Print-Ready Documents: Simplify printing by avoiding multi-page splits
- Executive Summaries: Present high-level data on one page
Before and After:
Without OnePagePerSheet:
- Large worksheet → Multiple PDF pages (respects page breaks)
- Sheet1: Pages 1-3, Sheet2: Pages 4-6
With OnePagePerSheet = true:
- Large worksheet → Scaled to fit single PDF page
- Sheet1: Page 1, Sheet2: Page 2
Feature Breakdown: File-Based vs Stream-Based Output
Choose the output method that best fits your scenario:
// File-based output (simplest)
lcsopts.OutputFile = "result\\PluginPdf2.pdf";
// Stream-based output (for in-memory processing)
MemoryStream ms = new MemoryStream();
lcsopts.OutputStream = ms;
// Network stream output (for cloud storage)
using (FileStream networkStream = new FileStream(@"\\server\share\output.pdf", FileMode.Create))
{
lcsopts.OutputStream = networkStream;
PdfConverter.Process(lclopts, lcsopts);
}
File-Based Output Advantages:
- Simpler syntax
- Automatic directory creation
- Lower memory usage
- Ideal for batch processing
Stream-Based Output Advantages:
- No disk I/O required
- Flexible routing (web response, cloud, email)
- In-memory caching
- Ideal for web APIs
Feature Breakdown: LowCode API Benefits
Compare the traditional and LowCode approaches:
// Traditional API (verbose, more control)
Workbook workbook = new Workbook("Book1.xlsx");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.OnePagePerSheet = true;
workbook.Save("output.pdf", saveOptions);
// LowCode API (concise, optimized)
LowCodeLoadOptions loadOpts = new LowCodeLoadOptions();
loadOpts.InputFile = "Book1.xlsx";
LowCodePdfSaveOptions saveOpts = new LowCodePdfSaveOptions();
PdfSaveOptions pdfOpts = new PdfSaveOptions();
pdfOpts.OnePagePerSheet = true;
saveOpts.PdfOptions = pdfOpts;
saveOpts.OutputFile = "output.pdf";
PdfConverter.Process(loadOpts, saveOpts);
LowCode Advantages:
- Optimized memory usage
- Faster execution
- Better resource management
- Cleaner separation of concerns
Advanced Pagination Control
Fine-tune how content flows across PDF pages:
PdfSaveOptions pdfOpts = new PdfSaveOptions();
// Single page per sheet with scaling
pdfOpts.OnePagePerSheet = true;
// Control all pages in workbook
pdfOpts.AllColumnsInOnePagePerSheet = true; // Fit all columns on one page
// Set page orientation
pdfOpts.PageCount = 1; // Limit number of pages
// Custom scaling
pdfOpts.PrintingPageType = PrintingPageType.IgnorePrintArea;
Selective Sheet Export
Export only specific worksheets to reduce PDF size:
PdfSaveOptions pdfOpts = new PdfSaveOptions();
// Export only first worksheet
pdfOpts.PageIndex = 0;
pdfOpts.PageCount = 1;
// Or use SheetSet for multiple specific sheets
// (Note: SheetSet is available in traditional API)
Traditional API: Maximum Control
For scenarios requiring comprehensive customization:
using Aspose.Cells;
// Load an Excel file
Workbook workbook = new Workbook("Book1.xlsx");
// Configure detailed PDF options
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.OnePagePerSheet = true;
saveOptions.Compliance = PdfCompliance.PdfA1b;
saveOptions.EmbedStandardWindowsFonts = true;
saveOptions.OptimizationType = PdfOptimizationType.MinimumSize;
saveOptions.CalculateFormula = true;
// Add metadata
saveOptions.CreatedTime = DateTime.Now;
saveOptions.Producer = "My Application v1.0";
// Save it as PDF
workbook.Save("output.pdf", saveOptions);
Web API Integration Example
Use PDF conversion in ASP.NET Core controllers:
[HttpPost("convert-to-pdf")]
public IActionResult ConvertToPdf(IFormFile file)
{
try
{
// Save uploaded file temporarily
string tempPath = Path.GetTempFileName();
using (var stream = new FileStream(tempPath, FileMode.Create))
{
file.CopyTo(stream);
}
// Configure conversion
LowCodeLoadOptions loadOpts = new LowCodeLoadOptions();
loadOpts.InputFile = tempPath;
LowCodePdfSaveOptions saveOpts = new LowCodePdfSaveOptions();
PdfSaveOptions pdfOpts = new PdfSaveOptions();
pdfOpts.OnePagePerSheet = true; // Optimize for web viewing
saveOpts.PdfOptions = pdfOpts;
MemoryStream ms = new MemoryStream();
saveOpts.OutputStream = ms;
PdfConverter.Process(loadOpts, saveOpts);
// Clean up
File.Delete(tempPath);
// Return PDF
return File(ms.ToArray(), "application/pdf", "converted.pdf");
}
catch (Exception ex)
{
return StatusCode(500, $"Conversion error: {ex.Message}");
}
}
Batch Processing with Mixed Options
Process multiple files with different pagination settings:
var conversions = new[]
{
new { File = "dashboard.xlsx", OnePagePerSheet = true },
new { File = "detailed-report.xlsx", OnePagePerSheet = false },
new { File = "summary.xlsx", OnePagePerSheet = true }
};
foreach (var conv in conversions)
{
LowCodeLoadOptions loadOpts = new LowCodeLoadOptions();
loadOpts.InputFile = conv.File;
LowCodePdfSaveOptions saveOpts = new LowCodePdfSaveOptions();
PdfSaveOptions pdfOpts = new PdfSaveOptions();
pdfOpts.OnePagePerSheet = conv.OnePagePerSheet;
saveOpts.PdfOptions = pdfOpts;
saveOpts.OutputFile = Path.ChangeExtension(conv.File, ".pdf");
PdfConverter.Process(loadOpts, saveOpts);
Console.WriteLine($"✓ Converted: {conv.File}");
}
Tips and Best Practices
Layout Optimization
- Define Print Areas: Set print areas and page setup in source Excel files before conversion for consistent results.
- Use OnePagePerSheet: Enable for dashboards, summaries, and presentation materials to ensure compact output.
- Test Page Breaks: Preview Excel print layout to understand how content will flow in PDF.
Performance Optimization
- Use LowCode API: Leverage
PdfConverter.Process()
for optimized memory usage and faster execution. - Pool Settings: For high-volume conversions, reuse
PdfSaveOptions
objects to reduce overhead. - Profile Memory: Monitor memory usage for very large workbooks; consider splitting exports if needed.
Output Quality
- Font Embedding: Use
PdfSaveOptions.EmbedStandardWindowsFonts
to ensure consistent rendering across platforms. - Compliance Standards: Set
Compliance
property for archival (PDF/A) or print (PDF/X) requirements. - Compression: Apply
OptimizationType
to balance file size and quality.
Resource Management
- Dispose Properly: Dispose of workbook and stream objects promptly to release resources.
- File vs Stream: Use file-based output for batch jobs, streams for web APIs.
- Temporary Files: Clean up temporary files after processing in web applications.
Production Deployment
- Error Handling: Wrap conversions in try-catch blocks for robust error management.
- Logging: Log conversion metrics (file size, page count, duration) for monitoring.
- Track Usage: Monitor metered licensing token usage for cost management.
- Output Validation: Verify PDF file size and page count after conversion.
Common Issues and Resolutions
Issue | Resolution |
---|---|
File not found | Verify file paths and ensure proper escaping of backslashes in Windows paths |
Unsupported format | Ensure the input file is one of the supported types (XLS, XLSX, XLSM, etc.) |
Large file performance | Enable streaming, use OnePagePerSheet = false for very large sheets |
Content cut off with OnePagePerSheet | Content is scaled to fit; check original Excel page setup and print preview |
Blurry text in PDF | Increase DesiredPPI in PdfSaveOptions or disable OnePagePerSheet |
Missing fonts | Embed fonts using EmbedStandardWindowsFonts = true or install fonts on server |
Output file locked | Ensure all streams are properly disposed after conversion |
Memory overflow | Process large files in chunks or disable OnePagePerSheet for better pagination |
Frequently Asked Questions
What is Aspose.Cells PDF Converter for .NET? A specialized tool for converting Excel and OpenOffice spreadsheets to PDF with high fidelity and precise pagination control.
How does it differ from Aspose.Cells for .NET? Aspose.Cells for .NET supports a broad range of spreadsheet manipulation tasks, while the PDF Converter focuses specifically on accurate PDF export with streamlined APIs.
Can I customize the PDF output?
Yes, using PdfSaveOptions
you can control compliance, compression, security, font embedding, and pagination behavior.
Which formats are supported for conversion? XLS, XLSX, XLSM, XLTX, XLTM, XLSB, CSV, TSV, ODS, HTML, XML, JSON, and more.
What does OnePagePerSheet do? It forces each worksheet to fit on exactly one PDF page by applying automatic scaling, ideal for dashboards and summary reports.
When should I use OnePagePerSheet? Use it for dashboards, executive summaries, or any content that should remain on a single page. Avoid it for detailed reports with large data tables.
Can I output to memory instead of files?
Yes! Use OutputStream
property with a MemoryStream
for complete in-memory processing.
How do I choose between file-based and stream-based output?
Use OutputFile
for batch processing and disk-based workflows. Use OutputStream
for web APIs, cloud storage, or in-memory operations.
Does the LowCode API support all features?
The LowCode API exposes the most commonly used features. For advanced scenarios (digital signatures, custom security), use the traditional Workbook
API.
API Reference Summary
Key Classes
PdfConverter
: Static class providing simplified conversion methodsLowCodeLoadOptions
: Configuration for loading Excel filesLowCodePdfSaveOptions
: Configuration for PDF outputPdfSaveOptions
: Detailed PDF conversion settings
Essential Properties
InputFile
: Source Excel file pathOutputFile
: Target PDF file path (file-based output)OutputStream
: Target stream for PDF output (stream-based output)PdfOptions
: PDF-specific rendering optionsOnePagePerSheet
: Force each worksheet onto a single PDF pageCompliance
: PDF compliance standard (PDF/A, PDF/X)OptimizationType
: Balance between file size and quality
Common Enumerations
PdfCompliance
: None, Pdf15, PdfA1b, PdfA2u, PdfX1aPdfOptimizationType
: Standard, MinimumSizePrintingPageType
: Default, IgnorePrintArea, IgnoreStyle