github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/split_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 ) 26 27 func TestSplitSpan1(t *testing.T) { 28 msg := "TestSplitSpan1" 29 fileName := "Acroforms2.pdf" 30 inFile := filepath.Join(inDir, fileName) 31 32 // Create single page files of inFile in outDir. 33 span := 1 34 if err := api.SplitFile(inFile, outDir, span, nil); err != nil { 35 t.Fatalf("%s: %v\n", msg, err) 36 } 37 } 38 39 func TestSplitSpan2(t *testing.T) { 40 msg := "TestSplitSpan2" 41 fileName := "Acroforms2.pdf" 42 inFile := filepath.Join(inDir, fileName) 43 44 // Create dual page files of inFile in outDir. 45 span := 2 46 if err := api.SplitFile(inFile, outDir, span, nil); err != nil { 47 t.Fatalf("%s: %v\n", msg, err) 48 } 49 } 50 51 func TestSplitByBookmark(t *testing.T) { 52 msg := "TestSplitByBookmark" 53 fileName := "5116.DCT_Filter.pdf" 54 inFile := filepath.Join(inDir, fileName) 55 56 // Split along bookmarks. 57 span := 0 58 if err := api.SplitFile(inFile, outDir, span, nil); err != nil { 59 t.Fatalf("%s: %v\n", msg, err) 60 } 61 } 62 63 func TestSplitByPageNr(t *testing.T) { 64 msg := "TestSplitByPageNr" 65 fileName := "5116.DCT_Filter.pdf" 66 inFile := filepath.Join(inDir, fileName) 67 68 // Generate page section 1 69 // Generate page section 2-9 70 // Generate page section 10-49 71 // Generate page section 50-last page 72 73 if err := api.SplitByPageNrFile(inFile, outDir, []int{2, 10, 50}, nil); err != nil { 74 t.Fatalf("%s: %v\n", msg, err) 75 } 76 } 77 78 func TestSplitLowLevel(t *testing.T) { 79 msg := "TestSplitLowLevel" 80 inFile := filepath.Join(inDir, "TheGoProgrammingLanguageCh1.pdf") 81 outFile := filepath.Join(outDir, "MyExtractedPageSpan.pdf") 82 83 // Create a context. 84 ctx, err := api.ReadContextFile(inFile) 85 if err != nil { 86 t.Fatalf("%s readContext: %v\n", msg, err) 87 } 88 89 // Extract a page span. 90 from, thru := 2, 4 91 selectedPages := api.PagesForPageRange(from, thru) 92 usePgCache := false 93 ctxNew, err := pdfcpu.ExtractPages(ctx, selectedPages, usePgCache) 94 if err != nil { 95 t.Fatalf("%s ExtractPages(%d,%d): %v\n", msg, from, thru, err) 96 } 97 98 // Here you can process this single page PDF context. 99 100 // Write context to file. 101 if err := api.WriteContextFile(ctxNew, outFile); err != nil { 102 t.Fatalf("%s write: %v\n", msg, err) 103 } 104 }