Mercurial > hgrepos > Python2 > PyMuPDF
view mupdf-source/docs/cookbook/javascript/basics.md @ 37:1252325c592f
>>>>> tag v1.26.4+2 for changeset 14b91574d44a
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 22 Sep 2025 11:44:07 +0200 |
| parents | b50eed0cc0ef |
| children |
line wrap: on
line source
# 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) }
