Skip to content

Commit 9cdf2fd

Browse files
committed
add pagemode, pagelayout commands
1 parent 9b1d686 commit 9cdf2fd

26 files changed

+1380
-95
lines changed

cmd/pdfcpu/init.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ func initWatermarkCmdMap() commandMap {
193193
return m
194194
}
195195

196+
func initPageModeCmdMap() commandMap {
197+
m := newCommandMap()
198+
for k, v := range map[string]command{
199+
"list": {processListPageModeCommand, nil, "", ""},
200+
"set": {processSetPageModeCommand, nil, "", ""},
201+
"reset": {processResetPageModeCommand, nil, "", ""},
202+
} {
203+
m.register(k, v)
204+
}
205+
return m
206+
}
207+
208+
func initPageLayoutCmdMap() commandMap {
209+
m := newCommandMap()
210+
for k, v := range map[string]command{
211+
"list": {processListPageLayoutCommand, nil, "", ""},
212+
"set": {processSetPageLayoutCommand, nil, "", ""},
213+
"reset": {processResetPageLayoutCommand, nil, "", ""},
214+
} {
215+
m.register(k, v)
216+
}
217+
return m
218+
}
219+
196220
func initCommandMap() {
197221
annotsCmdMap := initAnnotsCmdMap()
198222
attachCmdMap := initAttachCmdMap()
@@ -208,6 +232,8 @@ func initCommandMap() {
208232
propertiesCmdMap := initPropertiesCmdMap()
209233
stampCmdMap := initStampCmdMap()
210234
watermarkCmdMap := initWatermarkCmdMap()
235+
pageModeCmdMap := initPageModeCmdMap()
236+
pageLayoutCmdMap := initPageLayoutCmdMap()
211237

212238
cmdMap = newCommandMap()
213239

@@ -240,6 +266,8 @@ func initCommandMap() {
240266
"ndown": {processNDownCommand, nil, usageNDown, usageLongNDown},
241267
"nup": {processNUpCommand, nil, usageNUp, usageLongNUp},
242268
"optimize": {processOptimizeCommand, nil, usageOptimize, usageLongOptimize},
269+
"pagelayout": {nil, pageLayoutCmdMap, usagePageLayout, usageLongPageLayout},
270+
"pagemode": {nil, pageModeCmdMap, usagePageMode, usageLongPageMode},
243271
"pages": {nil, pagesCmdMap, usagePages, usageLongPages},
244272
"paper": {printPaperSizes, nil, usagePaper, usageLongPaper},
245273
"permissions": {nil, permissionsCmdMap, usagePerm, usageLongPerm},

cmd/pdfcpu/process.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,3 +2316,97 @@ func processRemoveBookmarksCommand(conf *model.Configuration) {
23162316

23172317
process(cli.RemoveBookmarksCommand(inFile, outFile, conf))
23182318
}
2319+
2320+
func processListPageLayoutCommand(conf *model.Configuration) {
2321+
if len(flag.Args()) != 1 || selectedPages != "" {
2322+
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePageLayoutList)
2323+
os.Exit(1)
2324+
}
2325+
2326+
inFile := flag.Arg(0)
2327+
if conf.CheckFileNameExt {
2328+
ensurePDFExtension(inFile)
2329+
}
2330+
process(cli.ListPageLayoutCommand(inFile, conf))
2331+
}
2332+
2333+
func processSetPageLayoutCommand(conf *model.Configuration) {
2334+
if len(flag.Args()) != 2 || selectedPages != "" {
2335+
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePageLayoutSet)
2336+
os.Exit(1)
2337+
}
2338+
2339+
inFile := flag.Arg(0)
2340+
if conf.CheckFileNameExt {
2341+
ensurePDFExtension(inFile)
2342+
}
2343+
2344+
v := flag.Arg(1)
2345+
2346+
if !validate.DocumentPageLayout(v) {
2347+
fmt.Fprintln(os.Stderr, "invalid page layout, use one of: SinglePage, TwoColumnLeft, TwoColumnRight, TwoPageLeft, TwoPageRight")
2348+
os.Exit(1)
2349+
}
2350+
2351+
process(cli.SetPageLayoutCommand(inFile, "", v, conf))
2352+
}
2353+
2354+
func processResetPageLayoutCommand(conf *model.Configuration) {
2355+
if len(flag.Args()) != 1 || selectedPages != "" {
2356+
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePageLayoutReset)
2357+
os.Exit(1)
2358+
}
2359+
2360+
inFile := flag.Arg(0)
2361+
if conf.CheckFileNameExt {
2362+
ensurePDFExtension(inFile)
2363+
}
2364+
process(cli.ResetPageLayoutCommand(inFile, "", conf))
2365+
}
2366+
2367+
func processListPageModeCommand(conf *model.Configuration) {
2368+
if len(flag.Args()) != 1 || selectedPages != "" {
2369+
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePageModeList)
2370+
os.Exit(1)
2371+
}
2372+
2373+
inFile := flag.Arg(0)
2374+
if conf.CheckFileNameExt {
2375+
ensurePDFExtension(inFile)
2376+
}
2377+
process(cli.ListPageModeCommand(inFile, conf))
2378+
}
2379+
2380+
func processSetPageModeCommand(conf *model.Configuration) {
2381+
if len(flag.Args()) != 2 || selectedPages != "" {
2382+
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePageModeSet)
2383+
os.Exit(1)
2384+
}
2385+
2386+
inFile := flag.Arg(0)
2387+
if conf.CheckFileNameExt {
2388+
ensurePDFExtension(inFile)
2389+
}
2390+
2391+
v := flag.Arg(1)
2392+
2393+
if !validate.DocumentPageMode(v) {
2394+
fmt.Fprintln(os.Stderr, "invalid page mode, use one of: UseNone, UseThumb, FullScreen, UseOC, UseAttachments")
2395+
os.Exit(1)
2396+
}
2397+
2398+
process(cli.SetPageModeCommand(inFile, "", v, conf))
2399+
}
2400+
2401+
func processResetPageModeCommand(conf *model.Configuration) {
2402+
if len(flag.Args()) != 1 || selectedPages != "" {
2403+
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePageModeReset)
2404+
os.Exit(1)
2405+
}
2406+
2407+
inFile := flag.Arg(0)
2408+
if conf.CheckFileNameExt {
2409+
ensurePDFExtension(inFile)
2410+
}
2411+
process(cli.ResetPageModeCommand(inFile, "", conf))
2412+
}

