1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using PdfOxide.Exceptions;
using PdfOxide.Internal;
namespace PdfOxide.Core
{
/// <summary>
/// Represents a PDF document that can be created, edited, and saved.
/// Universal API combining creation, reading, and editing capabilities.
/// </summary>
/// <remarks>
/// <para>
/// Pdf is the universal PDF API that provides:
/// <list type="bullet">
/// <item><description>Creating PDFs from Markdown, HTML, or plain text</description></item>
/// <item><description>Saving to file or memory buffer</description></item>
/// <item><description>Editing page content and metadata</description></item>
/// <item><description>Extracting content and converting formats</description></item>
/// </list>
/// </para>
/// <para>
/// The document must be explicitly disposed to release native resources.
/// Use 'using' statements for automatic cleanup.
/// </para>
/// </remarks>
/// <example>
/// <code>
/// // Create PDF from Markdown
/// using (var pdf = Pdf.FromMarkdown("# Hello\n\n**Bold** text"))
/// {
/// pdf.Save("output.pdf");
/// }
///
/// // Create from HTML
/// using (var pdf = Pdf.FromHtml("<h1>Title</h1><p>Content</p>"))
/// {
/// byte[] bytes = pdf.SaveToBytes();
/// File.WriteAllBytes("output.pdf", bytes);
/// }
/// </code>
/// </example>
public sealed class Pdf : IDisposable
{
private NativeHandle _handle;
private bool _disposed;
private Pdf(NativeHandle handle)
{
_handle = handle ?? throw new ArgumentNullException(nameof(handle));
}
/// <summary>
/// Creates a PDF from Markdown content.
/// </summary>
/// <param name="markdown">The Markdown content.</param>
/// <returns>A new Pdf document.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="markdown"/> is null.</exception>
/// <exception cref="PdfException">Thrown if PDF creation fails.</exception>
/// <example>
/// <code>
/// using (var pdf = Pdf.FromMarkdown("# Title\n\nParagraph text"))
/// {
/// pdf.Save("document.pdf");
/// }
/// </code>
/// </example>
public static Pdf FromMarkdown(string markdown)
{
if (markdown == null)
throw new ArgumentNullException(nameof(markdown));
var handle = NativeMethods.PdfFromMarkdown(markdown, out var errorCode);
if (handle.IsInvalid)
{
ExceptionMapper.ThrowIfError(errorCode);
}
return new Pdf(handle);
}
/// <summary>
/// Creates a PDF from HTML content.
/// </summary>
/// <param name="html">The HTML content.</param>
/// <returns>A new Pdf document.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="html"/> is null.</exception>
/// <exception cref="PdfException">Thrown if PDF creation fails.</exception>
/// <example>
/// <code>
/// using (var pdf = Pdf.FromHtml("<h1>Title</h1><p>Content</p>"))
/// {
/// pdf.Save("document.pdf");
/// }
/// </code>
/// </example>
public static Pdf FromHtml(string html)
{
if (html == null)
throw new ArgumentNullException(nameof(html));
var handle = NativeMethods.PdfFromHtml(html, out var errorCode);
if (handle.IsInvalid)
{
ExceptionMapper.ThrowIfError(errorCode);
}
return new Pdf(handle);
}
/// <summary>
/// Creates a PDF from plain text content.
/// </summary>
/// <param name="text">The text content.</param>
/// <returns>A new Pdf document.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is null.</exception>
/// <exception cref="PdfException">Thrown if PDF creation fails.</exception>
/// <example>
/// <code>
/// using (var pdf = Pdf.FromText("This is plain text"))
/// {
/// pdf.Save("document.pdf");
/// }
/// </code>
/// </example>
public static Pdf FromText(string text)
{
if (text == null)
throw new ArgumentNullException(nameof(text));
var handle = NativeMethods.PdfFromText(text, out var errorCode);
if (handle.IsInvalid)
{
ExceptionMapper.ThrowIfError(errorCode);
}
return new Pdf(handle);
}
/// <summary>
/// Gets the number of pages in the PDF.
/// </summary>
/// <value>The page count.</value>
/// <exception cref="ObjectDisposedException">Thrown if the document has been disposed.</exception>
/// <exception cref="PdfException">Thrown if page count cannot be determined.</exception>
public int PageCount
{
get
{
ThrowIfDisposed();
var count = NativeMethods.PdfGetPageCount(_handle.DangerousGetHandle(), out var errorCode);
ExceptionMapper.ThrowIfError(errorCode);
return count;
}
}
/// <summary>
/// Saves the PDF to a file.
/// </summary>
/// <param name="path">The output file path.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="path"/> is null.</exception>
/// <exception cref="ObjectDisposedException">Thrown if the document has been disposed.</exception>
/// <exception cref="PdfIoException">Thrown if the file cannot be written.</exception>
/// <example>
/// <code>
/// using (var pdf = Pdf.FromMarkdown("# Hello"))
/// {
/// pdf.Save("output.pdf");
/// }
/// </code>
/// </example>
public void Save(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
ThrowIfDisposed();
var result = NativeMethods.PdfSave(_handle, path, out var errorCode);
if (result != 0)
{
ExceptionMapper.ThrowIfError(errorCode);
}
}
/// <summary>
/// Saves the PDF to a byte array.
/// </summary>
/// <returns>The PDF content as bytes.</returns>
/// <exception cref="ObjectDisposedException">Thrown if the document has been disposed.</exception>
/// <exception cref="PdfException">Thrown if the PDF cannot be generated.</exception>
/// <example>
/// <code>
/// using (var pdf = Pdf.FromMarkdown("# Hello"))
/// {
/// byte[] pdfBytes = pdf.SaveToBytes();
/// File.WriteAllBytes("output.pdf", pdfBytes);
/// }
/// </code>
/// </example>
public byte[] SaveToBytes()
{
ThrowIfDisposed();
var outputPtr = NativeMethods.PdfSaveToBytes(_handle, out var outputLen, out var errorCode);
if (errorCode != 0)
{
ExceptionMapper.ThrowIfError(errorCode);
}
if (outputPtr == IntPtr.Zero || outputLen <= 0)
{
return System.Array.Empty<byte>();
}
try
{
var bytes = new byte[outputLen];
System.Runtime.InteropServices.Marshal.Copy(outputPtr, bytes, 0, outputLen);
return bytes;
}
finally
{
NativeMethods.FreeBytes(outputPtr, outputLen);
}
}
/// <summary>
/// Saves the PDF to a stream.
/// </summary>
/// <param name="stream">The output stream.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="stream"/> is null.</exception>
/// <exception cref="ObjectDisposedException">Thrown if the document has been disposed.</exception>
/// <exception cref="PdfException">Thrown if the PDF cannot be generated.</exception>
/// <example>
/// <code>
/// using (var pdf = Pdf.FromMarkdown("# Hello"))
/// using (var file = File.Create("output.pdf"))
/// {
/// pdf.SaveToStream(file);
/// }
/// </code>
/// </example>
public void SaveToStream(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
byte[] bytes = SaveToBytes();
stream.Write(bytes, 0, bytes.Length);
}
/// <summary>
/// Asynchronously saves the PDF to a file.
/// </summary>
/// <param name="path">The output file path.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that completes when the file is saved.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="path"/> is null.</exception>
/// <exception cref="OperationCanceledException">Thrown if the operation is cancelled.</exception>
public Task SaveAsync(string path, CancellationToken cancellationToken = default)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
return Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
Save(path);
}, cancellationToken);
}
/// <summary>
/// Asynchronously saves the PDF to a stream.
/// </summary>
/// <param name="stream">The output stream.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that completes when the PDF is saved.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="stream"/> is null.</exception>
/// <exception cref="OperationCanceledException">Thrown if the operation is cancelled.</exception>
public Task SaveToStreamAsync(Stream stream, CancellationToken cancellationToken = default)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
return Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
SaveToStream(stream);
}, cancellationToken);
}
/// <summary>
/// Disposes the PDF and releases native resources.
/// </summary>
public void Dispose()
{
if (!_disposed)
{
_handle?.Dispose();
_disposed = true;
}
}
private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(nameof(Pdf));
}
}
}