[{"data":1,"prerenderedAt":228},["Reactive",2],{"/guides/go/nethttp/adding-an-image-watermark":3,"related:qlDRX6GhLO":146},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"property":12,"output":13,"related":14,"default":17,"body":18,"_type":141,"_id":142,"_source":143,"_file":144,"_extension":145},"/guides/go/nethttp/adding-an-image-watermark","nethttp",false,"","Adding an image watermark","Discover how to effortlessly add image watermarks to PDFs using Go and the Net/HTTP library. Our guide shows you how to add images on top of your PDF to protect your document easily. This is easily done with a simple request to PDFShift's API.","Go","Net/HTTP","watermark","pdf",[15,16],"protecting-the-generated-pdf","adding-a-text-watermark",true,{"type":19,"children":20,"toc":138},"root",[21,29,42,54,59,74,87,92,117,122,127],{"type":22,"tag":23,"props":24,"children":25},"element","p",{},[26],{"type":27,"value":28},"text","In this guide, we'll walk you through the process of adding text watermarks to your PDF files, using PDFShift's API.",{"type":22,"tag":23,"props":30,"children":31},{},[32,34,40],{"type":27,"value":33},"Adding a watermark to your PDF can be done by adding the ",{"type":22,"tag":35,"props":36,"children":38},"code",{"className":37},[],[39],{"type":27,"value":12},{"type":27,"value":41}," object to your query.\nThe advantage of providing an image is that you can add a visual stamp on top of each of your pages that are generated through PDFShift.",{"type":22,"tag":43,"props":44,"children":49},"pre",{"className":45,"code":47,"language":48,"meta":7},[46],"language-go","package main\n\nimport (\n    \"bytes\"\n    \"encoding/json\"\n    \"fmt\"\n    \"io/ioutil\"\n    \"net/http\"\n)\n\nfunc main() {\n    // You can get an API key at https://pdfshift.io\n    apiKey := \"sk_xxxxxxxxxxxx\"\n\n    params := map[string]interface{}{\n        \"source\": \"https://www.example.com\",\n        \"watermark\": map[string]string{\n            \"image\":       \"https://placekitten.com/200/300\",\n            \"offset_x\":   \"center\",\n            \"offset_top\": \"top\",\n        },\n    }\n\n    // Marshal the parameters into JSON\n    jsonParams, err := json.Marshal(params)\n    if err != nil {\n        fmt.Println(\"Error marshaling JSON:\", err)\n        return\n    }\n\n    // Create a new HTTP client\n    client := &http.Client{}\n\n    // Create a new request\n    req, err := http.NewRequest(\"POST\", \"https://api.pdfshift.io/v3/convert/pdf\", bytes.NewBuffer(jsonParams))\n    if err != nil {\n        fmt.Println(\"Error creating request:\", err)\n        return\n    }\n\n    // Set request headers\n    req.Header.Set(\"Content-Type\", \"application/json\")\n\n    // Set basic authentication header\n    req.Header.Set(\"X-API-Key\", apiKey)\n\n    // Perform the request\n    resp, err := client.Do(req)\n    if err != nil {\n        fmt.Println(\"Error performing request:\", err)\n        return\n    }\n    defer resp.Body.Close()\n\n    // Read response body\n    body, err := ioutil.ReadAll(resp.Body)\n    if err != nil {\n        fmt.Println(\"Error reading response body:\", err)\n        return\n    }\n\n    // Check response status code\n    if resp.StatusCode >= 400 {\n        fmt.Printf(\"Request failed with status code %d: %s\\n\", resp.StatusCode, string(body))\n        return\n    }\n\n    // Save the PDF document\n    err = ioutil.WriteFile(\"result.pdf\", body, 0644)\n    if err != nil {\n        fmt.Println(\"Error saving PDF document:\", err)\n        return\n    }\n\n    fmt.Println(\"The PDF document was generated and saved to result.pdf\")\n}\n","go",[50],{"type":22,"tag":35,"props":51,"children":52},{"__ignoreMap":7},[53],{"type":27,"value":47},{"type":22,"tag":23,"props":55,"children":56},{},[57],{"type":27,"value":58},"The \"image\" parameter for the watermark can be used in two different values:",{"type":22,"tag":60,"props":61,"children":62},"ul",{},[63,69],{"type":22,"tag":64,"props":65,"children":66},"li",{},[67],{"type":27,"value":68},"URL : A full URL to an image that will be used as the watermark.",{"type":22,"tag":64,"props":70,"children":71},{},[72],{"type":27,"value":73},"Base64 : A base64 encoded image that will be used as the watermark.",{"type":22,"tag":23,"props":75,"children":76},{},[77,79,85],{"type":27,"value":78},"Note that you can also rotate the image by passing the ",{"type":22,"tag":35,"props":80,"children":82},{"className":81},[],[83],{"type":27,"value":84},"rotate",{"type":27,"value":86}," parameter as a degree (or negative degree)",{"type":22,"tag":23,"props":88,"children":89},{},[90],{"type":27,"value":91},"You can also customize the position of the text watermark by applying the following properties:",{"type":22,"tag":60,"props":93,"children":94},{},[95,106],{"type":22,"tag":64,"props":96,"children":97},{},[98,104],{"type":22,"tag":35,"props":99,"children":101},{"className":100},[],[102],{"type":27,"value":103},"offset_x",{"type":27,"value":105},": The horizontal position of the text. Can be 'left', 'center', 'right', or a specific value in pixels. Defaults to 'center'.",{"type":22,"tag":64,"props":107,"children":108},{},[109,115],{"type":22,"tag":35,"props":110,"children":112},{"className":111},[],[113],{"type":27,"value":114},"offset_y",{"type":27,"value":116},": The vertical position of the text. Can be 'top', 'middle', 'bottom', or a specific value in pixels. Defaults to 'center'.",{"type":22,"tag":23,"props":118,"children":119},{},[120],{"type":27,"value":121},"For the offset positions, we accept a value in integer which will be translated to pixels, but you can also pass a unit such as 'px', 'in', 'cm', 'mm', 'pt'.",{"type":22,"tag":23,"props":123,"children":124},{},[125],{"type":27,"value":126},"For example:",{"type":22,"tag":43,"props":128,"children":133},{"className":129,"code":131,"language":132,"meta":7},[130],"language-json","params = {\n    \"source\": \"https://www.example.com\",\n    \"watermark\": {\n        \"text\": \"PROTECTED DOCUMENT\",\n        \"offset_x\": \"5cm\",\n        \"offset_top\": \"15mm\"\n    }\n}\n","json",[134],{"type":22,"tag":35,"props":135,"children":136},{"__ignoreMap":7},[137],{"type":27,"value":131},{"title":7,"searchDepth":139,"depth":139,"links":140},2,[],"markdown","content:guides:go:nethttp:adding-an-image-watermark.md","content","guides/go/nethttp/adding-an-image-watermark.md","md",[147,151,155,159,160,164,168,172,176,180,184,188,192,196,200,204,208,212,216,220,224],{"_path":148,"title":149,"language":10,"library":11,"_id":150},"/guides/go/nethttp/accessing-secured-pages","Accessing secured pages","content:guides:go:nethttp:accessing-secured-pages.md",{"_path":152,"title":153,"language":10,"library":11,"_id":154},"/guides/go/nethttp/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:go:nethttp:adding-a-custom-header-or-footer.md",{"_path":156,"title":157,"language":10,"library":11,"_id":158},"/guides/go/nethttp/adding-a-text-watermark","Adding a text watermark","content:guides:go:nethttp:adding-a-text-watermark.md",{"_path":4,"title":8,"language":10,"library":11,"_id":142},{"_path":161,"title":162,"language":10,"library":11,"_id":163},"/guides/go/nethttp/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:go:nethttp:avoid-conversion-on-error.md",{"_path":165,"title":166,"language":10,"library":11,"_id":167},"/guides/go/nethttp/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:go:nethttp:convert-html-to-pdf-from-a-url.md",{"_path":169,"title":170,"language":10,"library":11,"_id":171},"/guides/go/nethttp/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:go:nethttp:convert-html-to-pdf-from-raw-html.md",{"_path":173,"title":174,"language":10,"library":11,"_id":175},"/guides/go/nethttp/define-a-time-limit","Define a time limit","content:guides:go:nethttp:define-a-time-limit.md",{"_path":177,"title":178,"language":10,"library":11,"_id":179},"/guides/go/nethttp/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:go:nethttp:exporting-only-a-specific-set-of-pages.md",{"_path":181,"title":182,"language":10,"library":11,"_id":183},"/guides/go/nethttp/generate-a-document-with-full-height","Generate a document with full height","content:guides:go:nethttp:generate-a-document-with-full-height.md",{"_path":185,"title":186,"language":10,"library":11,"_id":187},"/guides/go/nethttp/loading-css-from-a-string","Loading CSS from a string","content:guides:go:nethttp:loading-css-from-a-string.md",{"_path":189,"title":190,"language":10,"library":11,"_id":191},"/guides/go/nethttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:go:nethttp:loading-css-from-a-url.md",{"_path":193,"title":194,"language":10,"library":11,"_id":195},"/guides/go/nethttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:go:nethttp:loading-javascript-from-a-string.md",{"_path":197,"title":198,"language":10,"library":11,"_id":199},"/guides/go/nethttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:go:nethttp:loading-javascript-from-a-url.md",{"_path":201,"title":202,"language":10,"library":11,"_id":203},"/guides/go/nethttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:go:nethttp:protecting-the-generated-pdf.md",{"_path":205,"title":206,"language":10,"library":11,"_id":207},"/guides/go/nethttp/receive-a-webhook-event","Receive a webhook event","content:guides:go:nethttp:receive-a-webhook-event.md",{"_path":209,"title":210,"language":10,"library":11,"_id":211},"/guides/go/nethttp/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:go:nethttp:save-your-pdf-online-and-get-back-a-url.md",{"_path":213,"title":214,"language":10,"library":11,"_id":215},"/guides/go/nethttp/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:go:nethttp:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":217,"title":218,"language":10,"library":11,"_id":219},"/guides/go/nethttp/send-custom-http-headers","Send custom HTTP headers","content:guides:go:nethttp:send-custom-http-headers.md",{"_path":221,"title":222,"language":10,"library":11,"_id":223},"/guides/go/nethttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:go:nethttp:using-cookies.md",{"_path":225,"title":226,"language":10,"library":11,"_id":227},"/guides/go/nethttp/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:go:nethttp:waiting-for-a-custom-element-to-be-ready.md",1785426824566]