Regira-Packages

Regira Office.PDF — Examples

IHtmlToPdfService pdf = new Regira.Office.PDF.SelectPdf.PdfManager();

IMemoryFile result = await pdf.Create(new HtmlInput
{
    HtmlContent         = reportHtml,
    HeaderHtmlContent   = "<div style='text-align:right;font-size:10px'>Confidential</div>",
    HeaderHeight        = 15,
    FooterHtmlContent   = "<div style='text-align:center;font-size:10px'>Page {"layout"=>"page", "title"=>"Regira Office.PDF  Examples", "content"=>"# Regira Office.PDF  Examples\n\n## Example 1: HTML  PDF with header and footer\n\n```csharp\nIHtmlToPdfService pdf = new Regira.Office.PDF.SelectPdf.PdfManager();\n\nIMemoryFile result = await pdf.Create(new HtmlInput\n{\n    HtmlContent         = reportHtml,\n    HeaderHtmlContent   = \"<div style='text-align:right;font-size:10px'>Confidential</div>\",\n    HeaderHeight        = 15,\n    FooterHtmlContent   = \"<div style='text-align:center;font-size:10px'>Page {{page}} of {{pages}}</div>\",\n    FooterHeight        = 15,\n    Format              = PageSize.A4,\n    Orientation         = PageOrientation.Portrait,\n    Margins             = [20, 20, 20, 20]   // top, right, bottom, left (points)\n});\n\nawait fileService.Save(\"reports/output.pdf\", result.GetBytes()!);\n```\n\n---\n\n## Example 2: Render invoice template then convert to PDF\n\nCombine `IHtmlParser` (Razor) with `IHtmlToPdfService` for a full template-to-PDF pipeline.\n\n```csharp\npublic class InvoicePdfService(IHtmlParser html, IHtmlToPdfService pdf)\n{\n    public async Task<IMemoryFile> Generate(InvoiceDto invoice)\n    {\n        string template = await File.ReadAllTextAsync(\"Templates/Invoice.cshtml\");\n        string rendered = await html.Parse(template, invoice);\n\n        return await pdf.Create(new HtmlInput\n        {\n            HtmlContent = rendered,\n            Format      = PageSize.A4\n        });\n    }\n}\n```\n\n---\n\n## Example 3: Merge multiple PDFs\n\n```csharp\nIPdfMerger merger = new Regira.Office.PDF.DocNET.PdfManager(imageService);\n\nvar pages = new List<IMemoryFile>\n{\n    coverBytes.ToBinaryFile(),\n    chapterOneBytes.ToBinaryFile(),\n    appendixBytes.ToBinaryFile()\n};\n\nIMemoryFile merged = (await merger.Merge(pages))!;\n```\n\n---\n\n## Example 4: Split a PDF into individual pages\n\n```csharp\nIPdfSplitter splitter = new Regira.Office.PDF.DocNET.PdfManager(imageService);\n\nint total  = await splitter.GetPageCount(pdf);\nvar ranges = Enumerable.Range(1, total)\n                       .Select(i => new PdfSplitRange { Start = i, End = i });\n\nIEnumerable<IMemoryFile> pages = await splitter.Split(pdf, ranges);\n```\n\n---\n\n## Example 5: Convert PDF pages to images\n\n```csharp\nIPdfToImageService converter = new Regira.Office.PDF.Spire.PdfManager();\n\nvar images = await converter.ToImages(pdf, new PdfToImagesOptions\n{\n    Size   = new ImageSize(1200, 1697),   // approx A4 at 144 dpi\n    Format = ImageFormat.Jpeg\n});\n\nint page = 1;\nforeach (var img in images)\n    await fileService.Save($\"pages/page-{page++}.jpg\", img.GetBytes()!);\n```\n\n---\n\n## Example 6: Extract text for search indexing\n\n```csharp\nIPdfTextService extractor = new Regira.Office.PDF.DocNET.PdfManager(imageService);\n\nstring fullText        = await extractor.GetText(pdf);\nIList<string> byPage   = await extractor.GetTextPerPage(pdf);\n\n// Remove pages that contain no text (e.g. blank separator pages)\nIMemoryFile cleaned = (await extractor.RemoveEmptyPages(pdf))!;\n```\n\n---\n\n## Example 7: Print a PDF\n\n```csharp\nIPdfPrinter printer = new Regira.Office.PDF.Spire.PdfPrinter();\n\nConsole.WriteLine(\"Available printers:\");\nforeach (var p in await printer.List())\n    Console.WriteLine($\"  {p}\");\n\nawait printer.Print(new PdfPrinterInput\n{\n    PdfFile         = pdfFile,\n    PrinterName     = \"HP LaserJet Pro\",\n    PageSize        = PageSize.A4,\n    PageOrientation = PageOrientation.Portrait\n});\n```\n\n---\n\n## Overview\n\n1. [Index](/Regira-Packages/src/Common.Office/docs/pdf/) — Overview, interfaces, models, and implementation notes\n1. **[Examples](/Regira-Packages/src/Common.Office/docs/pdf/examples.html)** — HTML→PDF, merge, split, text extraction, printing\n", "dir"=>"/src/Common.Office/docs/pdf/", "name"=>"examples.md", "path"=>"src/Common.Office/docs/pdf/examples.md", "url"=>"/src/Common.Office/docs/pdf/examples.html"} of </div>",
    FooterHeight        = 15,
    Format              = PageSize.A4,
    Orientation         = PageOrientation.Portrait,
    Margins             = [20, 20, 20, 20]   // top, right, bottom, left (points)
});

