Skip to content

Commit f270308

Browse files
committed
docs: update API docs to reflect sync operations
Most page and form operations are now synchronous: - getPage(), getPages(), getForm() are sync - Form field methods (setValue, check, fill, flatten) are sync - Text extraction (extractText, findText) is sync - Annotation methods are sync - embedFont(), embedImage() are sync - PDF.create() is sync Only load() and save() remain async.
1 parent e7b1076 commit f270308

File tree

18 files changed

+237
-248
lines changed

18 files changed

+237
-248
lines changed

content/docs/advanced/library-authors.mdx

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -156,62 +156,50 @@ try {
156156

157157
## Async Patterns
158158

159-
### All I/O is Async
159+
### Loading and Saving are Async
160160

161-
LibPDF uses async/await for all I/O operations:
161+
LibPDF uses async/await for loading and saving:
162162

163163
```typescript
164164
// Loading is async
165165
const pdf = await PDF.load(bytes);
166166

167-
// Page access is async
168-
const page = await pdf.getPage(0);
167+
// Page access is sync
168+
const page = pdf.getPage(0);
169169

170-
// Text extraction is async
171-
const text = await page.extractText();
170+
// Text extraction is sync
171+
const text = page.extractText();
172172

173173
// Saving is async
174174
const savedBytes = await pdf.save();
175175
```
176176

177-
### Batch Operations
177+
### Getting Pages
178178

179-
For performance, batch related operations:
179+
Page access is synchronous:
180180

181181
```typescript
182-
// Inefficient: sequential page loading
183-
const pages: PDFPage[] = [];
184-
for (let i = 0; i < pdf.getPageCount(); i++) {
185-
pages.push(await pdf.getPage(i));
186-
}
182+
// Get a single page
183+
const page = pdf.getPage(0);
187184

188-
// Better: parallel loading
189-
const pagePromises = [];
190-
for (let i = 0; i < pdf.getPageCount(); i++) {
191-
pagePromises.push(pdf.getPage(i));
192-
}
193-
const pages = await Promise.all(pagePromises);
185+
// Get all pages
186+
const pages = pdf.getPages();
194187

195-
// Best: use getPages() which is optimized
196-
const pages = await pdf.getPages();
188+
// Get page count
189+
const count = pdf.getPageCount();
197190
```
198191

199-
### Streaming Considerations
192+
### Text Extraction
200193

201-
For large files, consider memory usage:
194+
Text extraction is synchronous:
202195

203196
```typescript
204-
// Process pages one at a time for large documents
205-
async function extractAllText(pdf: PDF): Promise<string[]> {
197+
function extractAllText(pdf: PDF): string[] {
206198
const results: string[] = [];
207199

208-
for (let i = 0; i < pdf.getPageCount(); i++) {
209-
const page = await pdf.getPage(i);
210-
if (page) {
211-
const text = await page.extractText();
212-
results.push(text.text);
213-
}
214-
// Page can be garbage collected after processing
200+
for (const page of pdf.getPages()) {
201+
const text = page.extractText();
202+
results.push(text.text);
215203
}
216204

217205
return results;
@@ -227,15 +215,15 @@ For advanced features, access internal APIs.
227215
Access the raw object storage:
228216

229217
```typescript
230-
import { PDF, PdfRef, PdfDict } from "@libpdf/core";
218+
import { PDF, PdfRef, PdfDict, PdfName } from "@libpdf/core";
231219

232220
const pdf = await PDF.load(bytes);
233221

234222
// Get the registry
235223
const registry = pdf.context.registry;
236224

237225
// Resolve any reference
238-
const obj = await registry.resolve(PdfRef.of(5, 0));
226+
const obj = registry.resolve(PdfRef.of(5, 0));
239227

240228
// Register new objects
241229
const newDict = PdfDict.of({ Type: PdfName.of("Custom") });
@@ -418,7 +406,7 @@ Here's a complete example of building on LibPDF:
418406

419407
```typescript
420408
// pdf-redactor/index.ts
421-
import { PDF, PDFPage, rgb, type Color } from "@libpdf/core";
409+
import { PDF, rgb, type Color } from "@libpdf/core";
422410

423411
export interface RedactionOptions {
424412
color?: Color;
@@ -447,11 +435,13 @@ export async function redactText(
447435

448436
// Process each page with redactions
449437
for (const [pageIndex, texts] of byPage) {
450-
const page = await pdf.getPage(pageIndex);
451-
if (!page) continue;
438+
const page = pdf.getPage(pageIndex);
439+
if (!page) {
440+
continue;
441+
}
452442

453443
for (const text of texts) {
454-
const matches = await page.findText(text);
444+
const matches = page.findText(text);
455445
for (const match of matches) {
456446
// Draw a filled rectangle over each match
457447
page.drawRectangle({
@@ -476,7 +466,7 @@ export async function redactText(
476466
| Dependencies | Peer dependency for libraries, direct for apps |
477467
| Types | Preserve generics, don't upcast |
478468
| Errors | Re-export error types, add context |
479-
| Async | Batch operations, consider memory |
469+
| Async | Only load/save are async; page ops are sync |
480470
| Low-level | Use sparingly, document version requirements |
481471
| Testing | Test with real PDFs, cover error cases |
482472
| Bundles | Import only what you need |

content/docs/api/annotations.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ PDF annotations add interactive and visual elements to pages - highlights, stick
1515
```typescript
1616
import { PDF, rgb } from "@libpdf/core";
1717

18-
const pdf = await PDF.create();
18+
const pdf = PDF.create();
1919
const page = pdf.addPage();
2020

2121
// Add a yellow highlight
@@ -40,18 +40,18 @@ const bytes = await pdf.save();
4040

4141
```typescript
4242
const pdf = await PDF.load(existingBytes);
43-
const page = await pdf.getPage(0);
43+
const page = pdf.getPage(0);
4444

4545
// Get all annotations
46-
const annotations = await page.getAnnotations();
46+
const annotations = page.getAnnotations();
4747

4848
for (const annot of annotations) {
4949
console.log(annot.type, annot.contents);
5050
}
5151

5252
// Get specific types
53-
const highlights = await page.getHighlightAnnotations();
54-
const links = await page.getLinkAnnotations();
53+
const highlights = page.getHighlightAnnotations();
54+
const links = page.getLinkAnnotations();
5555
```
5656

5757
---
@@ -356,7 +356,7 @@ page.addInkAnnotation({
356356
## Modifying Annotations
357357

358358
```typescript
359-
const highlights = await page.getHighlightAnnotations();
359+
const highlights = page.getHighlightAnnotations();
360360
const highlight = highlights[0];
361361

362362
// Update properties
@@ -375,8 +375,8 @@ highlight.setPrintable(true);
375375
Remove a specific annotation.
376376

377377
```typescript
378-
const highlights = await page.getHighlightAnnotations();
379-
await page.removeAnnotation(highlights[0]);
378+
const highlights = page.getHighlightAnnotations();
379+
page.removeAnnotation(highlights[0]);
380380
```
381381

382382
---
@@ -391,10 +391,10 @@ Remove annotations, optionally filtered by type.
391391

392392
```typescript
393393
// Remove all highlights
394-
await page.removeAnnotations({ type: "Highlight" });
394+
page.removeAnnotations({ type: "Highlight" });
395395

396396
// Remove all annotations
397-
await page.removeAnnotations();
397+
page.removeAnnotations();
398398
```
399399

400400
---

0 commit comments

Comments
 (0)