Sample code · C#
using System; using System.Collections.Generic; using System.IO; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using SelectPdf.Universal; namespace SelectPdf.Samples.Pages { public class PdfOfficeElementsModel : PageModel { public void OnGet() { DdlDocumentType = "Excel"; DdlSize = "Automatic"; TxtWidth = "400"; TxtHeight = "300"; DdlLayout = "FitAllColumnsOnOnePage"; DdlGridLines = "Automatic"; ChkSheetBookmarks = true; DdlPublish = "Slides"; DdlSlidesPerHandout = "6"; } [BindProperty] public string DdlDocumentType { get; set; } public List<SelectListItem> DocumentTypes { get; } = new List<SelectListItem> { new SelectListItem { Value = "Excel", Text = "Excel workbook (PdfExcelElement)" }, new SelectListItem { Value = "PowerPoint", Text = "PowerPoint presentation (PdfPowerPointElement)" }, }; // An uploaded workbook or presentation, or nothing - the shipped files/workbook.xlsx or // files/presentation.pptx is drawn then. [BindProperty] public IFormFile FileOffice { get; set; } [BindProperty] public string DdlSize { get; set; } public List<SelectListItem> Sizes { get; } = new List<SelectListItem> { new SelectListItem { Value = "Automatic", Text = "Automatic - full width, flowing onto the next pages" }, new SelectListItem { Value = "Fixed", Text = "Fixed rectangle - the width and height below" }, }; [BindProperty] public string TxtWidth { get; set; } [BindProperty] public string TxtHeight { get; set; } [BindProperty] public string TxtPageRanges { get; set; } // Excel element options [BindProperty] public string TxtWorksheets { get; set; } [BindProperty] public string DdlLayout { get; set; } public List<SelectListItem> Layouts { get; } = new List<SelectListItem> { new SelectListItem { Value = "Automatic", Text = "The sheet's own print scaling" }, new SelectListItem { Value = "NoScaling", Text = "Actual size" }, new SelectListItem { Value = "FitSheetOnOnePage", Text = "Fit each sheet on one page" }, new SelectListItem { Value = "FitAllColumnsOnOnePage", Text = "Fit all columns on one page" }, new SelectListItem { Value = "FitAllRowsOnOnePage", Text = "Fit all rows on one page" }, }; [BindProperty] public string DdlGridLines { get; set; } public List<SelectListItem> GridLines { get; } = new List<SelectListItem> { new SelectListItem { Value = "Automatic", Text = "As each sheet's print setting says" }, new SelectListItem { Value = "Visible", Text = "Draw the gridlines" }, new SelectListItem { Value = "Hidden", Text = "No gridlines" }, }; [BindProperty] public bool ChkSheetBookmarks { get; set; } // PowerPoint element options [BindProperty] public string DdlPublish { get; set; } public List<SelectListItem> PublishOptions { get; } = new List<SelectListItem> { new SelectListItem { Value = "Slides", Text = "Slides" }, new SelectListItem { Value = "NotesPages", Text = "Notes pages" }, new SelectListItem { Value = "Handouts", Text = "Handouts" }, }; [BindProperty] public string DdlSlidesPerHandout { get; set; } public List<SelectListItem> SlidesPerHandout { get; } = new List<SelectListItem> { new SelectListItem { Value = "1", Text = "1" }, new SelectListItem { Value = "2", Text = "2" }, new SelectListItem { Value = "3", Text = "3" }, new SelectListItem { Value = "4", Text = "4" }, new SelectListItem { Value = "6", Text = "6" }, new SelectListItem { Value = "9", Text = "9" }, }; [BindProperty] public bool ChkShowHiddenSlides { get; set; } public IActionResult OnPost() { // read parameters from the webpage bool excel = DdlDocumentType != "PowerPoint"; bool fixedSize = DdlSize == "Fixed"; float width; if (!float.TryParse(TxtWidth, out width) || width <= 0) { width = 400; } float height; if (!float.TryParse(TxtHeight, out height) || height <= 0) { height = 300; } // create a new pdf document PdfDocument doc = new PdfDocument(); // add a new page to the document PdfPage page = doc.AddPage(); // a heading drawn by a text element PdfFont font = PdfFont.CreateStandardFont(PdfStandardFont.HelveticaBold, 16); PdfTextElement heading = new PdfTextElement(0, 0, excel ? "Quarterly figures - Excel workbook drawn into the page" : "Presentation - PowerPoint slides drawn into the page", font, new PdfColor(44, 62, 80)); PdfRenderingResult result = page.Add(heading); // the office document starts 10 points below the heading float y = result.PdfPageLastRectangle.Bottom + 10; // the uploaded document, or the sample one shipped with the application (the stream // is read when the element is added to the page) Stream input; if (FileOffice != null && FileOffice.Length > 0) { input = new MemoryStream(); FileOffice.CopyTo(input); input.Position = 0; } else { string file = Path.Combine(Startup.ServerPath, "wwwroot", "files", excel ? "workbook.xlsx" : "presentation.pptx"); input = new MemoryStream(System.IO.File.ReadAllBytes(file)); } using (input) { if (excel) { // an excel workbook as a page element: with no width and height it takes the // width of the page and flows onto the pages that follow; a fixed rectangle // shows what fits in it PdfExcelElement workbook = fixedSize ? new PdfExcelElement(0, y, width, height, input) : new PdfExcelElement(0, y, input); // the format is taken from the uploaded file's name, since a stream carries none if (FileOffice != null && FileOffice.Length > 0) { workbook.Format = ExcelFormatFromFileName(FileOffice.FileName); } // the sheets drawn (by name, comma separated - empty draws every visible sheet), // how each sheet is scaled onto the pages, the gridlines and one outline entry // per sheet workbook.Worksheets = TxtWorksheets ?? string.Empty; workbook.Layout = SampleForm.ParseEnum(DdlLayout, ExcelLayout.Automatic); workbook.GridLines = SampleForm.ParseEnum(DdlGridLines, ExcelGridLines.Automatic); workbook.SheetBookmarks = ChkSheetBookmarks; workbook.PageRanges = TxtPageRanges ?? string.Empty; page.Add(workbook); } else { // a powerpoint presentation as a page element: every slide keeps its shape, // scaled to the element's width, and the slides are stacked one under the // other, continuing on the pages that follow PdfPowerPointElement presentation = fixedSize ? new PdfPowerPointElement(0, y, width, height, input) : new PdfPowerPointElement(0, y, input); // what is drawn (slides, notes pages or handouts), the slides on a handout page // and whether the hidden slides are drawn too presentation.Publish = SampleForm.ParseEnum(DdlPublish, PowerPointPublish.Slides); int slidesPerHandout; if (!int.TryParse(DdlSlidesPerHandout, out slidesPerHandout)) { slidesPerHandout = 6; } presentation.SlidesPerHandout = slidesPerHandout; presentation.ShowHiddenSlides = ChkShowHiddenSlides; presentation.PageRanges = TxtPageRanges ?? string.Empty; page.Add(presentation); } // save pdf document byte[] pdf = doc.Save(); // close pdf document doc.Close(); // return resulted pdf document FileResult fileResult = new FileContentResult(pdf, "application/pdf"); fileResult.FileDownloadName = "Document.pdf"; return fileResult; } } private static ExcelFormat ExcelFormatFromFileName(string fileName) { switch ((Path.GetExtension(fileName) ?? string.Empty).ToLowerInvariant()) { case ".xlsx": return ExcelFormat.Xlsx; case ".xlsm": return ExcelFormat.Xlsm; case ".xltx": return ExcelFormat.Xltx; case ".xltm": return ExcelFormat.Xltm; case ".xls": return ExcelFormat.Xls; case ".xlt": return ExcelFormat.Xlt; case ".csv": return ExcelFormat.Csv; case ".tsv": return ExcelFormat.Tsv; default: return ExcelFormat.Automatic; } } } }