diff --git a/CreatePdf.NET/CreatePdf.NET.csproj b/CreatePdf.NET/CreatePdf.NET.csproj index 8670f06..4c676ed 100644 --- a/CreatePdf.NET/CreatePdf.NET.csproj +++ b/CreatePdf.NET/CreatePdf.NET.csproj @@ -7,7 +7,7 @@ CreatePdf.NET CreatePdf.NET - Simple PDF Creation Library - 3.0.4 + 3.0.5 Alexander Nachtmann Lightweight PDF generation library for .NET 10/9/8 with text rendering, bitmap graphics, and OCR CreatePdf.NET is a simple, fast PDF creation library for .NET applications. Generate PDF documents with text rendering, bitmap graphics, and optional OCR text extraction. Lightweight, dependency-free, 100% test coverage. Perfect for creating reports, invoices, receipts, labels, and documents. Supports .NET 10, .NET 9, and .NET 8 with fluent C# API. diff --git a/CreatePdf.NET/Document.cs b/CreatePdf.NET/Document.cs index 020fcc1..34d66b1 100644 --- a/CreatePdf.NET/Document.cs +++ b/CreatePdf.NET/Document.cs @@ -202,21 +202,37 @@ public async Task SaveAndShowDirectoryAsync(string? filename = null) } /// - /// Saves the document to a PDF file. + /// Renders the document to an in-memory PDF and returns its bytes, without touching the disk. /// - /// Optional filename. If not provided, generates a timestamped name. - /// The full path to the saved PDF file. - public async Task SaveAsync(string? filename = null) + /// The complete PDF document as a byte array. + /// + /// + /// byte[] pdf = await Pdf.Create(Dye.White) + /// .AddText("Hello World") + /// .ToBytesAsync(); + /// + /// + public async Task ToBytesAsync() { - var path = FileOperations.GetOutputPath(filename); - - await using var stream = File.Create(path); + await using var stream = new MemoryStream(); await using var writer = new PdfWriter(stream, _background); foreach (var content in _contents) content.Render(writer); await writer.FinalizeAsync(); + return stream.ToArray(); + } + + /// + /// Saves the document to a PDF file. + /// + /// Optional filename. If not provided, generates a timestamped name. + /// The full path to the saved PDF file. + public async Task SaveAsync(string? filename = null) + { + var path = FileOperations.GetOutputPath(filename); + await File.WriteAllBytesAsync(path, await ToBytesAsync()); return path; }