@@ -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
165165const 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
174174const 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.
227215Access 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
232220const pdf = await PDF .load (bytes );
233221
234222// Get the registry
235223const 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
241229const 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
423411export 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 |
0 commit comments