github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/stamp_test.go (about)

     1  /*
     2  Copyright 2020 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 test
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/cli"
    24  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
    25  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
    26  	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
    27  )
    28  
    29  func testAddWatermarks(t *testing.T, msg, inFile, outFile string, selectedPages []string, mode, modeParm, desc string, onTop bool) {
    30  	t.Helper()
    31  	inFile = filepath.Join(inDir, inFile)
    32  	outFile = filepath.Join(outDir, outFile)
    33  	unit := types.POINTS
    34  
    35  	var (
    36  		wm  *model.Watermark
    37  		err error
    38  	)
    39  	switch mode {
    40  	case "text":
    41  		wm, err = pdfcpu.ParseTextWatermarkDetails(modeParm, desc, onTop, unit)
    42  	case "image":
    43  		wm, err = pdfcpu.ParseImageWatermarkDetails(modeParm, desc, onTop, unit)
    44  	case "pdf":
    45  		wm, err = pdfcpu.ParsePDFWatermarkDetails(modeParm, desc, onTop, unit)
    46  	}
    47  	if err != nil {
    48  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    49  	}
    50  
    51  	cmd := cli.AddWatermarksCommand(inFile, outFile, selectedPages, wm, conf)
    52  	if _, err := cli.Process(cmd); err != nil {
    53  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
    54  	}
    55  	if err := validateFile(t, outFile, conf); err != nil {
    56  		t.Fatalf("%s: %v\n", msg, err)
    57  	}
    58  }
    59  
    60  func TestAddWatermarks(t *testing.T) {
    61  	for _, tt := range []struct {
    62  		msg             string
    63  		inFile, outFile string
    64  		selectedPages   []string
    65  		onTop           bool
    66  		mode            string
    67  		modeParm        string
    68  		wmConf          string
    69  	}{
    70  		// Add text watermark to all pages of inFile starting at page 1 using a rotation angle of 20 degrees.
    71  		{"TestWatermarkText",
    72  			"Acroforms2.pdf",
    73  			"testwm.pdf",
    74  			[]string{"1-"},
    75  			false,
    76  			"text",
    77  			"Draft",
    78  			"scale:0.7, rot:20"},
    79  
    80  		// Add a greenish, slightly transparent stroked and filled text stamp to all odd pages of inFile other than page 1
    81  		// using the default rotation which is aligned along the first diagonal running from lower left to upper right corner.
    82  		{"TestStampText",
    83  			"pike-stanford.pdf",
    84  			"testStampText1.pdf",
    85  			[]string{"odd", "!1"},
    86  			true,
    87  			"text",
    88  			"Demo",
    89  			"font:Courier, c: 0 .8 0, op:0.8, mode:2"},
    90  
    91  		// Add a red filled text stamp to all odd pages of inFile other than page 1 using a font size of 48 points
    92  		// and the default rotation which is aligned along the first diagonal running from lower left to upper right corner.
    93  		{"TestStampTextUsingFontsize",
    94  			"pike-stanford.pdf",
    95  			"testStampText2.pdf",
    96  			[]string{"odd", "!1"},
    97  			true,
    98  			"text",
    99  			"Demo",
   100  			"font:Courier, c: 1 0 0, op:1, scale:1 abs, points:48"},
   101  
   102  		// Add image watermark to inFile starting at page 1 using no rotation.
   103  		{"TestWatermarkImage",
   104  			"Acroforms2.pdf", "testWMImageRel.pdf",
   105  			[]string{"1-"},
   106  			false,
   107  			"image",
   108  			filepath.Join(resDir, "pdfchip3.png"),
   109  			"rot:0"},
   110  
   111  		// Add image stamp to inFile using absolute scaling and a negative rotation of 90 degrees.
   112  		{"TestStampImageAbsScaling",
   113  			"Acroforms2.pdf",
   114  			"testWMImageAbs.pdf",
   115  			[]string{"1-"},
   116  			true,
   117  			"image",
   118  			filepath.Join(resDir, "pdfchip3.png"),
   119  			"scale:.5 a, rot:-90"},
   120  
   121  		// Add a PDF stamp to all pages of inFile using the 3rd page of pdfFile
   122  		// and rotate along the 2nd diagonal running from upper left to lower right corner.
   123  		{"TestWatermarkText",
   124  			"Acroforms2.pdf",
   125  			"testStampPDF.pdf",
   126  			nil,
   127  			true,
   128  			"pdf",
   129  			filepath.Join(inDir, "Wonderwall.pdf:3"),
   130  			"d:2"},
   131  
   132  		// Add a PDF multistamp to all pages of inFile
   133  		// and rotate along the 2nd diagonal running from upper left to lower right corner.
   134  		{"TestWatermarkText",
   135  			"Acroforms2.pdf",
   136  			"testMultistampPDF.pdf",
   137  			nil,
   138  			true,
   139  			"pdf",
   140  			filepath.Join(inDir, "Wonderwall.pdf"),
   141  			"d:2"},
   142  	} {
   143  		testAddWatermarks(t, tt.msg, tt.inFile, tt.outFile, tt.selectedPages, tt.mode, tt.modeParm, tt.wmConf, tt.onTop)
   144  	}
   145  }
   146  
   147  func TestStampingLifecycle(t *testing.T) {
   148  	msg := "TestStampingLifecycle"
   149  	inFile := filepath.Join(inDir, "Acroforms2.pdf")
   150  	outFile := filepath.Join(outDir, "stampLC.pdf")
   151  	onTop := true // we are testing stamps
   152  	unit := types.POINTS
   153  
   154  	// Stamp all pages.
   155  	wm, err := pdfcpu.ParseTextWatermarkDetails("Demo", "", onTop, unit)
   156  	if err != nil {
   157  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   158  	}
   159  	cmd := cli.AddWatermarksCommand(inFile, outFile, nil, wm, conf)
   160  	if _, err := cli.Process(cmd); err != nil {
   161  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   162  	}
   163  
   164  	// // Update stamp on page 1.
   165  	wm, err = pdfcpu.ParseTextWatermarkDetails("Confidential", "", onTop, unit)
   166  	if err != nil {
   167  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   168  	}
   169  	wm.Update = true
   170  	cmd = cli.AddWatermarksCommand(outFile, "", []string{"1"}, wm, conf)
   171  	if _, err := cli.Process(cmd); err != nil {
   172  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   173  	}
   174  
   175  	// Add another stamp on top for all pages.
   176  	// This is a redish transparent footer.
   177  	wm, err = pdfcpu.ParseTextWatermarkDetails("Footer", "pos:bc, c:0.8 0 0, op:.6, rot:0", onTop, unit)
   178  	if err != nil {
   179  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   180  	}
   181  	cmd = cli.AddWatermarksCommand(outFile, "", nil, wm, conf)
   182  	if _, err := cli.Process(cmd); err != nil {
   183  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   184  	}
   185  
   186  	// Remove stamp on page 1.
   187  	cmd = cli.RemoveWatermarksCommand(outFile, "", []string{"1"}, conf)
   188  	if _, err := cli.Process(cmd); err != nil {
   189  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   190  	}
   191  
   192  	// Remove all stamps.
   193  	cmd = cli.RemoveWatermarksCommand(outFile, "", nil, conf)
   194  	if _, err := cli.Process(cmd); err != nil {
   195  		t.Fatalf("%s %s: %v\n", msg, outFile, err)
   196  	}
   197  
   198  	// Validate the result.
   199  	if err := validateFile(t, outFile, conf); err != nil {
   200  		t.Fatalf("%s: %v\n", msg, err)
   201  	}
   202  }