Repository navigation
|
Can you add button to change dpi when convert to image ?
|
Replies: 4 comments 1 reply
|
The Convert PDF to image command has no DPI control. It always renders at 150 DPI. For a different DPI, use the command line tool.
|
|
Added in b67c84cff Convert PDF to Images now has a Resolution (DPI) box: pick 72, 96, 150 (default), 300, 600 or 1200, or type any other value. Save Selection As Image got the same box. It will be in the next pre-release build. |
|
In current SumatraPDF 3.7 pre-release builds, there are UI options for this and a script for the saved PDF. Using the UI Press Ctrl+K → Convert PDF to Images... (also right-click → Document). Choose PNG/JPEG/BMP, the destination template and Current/All/Range pages. The regular conversion dialog uses 150 DPI; use the script for a chosen DPI. Using Run PDF Script
[page] or inserts the page number. Without one, the script adds a page-number suffix. Existing exports are overwritten. From the command line Save the code below as sumatrapdf-tool.exe run "discussion-6283-render-images.js" "input.pdf" "C:\Exports\page-[page].png" "300" "1-3" "90"Replace the example paths/values with yours. Use the full path to // Discussion #6283: https://github.com/sumatrapdfreader/sumatrapdf/discussions/6283
// Info: Export pages as PNG or JPEG images at a chosen DPI.
// Arg: <INPUT> : Input PDF
// Arg: <DIR>/<BASE>-[page].png : Image filename template (.png or .jpg; folder must exist)
// Arg: 300 : DPI
// Arg: all : Page range
// Arg: 90 : JPEG quality
// Page range: all or empty = all pages; 2 = page 2; 4-6 = inclusive; 6-4 = reverse; 4- = through last; N = last; 2-N, N-1.
// Combine with commas: 2,4-6. Pages start at 1 and must exist; order kept, duplicates ignored; all/N ignore case; spaces around pages/separators allowed.
var input = scriptArgs[0];
var output = scriptArgs[1];
if (!input || !output) throw new Error("Supply input PDF and output path.");
function pathKey(s) {
return s.replace(/\\/g, "/").toLowerCase();
}
if (pathKey(input) === pathKey(output)) throw new Error("Choose a different output path.");
var doc = Document.openDocument(input);
if (doc.needsPassword()) throw new Error("Use a decrypted input PDF.");
function save() {
doc.save(output, "garbage=4,compress");
print("Saved " + output);
}
function number(s, fallback) {
var n = s === undefined || s === "" ? fallback : Number(s);
if (!isFinite(n)) throw new Error("Invalid number: " + s);
return n;
}
function pageList(text, count) {
var pages = [];
if (!text || /^all$/i.test(text)) {
for (var i = 0; i < count; i++) pages.push(i);
return pages;
}
var ranges = text.split(",");
for (var i = 0; i < ranges.length; i++) {
var range = ranges[i];
var match = /^\s*(\d+|N)\s*(?:-\s*(\d+|N)?\s*)?$/i.exec(range);
if (!match) throw new Error("Invalid page range: " + range);
var first = /^N$/i.test(match[1]) ? count : Number(match[1]);
var last = first;
if (match[2]) last = /^N$/i.test(match[2]) ? count : Number(match[2]);
else if (range.indexOf("-") >= 0) last = count;
if (first < 1 || last < 1 || first > count || last > count) {
throw new Error("Page outside document: " + range);
}
var step = first <= last ? 1 : -1;
for (var page = first; ; page += step) {
if (pages.indexOf(page - 1) < 0) pages.push(page - 1);
if (page === last) break;
}
}
return pages;
}
var kPointsPerInch = 72;
var dpi = number(scriptArgs[2], 300);
var pageRange = scriptArgs[3];
var selected = pageList(pageRange, doc.countPages());
var quality = number(scriptArgs[4], 90);
if (dpi <= 0 || dpi % 1) throw new Error("DPI must be a positive integer.");
if (quality < 1 || quality > 100) throw new Error("JPEG quality must be 1..100.");
var ext = /\.(png|jpe?g)$/i.exec(output);
if (!ext) throw new Error("Output template must end in .png or .jpg.");
var template = output;
if (!/\[page\]|<N>/.test(template)) template = template.replace(/(\.[^.]+)$/, "-[page]$1");
for (var i = 0; i < selected.length; i++) {
var p = selected[i];
var n = ("000" + (p + 1)).slice(-Math.max(3, String(p + 1).length));
var path = template.replace(/\[page\]|<N>/g, n);
var pix = doc
.loadPage(p)
.toPixmap(Matrix.scale(dpi / kPointsPerInch, dpi / kPointsPerInch), ColorSpace.DeviceRGB, false);
pix.setResolution(dpi, dpi);
if (ext[1].toLowerCase() === "png") pix.saveAsPNG(path);
else pix.saveAsJPEG(path, quality);
print("Saved " + path);
} |





Added in b67c84cff
Convert PDF to Images now has a Resolution (DPI) box: pick 72, 96, 150 (default), 300, 600 or 1200, or type any other value. Save Selection As Image got the same box.
It will be in the next pre-release build.