await fileService.Save("reports/output.pdf", result.GetBytes()!);

Example 2: Render invoice template then convert to PDF

Combine IHtmlParser (Razor) with IHtmlToPdfService for a full template-to-PDF pipeline.

public class InvoicePdfService(IHtmlParser html, IHtmlToPdfService pdf)
{
    public async Task<IMemoryFile> Generate(InvoiceDto invoice)
    {
        string template = await File.ReadAllTextAsync("Templates/Invoice.cshtml");
        string rendered = await html.Parse(template, invoice);

        return await pdf.Create(new HtmlInput
        {
            HtmlContent = rendered,
            Format      = PageSize.A4
        });
    }
}

Example 3: Merge multiple PDFs

IPdfMerger merger = new Regira.Office.PDF.DocNET.PdfManager(imageService);

var pages = new List<IMemoryFile>
{
    coverBytes.ToBinaryFile(),
    chapterOneBytes.ToBinaryFile(),
    appendixBytes.ToBinaryFile()
};

IMemoryFile merged = (await merger.Merge(pages))!;

Example 4: Split a PDF into individual pages

IPdfSplitter splitter = new Regira.Office.PDF.DocNET.PdfManager(imageService);

int total  = await splitter.GetPageCount(pdf);
var ranges = Enumerable.Range(1, total)
                       .Select(i => new PdfSplitRange { Start = i, End = i });

IEnumerable<IMemoryFile> pages = await splitter.Split(pdf, ranges);

Example 5: Convert PDF pages to images

IPdfToImageService converter = new Regira.Office.PDF.Spire.PdfManager();

var images = await converter.ToImages(pdf, new PdfToImagesOptions
{
    Size   = new ImageSize(1200, 1697),   // approx A4 at 144 dpi
    Format = ImageFormat.Jpeg
});

int page = 1;
foreach (var img in images)
    await fileService.Save($"pages/page-{page++}.jpg", img.GetBytes()!);

Example 6: Extract text for search indexing

IPdfTextService extractor = new Regira.Office.PDF.DocNET.PdfManager(imageService);

string fullText        = await extractor.GetText(pdf);
IList<string> byPage   = await extractor.GetTextPerPage(pdf);

// Remove pages that contain no text (e.g. blank separator pages)
IMemoryFile cleaned = (await extractor.RemoveEmptyPages(pdf))!;

Example 7: Print a PDF

IPdfPrinter printer = new Regira.Office.PDF.Spire.PdfPrinter();

Console.WriteLine("Available printers:");
foreach (var p in await printer.List())
    Console.WriteLine($"  {p}");

await printer.Print(new PdfPrinterInput
{
    PdfFile         = pdfFile,
    PrinterName     = "HP LaserJet Pro",
    PageSize        = PageSize.A4,
    PageOrientation = PageOrientation.Portrait
});

Overview

  1. Index — Overview, interfaces, models, and implementation notes
  2. Examples — HTML→PDF, merge, split, text extraction, printing