Watermark Entire TIFF Image File without Looping through Pages using C# .NET

Current sample ASPOSE code block to apply watermark on TIF is running with loop page by page, which is time consuming and may yield to performance.

Plz suggest, if you have any .Net watermark functions which can apply on entire document, rather than looping through the page.


This Topic is created by awais.hafeez using Email to Topic tool.

@subramanyam.gvsrb

You can consider the option of using batch processing for increasing the performance as well as avoid getting into loops. Please visit this documentation link for batch processing where you can embed your logic for adding watermark per page.

Our previous vendor use to support at Document level and response is very quick though pdf file has 500 pages or more.
But our current vendor is not supporting this and we are running out of memory performance issues. This is one of the reason for us to look for a new vendor.

Can you plz provide full code block including releasing memory to apply watermark diagonally on pdf and tif on entire document? We will do a load test.

@subramanyam.gvsrb

Can you please try using following code on your end.

     public static void TestImageWatermark()
    {

        //Declare a String object with Watermark Text
        String theString = "45 Degree Rotated Text";

        //The possibility of batch conversion before saving (exporting) Tiff images is implemented.

        using (TiffImage tiffImage = (TiffImage)Image.Load("Test.tif"))
        {
            // Set batch operation for pages
            //   tiffImage.PageExportingAction = delegate (int index, Image page)
            tiffImage.PageExportingAction = delegate (int index, Image page)
            {
            // Fires garbage collection to avoid unnecessary garbage storage from previous pages
            GC.Collect();

            //Create and initialize an instance of Graphics class
            Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(((RasterImage)page));

            //Initialize an object of SizeF to store image Size
            Aspose.Imaging.SizeF sz = graphics.Image.Size;


            //Creates an instance of Font, initialize it with Font Face, Size and Style
            Aspose.Imaging.Font font = new Aspose.Imaging.Font("Times New Roman", 14, Aspose.Imaging.FontStyle.Bold);

            //Create an instance of SolidBrush and set its various properties
            Aspose.Imaging.Brushes.SolidBrush brush = new Aspose.Imaging.Brushes.SolidBrush();
                brush.Color = Aspose.Imaging.Color.White;
            //Set Opacity of Brush object between 0 to 1
            //0 means fully visible, 1 means invisible
            brush.Opacity = 0;

            //Initialize an object of StringFormat class and set its various properties
            Aspose.Imaging.StringFormat format = new Aspose.Imaging.StringFormat();
                format.Alignment = Aspose.Imaging.StringAlignment.Center;
                format.FormatFlags = Aspose.Imaging.StringFormatFlags.MeasureTrailingSpaces;

            //Create an object of Matrix class for transformation
            Aspose.Imaging.Matrix matrix = new Aspose.Imaging.Matrix();
            //First a translation
            matrix.Translate(sz.Width / 2, sz.Height / 2);
            //Then a rotation
            matrix.Rotate(-45.0f);

            //Set the Transformation through Matrix
            graphics.Transform = matrix;

            //Draw the string on Image
            graphics.DrawString(theString, font, brush, 0, 0, format);

            };

            tiffImage.Save("rotated.tif"); /* or export through tiffImage.Save("rotated.tif", new TiffOptions(TIFF_EXPECTED_FORMAT))*/

            /* Attention! In batch mode all pages will be released in this line!
                If you want to further perform operations on the original image, you should reload it from the source to another instance. */
        }
    }