github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/page_test.go (about) 1 /* 2 Copyright 2020 The pdf Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "path/filepath" 21 "testing" 22 23 "github.com/pdfcpu/pdfcpu/pkg/api" 24 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu" 25 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" 26 ) 27 28 func TestInsertRemovePages(t *testing.T) { 29 msg := "TestInsertRemovePages" 30 inFile := filepath.Join(inDir, "Acroforms2.pdf") 31 outFile := filepath.Join(outDir, "test.pdf") 32 33 n1, err := api.PageCountFile(inFile) 34 if err != nil { 35 t.Fatalf("%s %s: %v\n", msg, inFile, err) 36 } 37 38 // Insert an empty page before pages 1 and 2. 39 if err := api.InsertPagesFile(inFile, outFile, []string{"-2"}, true, nil, nil); err != nil { 40 t.Fatalf("%s %s: %v\n", msg, outFile, err) 41 } 42 if err := api.ValidateFile(outFile, nil); err != nil { 43 t.Fatalf("%s: %v\n", msg, err) 44 } 45 n2, err := api.PageCountFile(outFile) 46 if err != nil { 47 t.Fatalf("%s %s: %v\n", msg, outFile, err) 48 } 49 if n2 != n1+2 { 50 t.Fatalf("%s %s: pageCount want:%d got:%d\n", msg, inFile, n1+2, n2) 51 } 52 53 // // Remove pages 1 and 2. 54 if err := api.RemovePagesFile(outFile, "", []string{"-2"}, nil); err != nil { 55 t.Fatalf("%s %s: %v\n", msg, outFile, err) 56 } 57 if err := api.ValidateFile(outFile, nil); err != nil { 58 t.Fatalf("%s: %v\n", msg, err) 59 } 60 n2, err = api.PageCountFile(outFile) 61 if err != nil { 62 t.Fatalf("%s %s: %v\n", msg, inFile, err) 63 } 64 if n1 != n2 { 65 t.Fatalf("%s %s: pageCount want:%d got:%d\n", msg, inFile, n1, n2) 66 } 67 } 68 69 func TestInsertCustomBlankPage(t *testing.T) { 70 msg := "TestInsertCustomBlankPage" 71 inFile := filepath.Join(inDir, "Acroforms2.pdf") 72 outFile := filepath.Join(outDir, "test.pdf") 73 74 selectedPages := []string{"2"} 75 76 before := false 77 78 pageConf, err := pdfcpu.ParsePageConfiguration("f:A5L", conf.Unit) 79 if err != nil { 80 t.Fatalf("%s %s: %v\n", msg, outFile, err) 81 } 82 83 // Insert an empty A5 page in landscape mode after page 5. 84 if err := api.InsertPagesFile(inFile, outFile, selectedPages, before, pageConf, conf); err != nil { 85 t.Fatalf("%s %s: %v\n", msg, outFile, err) 86 } 87 88 selectedPages = []string{"odd"} 89 90 pageConf, err = pdfcpu.ParsePageConfiguration("dim:5 10", types.CENTIMETRES) 91 if err != nil { 92 t.Fatalf("%s %s: %v\n", msg, outFile, err) 93 } 94 95 // Insert an empty page with dimensions 5 x 10 cm after every odd page. 96 if err := api.InsertPagesFile(inFile, outFile, selectedPages, before, pageConf, conf); err != nil { 97 t.Fatalf("%s %s: %v\n", msg, outFile, err) 98 } 99 100 }