cmd/pdfcpu/usage.go

Lines changed: 187 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,50 @@ Usage:
2525
2626
The commands are:
2727
28-
annotations list, remove page annotations
29-
attachments list, add, remove, extract embedded file attachments
30-
booklet arrange pages onto larger sheets of paper to make a booklet or zine
31-
bookmarks list, import, export, remove bookmarks
32-
boxes list, add, remove page boundaries for selected pages
33-
changeopw change owner password
34-
changeupw change user password
35-
collect create custom sequence of selected pages
36-
config print configuration
37-
create create PDF content including forms via JSON
38-
crop set cropbox for selected pages
39-
cut custom cut pages horizontally or vertically
40-
decrypt remove password protection
41-
encrypt set password protection
42-
extract extract images, fonts, content, pages or metadata
43-
fonts install, list supported fonts, create cheat sheets
44-
form list, remove fields, lock, unlock, reset, export, fill form via JSON or CSV
45-
grid rearrange pages or images for enhanced browsing experience
46-
images list images for selected pages
47-
import import/convert images to PDF
48-
info print file info
49-
keywords list, add, remove keywords
50-
merge concatenate PDFs
51-
ndown cut selected pages into n pages symmetrically
52-
nup rearrange pages or images for reduced number of pages
53-
optimize optimize PDF by getting rid of redundant page resources
54-
pages insert, remove selected pages
55-
paper print list of supported paper sizes
56-
permissions list, set user access permissions
57-
portfolio list, add, remove, extract portfolio entries with optional description
58-
poster cut selected pages into poster using paper size or dimensions
59-
properties list, add, remove document properties
60-
resize scale selected pages
61-
rotate rotate selected pages
62-
selectedpages print definition of the -pages flag
63-
split split up a PDF by span or bookmark
64-
stamp add, remove, update Unicode text, image or PDF stamps for selected pages
65-
trim create trimmed version of selected pages
66-
validate validate PDF against PDF 32000-1:2008 (PDF 1.7)
67-
version print version
68-
watermark add, remove, update Unicode text, image or PDF watermarks for selected pages
28+
annotations list, remove page annotations
29+
attachments list, add, remove, extract embedded file attachments
30+
booklet arrange pages onto larger sheets of paper to make a booklet or zine
31+
bookmarks list, import, export, remove bookmarks
32+
boxes list, add, remove page boundaries for selected pages
33+
changeopw change owner password
34+
changeupw change user password
35+
collect create custom sequence of selected pages
36+
config print configuration
37+
create create PDF content including forms via JSON
38+
crop set cropbox for selected pages
39+
cut custom cut pages horizontally or vertically
40+
decrypt remove password protection
41+
encrypt set password protection
42+
extract extract images, fonts, content, pages or metadata
43+
fonts install, list supported fonts, create cheat sheets
44+
form list, remove fields, lock, unlock, reset, export, fill form via JSON or CSV
45+
grid rearrange pages or images for enhanced browsing experience
46+
images list images for selected pages
47+
import import/convert images to PDF
48+
info print file info
49+
keywords list, add, remove keywords
50+
merge concatenate PDFs
51+
ndown cut selected pages into n pages symmetrically
52+
nup rearrange pages or images for reduced number of pages
53+
optimize optimize PDF by getting rid of redundant page resources
54+
pagelayout list, set, reset page layout for opened document
55+
pagemode list, set, reset page mode for opened document
56+
pages insert, remove selected pages
57+
paper print list of supported paper sizes
58+
permissions list, set user access permissions
59+
portfolio list, add, remove, extract portfolio entries with optional description
60+
poster cut selected pages into poster using paper size or dimensions
61+
properties list, add, remove document properties
62+
resize scale selected pages
63+
rotate rotate selected pages
64+
selectedpages print definition of the -pages flag
65+
split split up a PDF by span or bookmark
66+
stamp add, remove, update Unicode text, image or PDF stamps for selected pages
67+
trim create trimmed version of selected pages
68+
validate validate PDF against PDF 32000-1:2008 (PDF 1.7)
69+
version print version
70+
viewpreferences list, set, reset viewing preferences for opened document
71+
watermark add, remove, update Unicode text, image or PDF watermarks for selected pages
6972
7073
All instantly recognizable command prefixes are supported eg. val for validation
7174
One letter Unix style abbreviations supported for flags and command parameters.
@@ -1322,4 +1325,147 @@ description ... scalefactor, dimensions, formsize, enforce, border, bgcolor
13221325
outFile ... output PDF file
13231326
outFileJSON ... output PDF file
13241327
`
1328+
1329+
usagePageLayoutList = "pdfcpu pagelayout list inFile"
1330+
usagePageLayoutSet = "pdfcpu pagelayout set inFile value"
1331+
usagePageLayoutReset = "pdfcpu pagelayout reset inFile" + generalFlags
1332+
1333+
usagePageLayout = "usage: " + usagePageLayoutList +
1334+
"\n " + usagePageLayoutSet +
1335+
"\n " + usagePageLayoutReset
1336+
1337+
usageLongPageLayout = `Manage the page layout which shall be used when the document is opened:
1338+
1339+
inFile ... input PDF file
1340+
value ... one of:
1341+
1342+
SinglePage ... Display one page at a time (default)
1343+
TwoColumnLeft ... Display the pages in two columns, with odd- numbered pages on the left
1344+
TwoColumnRight ... Display the pages in two columns, with odd- numbered pages on the right
1345+
TwoPageLeft ... Display the pages two at a time, with odd-numbered pages on the left
1346+
TwoPageRight ... Display the pages two at a time, with odd-numbered pages on the right
1347+
1348+
Eg. set page layout:
1349+
pdfcpu pagelayout set test.pdf TwoPageLeft
1350+
1351+
remove page layout:
1352+
pdfcpu pagelayout remove test.pdf
1353+
`
1354+
1355+
usagePageModeList = "pdfcpu pagemode list inFile"
1356+
usagePageModeSet = "pdfcpu pagemode set inFile value"
1357+
usagePageModeReset = "pdfcpu pagemode reset inFile" + generalFlags
1358+
1359+
usagePageMode = "usage: " + usagePageModeList +
1360+
"\n " + usagePageModeSet +
1361+
"\n " + usagePageModeReset
1362+
1363+
usageLongPageMode = `Manage how the document shall be displayed when opened:
1364+
1365+
inFile ... input PDF file
1366+
value ... one of:
1367+
1368+
UseNone ... Neither document outline nor thumbnail images visible (default)
1369+
UseOutlines ... Document outline visible
1370+
UseThumbs ... Thumbnail images visible
1371+
FullScreen ... Full-screen mode, with no menu bar, window controls, or any other window visible
1372+
UseOC ... Optional content group panel visible (since PDF 1.5)
1373+
UseAttachments ... Attachments panel visible (since PDF 1.6)
1374+
1375+
Eg. set page mode:
1376+
pdfcpu pagemode set test.pdf UseOutlines
1377+
1378+
remove page mode:
1379+
pdfcpu pagemode remove test.pdf
1380+
`
1381+
1382+
usageViewPrefList = "pdfcpu viewpreferences list inFile"
1383+
usageViewPrefSet = "pdfcpu viewpreferences set inFile inFileJSON | nameValuePair..."
1384+
usageViewPrefReset = "pdfcpu viewpreferences reset inFile" + generalFlags
1385+
1386+
usageViewPrefMode = "usage: " + usageViewPrefList +
1387+
"\n " + usageViewPrefSet +
1388+
"\n " + usageViewPrefReset
1389+
1390+
usageLongViewPrefMode = `Manage the way the document shall be displayed on the screen and shall be printed:
1391+
1392+
inFile ... input PDF file
1393+
inFileJSON ... input JSON file containing viewing preferences
1394+
nameValuePair ... 'name = value'
1395+
1396+
The preferences are:
1397+
1398+
HideToolbar ... Hide tool bars when the document is active (default=false).
1399+
HideMenubar ... Hide the menu bar when the document is active (default=false).
1400+
HideWindowUI ... Hide user interface elements in the document’s window (default=false).
1401+
FitWindow ... Resize the document’s window to fit the size of the first displayed page (default=false).
1402+
CenterWindow ... Position the document’s window in the centre of the screen (default=false).
1403+
DisplayDocTitle ... true: The window’s title bar should display the document title taken from the dc:title element of the XMP metadata stream.
1404+
false: The title bar should display the name of the PDF file containing the document (default=false).
1405+
1406+
NonFullScreenPageMode ... How to display the document on exiting full-screen mode:
1407+
UseNone = Neither document outline nor thumbnail images visible (=default)
1408+
UseOutlines = Documentoutlinevisible
1409+
UseThumbs = Thumbnail images visible
1410+
UseOC = Optional content group panel visible
1411+
1412+
Direction ... The predominant logical content order for text
1413+
L2R = Left to right (=default)
1414+
R2L = Right to left (including vertical writing systems, such as Chinese, Japanese, and Korean)
1415+
1416+
Duplex ... The paper handling option that shall be used when printing the file from the print dialogue (since PDF 1.7):
1417+
Simplex = Print single-sided
1418+
DuplexFlipShortEdge = Duplex and flip on the short edge of the sheet
1419+
DuplexFlipLongEdge = Duplex and flip on the long edge of the sheet
1420+
1421+
PickTrayByPDFSize ... Whether the PDF page size shall be used to select the input paper tray.
1422+
PrintPageRange ... The page numbers used to initialize the print dialogue box when the file is printed (since PDF 1.7).
1423+
The array shall contain an even number of integers to be interpreted in pairs, with each pair specifying
1424+
the first and last pages in a sub-range of pages to be printed. The first page of the PDF file shall be denoted by 1.
1425+
NumCopies ... The number of copies that shall be printed when the print dialog is opened for this file (since PDF 1.7).
1426+
Enforce ... Array of names of Viewer preference settings that shall be enforced by PDF processors and
1427+
that shall not be overridden by subsequent selections in the application user interface (since PDF 2.0).
1428+
Possible values: PrintScaling
1429+
1430+
Eg. list viewer preferences:
1431+
pdfcpu viewpref list test.pdf
1432+
1433+
remove viewer preferences:
1434+
pdfcpu viewpref remove test.pdf
1435+
1436+
set printer preferences:
1437+
pdfcpu viewpref add test.pdf 'duplex = duplexFlipShortEdge' 'printPageRange = 1 4 10 20' 'numcopies = 3'
1438+
1439+
set viewer preferences:
1440+
pdfcpu viewpref add test.pdf 'fitwindow = true', 'hidetoolbar = t', 'centerwindow = yes', 'hidemenubar = y'
1441+
1442+
set viewer preferences via JSON:
1443+
pdfcpu viewpref add test.pdf viewpref.json
1444+
1445+
and eg. viewpref.json (each preferences is optional!):
1446+
1447+
{
1448+
"viewingPreferences": {
1449+
"HideToolBar": true,
1450+
"HideMenuBar": false,
1451+
"HideWindowUI": false,
1452+
"FitWindow": true,
1453+
"CenterWindow": true,
1454+
"DisplayDocTitle": true,
1455+
"NonFullScreenPageMode": "UseThumbs",
1456+
"Direction": "R2L",
1457+
"Duplex": "Simplex",
1458+
"PickTrayByPDFSize": false,
1459+
"PrintPageRange": [
1460+
1, 4,
1461+
10, 20
1462+
],
1463+
"NumCopies": 3,
1464+
"Enforce": [
1465+
"PrintScaling"
1466+
]
1467+
}
1468+
}
1469+
1470+
`
13251471
)

pkg/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func readAndValidate(rs io.ReadSeeker, conf *model.Configuration, from1 time.Tim
139139
return ctx, dur1, dur2, nil
140140
}
141141

142+
// ReadValidateAndOptimize returns the model.Context of rs ready for processing.
142143
func ReadValidateAndOptimize(rs io.ReadSeeker, conf *model.Configuration, from1 time.Time) (ctx *model.Context, dur1, dur2, dur3 float64, err error) {
143144
ctx, dur1, dur2, err = readAndValidate(rs, conf, from1)
144145
if err != nil {

0 commit comments

Comments
 (0)