skip to Main Content

I tried IronPdf’s PdfDocument.ApplyWatermark(watermarkHtml), but it din’t work, no watermark is appied. You guys know any other library or workarounds? I’m using Dotnet 6 and IronPdf 2023.5.8

    IronPdf.Logging.Logger.EnableDebugging = true;
    IronPdf.Logging.Logger.LogFilePath = "Default.log"; //May be set to a directory name or full file
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
    Installation.LinuxAndDockerDependenciesAutoConfig = false;
    Installation.Initialize();

    HttpResponseMessage stream = await this.httpClient.GetAsync("https://test2134.blob.core.windows.net/test34343/Form 343 - Blank.pdf");
    PdfDocument pdf = new PdfDocument(stream.Content.ReadAsStream());
    string html = "<h1> Example Title <h1/>";
    int rotation = 0;
    int watermarkOpacity = 30;
    pdf.ApplyWatermark(html, rotation, watermarkOpacity); Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
    pdf.SaveAs(Path.Combine(Directory.GetCurrentDirectory(), "test.pdf"));

2

Answers


  1. u can use iTextSharp package from NuGet, then

    using System;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    class Program
    {
        static void Main()
        {
            string inputFile = "path/to/input.pdf";
            string outputFile = "path/to/output.pdf";
            string watermarkText = "Confidential";
    
            AddWatermark(inputFile, outputFile, watermarkText);
    
            Console.WriteLine("Watermark added successfully!");
        }
    
        static void AddWatermark(string inputFile, string outputFile, string watermarkText)
        {
            using (PdfReader reader = new PdfReader(inputFile))
            using (FileStream stream = new FileStream(outputFile, FileMode.Create))
            using (PdfStamper stamper = new PdfStamper(reader, stream))
            {
                int pageCount = reader.NumberOfPages;
    
                // Create a BaseFont for the watermark text
                BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    
                // Define the watermark appearance
                PdfGState gState = new PdfGState();
                gState.FillOpacity = 0.3f; // Adjust opacity as needed
    
                // Loop through each page and add the watermark
                for (int i = 1; i <= pageCount; i++)
                {
                    PdfContentByte canvas = stamper.GetOverContent(i);
    
                    // Save the graphics state
                    canvas.SaveState();
    
                    // Set the watermark appearance
                    canvas.SetGState(gState);
                    canvas.BeginText();
    
                    // Set the font and size of the watermark text
                    canvas.SetFontAndSize(baseFont, 48);
    
                    // Set the position and rotation of the watermark text
                    canvas.SetTextMatrix(30, 30);
                    canvas.ShowTextAligned(Element.ALIGN_LEFT, watermarkText, 0, 0, 45);
    
                    // End the text and restore the graphics state
                    canvas.EndText();
                    canvas.RestoreState();
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. You can try CoherentPDF from here:

    https://www.nuget.org/packages/CoherentPDF/

    The easy way to try first, would be to use the command line tools from https://community.coherentpdf.com/ and write cpdf -stamp-on stamp.pdf in.pdf -o out.pdf. If that works as you expect, the .NET tools will too.

    If you have example files, it will help us suggest why the watermarking is not working as you expect with IronPDF.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search