github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/example_test.go (about) 1 /* 2 Copyright 2018 The pdfcpu 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 api 18 19 import ( 20 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 21 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" 22 ) 23 24 func ExampleValidateFile() { 25 26 // Use the default configuration to validate in.pdf. 27 ValidateFile("in.pdf", nil) 28 } 29 30 func ExampleOptimizeFile() { 31 32 conf := model.NewDefaultConfiguration() 33 34 // Set passwords for encrypted files. 35 conf.UserPW = "upw" 36 conf.OwnerPW = "opw" 37 38 // Configure end of line sequence for writing. 39 conf.Eol = types.EolLF 40 41 // Create an optimized version of in.pdf and write it to out.pdf. 42 OptimizeFile("in.pdf", "out.pdf", conf) 43 44 // Create an optimized version of inFile. 45 // If you want to modify the original file, pass an empty string for outFile. 46 // Use nil for a default configuration. 47 OptimizeFile("in.pdf", "", nil) 48 } 49 50 func ExampleTrimFile() { 51 52 // Create a trimmed version of in.pdf containing odd page numbers only. 53 TrimFile("in.pdf", "outFile", []string{"odd"}, nil) 54 55 // Create a trimmed version of in.pdf containing the first two pages only. 56 // If you want to modify the original file, pass an empty string for outFile. 57 TrimFile("in.pdf", "", []string{"1-2"}, nil) 58 } 59 60 func ExampleSplitFile() { 61 62 // Create single page PDFs for in.pdf in outDir using the default configuration. 63 SplitFile("in.pdf", "outDir", 1, nil) 64 65 // Create dual page PDFs for in.pdf in outDir using the default configuration. 66 SplitFile("in.pdf", "outDir", 2, nil) 67 68 // Create a sequence of PDFs representing bookmark sections. 69 SplitFile("in.pdf", "outDir", 0, nil) 70 } 71 72 func ExampleRotateFile() { 73 74 // Rotate all pages of in.pdf, clockwise by 90 degrees and write the result to out.pdf. 75 RotateFile("in.pdf", "out.pdf", 90, nil, nil) 76 77 // Rotate the first page of in.pdf by 180 degrees. 78 // If you want to modify the original file, pass an empty string as outFile. 79 RotateFile("in.pdf", "", 180, []string{"1"}, nil) 80 } 81 82 func ExampleMergeCreateFile() { 83 84 // Merge inFiles by concatenation in the order specified and write the result to out.pdf. 85 // out.pdf will be overwritten. 86 inFiles := []string{"in1.pdf", "in2.pdf"} 87 MergeCreateFile(inFiles, "out.pdf", false, nil) 88 } 89 90 func ExampleMergeAppendFile() { 91 92 // Merge inFiles by concatenation in the order specified and write the result to out.pdf. 93 // If out.pdf already exists it will be preserved and serves as the beginning of the merge result. 94 inFiles := []string{"in1.pdf", "in2.pdf"} 95 MergeAppendFile(inFiles, "out.pdf", false, nil) 96 } 97 98 func ExampleInsertPagesFile() { 99 100 // Insert a blank page into in.pdf before page #3. 101 InsertPagesFile("in.pdf", "", []string{"3"}, true, nil, nil) 102 103 // Insert a blank page into in.pdf after every page. 104 InsertPagesFile("in.pdf", "", nil, false, nil, nil) 105 } 106 107 func ExampleRemovePagesFile() { 108 109 // Remove pages 2 and 8 of in.pdf. 110 RemovePagesFile("in.pdf", "", []string{"2", "8"}, nil) 111 112 // Remove first 2 pages of in.pdf. 113 RemovePagesFile("in.pdf", "", []string{"-2"}, nil) 114 115 // Remove all pages >= 10 of in.pdf. 116 RemovePagesFile("in.pdf", "", []string{"10-"}, nil) 117 } 118 119 func ExampleAddWatermarksFile() { 120 121 // Unique abbreviations are accepted for all watermark descriptor parameters. 122 // eg. sc = scalefactor or rot = rotation 123 124 // Add a "Demo" watermark to all pages of in.pdf along the diagonal running from lower left to upper right. 125 onTop := false 126 update := false 127 wm, _ := TextWatermark("Demo", "", onTop, update, types.POINTS) 128 AddWatermarksFile("in.pdf", "", nil, wm, nil) 129 130 // Stamp all odd pages of in.pdf in red "Confidential" in 48 point Courier 131 // using a rotation angle of 45 degrees and an absolute scalefactor of 1.0. 132 onTop = true 133 wm, _ = TextWatermark("Confidential", "font:Courier, points:48, col: 1 0 0, rot:45, scale:1 abs, ", onTop, update, types.POINTS) 134 AddWatermarksFile("in.pdf", "", []string{"odd"}, wm, nil) 135 136 // Add image stamps to in.pdf using absolute scaling and a negative rotation of 90 degrees. 137 wm, _ = ImageWatermark("image.png", "scalefactor:.5 a, rot:-90", onTop, update, types.POINTS) 138 AddWatermarksFile("in.pdf", "", nil, wm, nil) 139 140 // Add a PDF stamp to all pages of in.pdf using the 2nd page of stamp.pdf, use absolute scaling of 0.5 141 // and rotate along the 2nd diagonal running from upper left to lower right corner. 142 wm, _ = PDFWatermark("stamp.pdf:2", "scale:.5 abs, diagonal:2", onTop, update, types.POINTS) 143 AddWatermarksFile("in.pdf", "", nil, wm, nil) 144 } 145 146 func ExampleRemoveWatermarksFile() { 147 148 // Add a "Demo" stamp to all pages of in.pdf along the diagonal running from lower left to upper right. 149 onTop := true 150 update := false 151 wm, _ := TextWatermark("Demo", "", onTop, update, types.POINTS) 152 AddWatermarksFile("in.pdf", "", nil, wm, nil) 153 154 // Update stamp for correction: 155 update = true 156 wm, _ = TextWatermark("Confidential", "", onTop, update, types.POINTS) 157 AddWatermarksFile("in.pdf", "", nil, wm, nil) 158 159 // Add another watermark on top of page 1 160 wm, _ = TextWatermark("Footer stamp", "c:.5 1 1, pos:bc", onTop, update, types.POINTS) 161 AddWatermarksFile("in.pdf", "", nil, wm, nil) 162 163 // Remove watermark on page 1 164 RemoveWatermarksFile("in.pdf", "", []string{"1"}, nil) 165 166 // Remove all watermarks 167 RemoveWatermarksFile("in.pdf", "", nil, nil) 168 } 169 170 func ExampleImportImagesFile() { 171 172 // Convert an image into a single page of out.pdf which will be created if necessary. 173 // The page dimensions will match the image dimensions. 174 // If out.pdf already exists, append a new page. 175 // Use the default import configuration. 176 ImportImagesFile([]string{"image.png"}, "out.pdf", nil, nil) 177 178 // Import images by creating an A3 page for each image. 179 // Images are page centered with 1.0 relative scaling. 180 // Import an image as a new page of the existing out.pdf. 181 imp, _ := Import("form:A3, pos:c, s:1.0", types.POINTS) 182 ImportImagesFile([]string{"a1.png", "a2.jpg", "a3.tiff"}, "out.pdf", imp, nil) 183 } 184 185 func ExampleNUpFile() { 186 187 // 4-Up in.pdf and write result to out.pdf. 188 nup, _ := PDFNUpConfig(4, "", nil) 189 inFiles := []string{"in.pdf"} 190 NUpFile(inFiles, "out.pdf", nil, nup, nil) 191 192 // 9-Up a sequence of images using format Tabloid w/o borders and no margins. 193 nup, _ = ImageNUpConfig(9, "f:Tabloid, b:off, m:0", nil) 194 inFiles = []string{"in1.png", "in2.jpg", "in3.tiff"} 195 NUpFile(inFiles, "out.pdf", nil, nup, nil) 196 197 // TestGridFromPDF 198 nup, _ = PDFGridConfig(1, 3, "f:LegalL", nil) 199 inFiles = []string{"in.pdf"} 200 NUpFile(inFiles, "out.pdf", nil, nup, nil) 201 202 // TestGridFromImages 203 nup, _ = ImageGridConfig(4, 2, "d:500 500, m:20, b:off", nil) 204 inFiles = []string{"in1.png", "in2.jpg", "in3.tiff"} 205 NUpFile(inFiles, "out.pdf", nil, nup, nil) 206 } 207 208 func ExampleSetPermissionsFile() { 209 210 // Setting all permissions for the AES-256 encrypted in.pdf. 211 conf := model.NewAESConfiguration("upw", "opw", 256) 212 conf.Permissions = model.PermissionsAll 213 SetPermissionsFile("in.pdf", "", conf) 214 215 // Restricting permissions for the AES-256 encrypted in.pdf. 216 conf = model.NewAESConfiguration("upw", "opw", 256) 217 conf.Permissions = model.PermissionsNone 218 SetPermissionsFile("in.pdf", "", conf) 219 } 220 221 func ExampleEncryptFile() { 222 223 // Encrypting a file using AES-256. 224 conf := model.NewAESConfiguration("upw", "opw", 256) 225 EncryptFile("in.pdf", "", conf) 226 } 227 228 func ExampleDecryptFile() { 229 230 // Decrypting an AES-256 encrypted file. 231 conf := model.NewAESConfiguration("upw", "opw", 256) 232 DecryptFile("in.pdf", "", conf) 233 } 234 235 func ExampleChangeUserPasswordFile() { 236 237 // Changing the user password for an AES-256 encrypted file. 238 conf := model.NewAESConfiguration("upw", "opw", 256) 239 ChangeUserPasswordFile("in.pdf", "", "upw", "upwNew", conf) 240 } 241 242 func ExampleChangeOwnerPasswordFile() { 243 244 // Changing the owner password for an AES-256 encrypted file. 245 conf := model.NewAESConfiguration("upw", "opw", 256) 246 ChangeOwnerPasswordFile("in.pdf", "", "opw", "opwNew", conf) 247 } 248 249 func ExampleAddAttachmentsFile() { 250 251 // Attach 3 files to in.pdf. 252 AddAttachmentsFile("in.pdf", "", []string{"img.jpg", "attach.pdf", "test.zip"}, false, nil) 253 } 254 255 func ExampleRemoveAttachmentsFile() { 256 257 // Remove 1 attachment from in.pdf. 258 RemoveAttachmentsFile("in.pdf", "", []string{"img.jpg"}, nil) 259 260 // Remove all attachments from in.pdf 261 RemoveAttachmentsFile("in.pdf", "", nil, nil) 262 } 263 264 func ExampleExtractAttachmentsFile() { 265 266 // Extract 1 attachment from in.pdf into outDir. 267 ExtractAttachmentsFile("in.pdf", "outDir", []string{"img.jpg"}, nil) 268 269 // Extract all attachments from in.pdf into outDir 270 ExtractAttachmentsFile("in.pdf", "outDir", nil, nil) 271 } 272 273 func ExampleExtractImagesFile() { 274 275 // Extract embedded images from in.pdf into outDir. 276 ExtractImagesFile("in.pdf", "outDir", nil, nil) 277 } 278 279 func ExampleExtractFontsFile() { 280 281 // Extract embedded fonts for pages 1-3 from in.pdf into outDir. 282 ExtractFontsFile("in.pdf", "outDir", []string{"1-3"}, nil) 283 } 284 285 func ExampleExtractContentFile() { 286 287 // Extract content for all pages in PDF syntax from in.pdf into outDir. 288 ExtractContentFile("in.pdf", "outDir", nil, nil) 289 } 290 291 func ExampleExtractPagesFile() { 292 293 // Extract all even numbered pages from in.pdf into outDir. 294 ExtractPagesFile("in.pdf", "outDir", []string{"even"}, nil) 295 } 296 297 func ExampleExtractMetadataFile() { 298 299 // Extract all metadata from in.pdf into outDir. 300 ExtractMetadataFile("in.pdf", "outDir", nil) 301 }