Mercurial > hgrepos > Python2 > PyMuPDF
diff mupdf-source/docs/cookbook/javascript/basics.md @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mupdf-source/docs/cookbook/javascript/basics.md Mon Sep 15 11:43:07 2025 +0200 @@ -0,0 +1,66 @@ +# Basics + +## Loading a PDF + +The following example demonstrates how to load a document and then print out the page count. +Ensure you have a PDF file named "test.pdf" alongside this example before running it. + + import * as mupdf from "mupdf" + + var doc = mupdf.Document.openDocument("test.pdf") + console.log(doc.countPages()) + +## Creating a PDF + +How to create a new PDF file with a single blank page: + + import * as mupdf from "mupdf" + + var doc = new mupdf.PDFDocument() + doc.insertPage(-1, doc.addPage([0, 0, 595, 842], 0, null, "")) + doc.save("blank.pdf") + +## Adding an annotation + +How to add a simple annotation to a PDF file: + + import * as mupdf from "mupdf" + + var doc = mupdf.Document.openDocument("blank.pdf") + + var page = doc.loadPage(0) + + var annot = page.createAnnotation("FreeText") + annot.setRect([10, 10, 200, 100]) + annot.setContents("Hello, world!") + + page.update() + + doc.save("annotation.pdf") + +## Converting a PDF to plain text + + import * as mupdf from "mupdf" + + var doc = mupdf.Document.openDocument("test.pdf") + for (var i = 0; i < doc.countpages(); ++i) { + var page = doc.loadPage(i) + var text = page.toStructuredText().asText() + console.log(text) + } + +## Converting a PDF to image files + + import * as fs from "fs" + import * as mupdf from "mupdf" + + var doc = mupdf.Document.openDocument("test.pdf") + for (var i = 0; i < doc.countpages(); ++i) { + var page = doc.loadPage(i) + var pixmap = page.toPixmap( + mupdf.Matrix.scale(96 / 72, 96 / 72), + mupdf.ColorSpace.DeviceRGB + ) + var buffer = pixmap.asPNG() + fs.writeFileSync("page" + (i+1) + ".png", buffer) + }
