Watermark text on multitiff image

Example requests & Code samples for GdPicture Toolkits.
Post Reply
coloboxp
Posts: 6
Joined: Fri Jul 29, 2016 12:12 pm

Watermark text on multitiff image

Post by coloboxp » Tue Jul 03, 2018 12:45 pm

Hi

I think I'm close but still I'm having troubles, couldn't figure it out already two days.

I'm taking a multitiff "A", and want to add a border in the top and watermark it with text, only on the first page, the rest should stay as is.

I did some tests and it works with single paged tiffs, or a multitiff is saved as single paged.

Please could someone help me out pointing where I am doing the mistake?, this is like the 20th iteration code:

The function takes the path to the tiff (with extension .TF, that's why I change the extension to .TIF later), and a string with the text to embed on the first page.

Code: Select all

       
       public void EmbedIdentificationToTiffHeader(string pathToTiff, string code)
        {
            using (GdPictureImaging image = new GdPictureImaging())
            {
                int imgIdSrc = image.CreateGdPictureImageFromFile(pathToTiff);
                int imgIdTgt = image.TiffCreateMultiPageFromGdPictureImage(imgIdSrc);

                int pages = image.TiffGetPageCount(imgIdSrc);

                for (int pageId = 1; pageId <= pages; pageId++)
                {
                    image.TiffSelectPage(imgIdSrc, pageId);
                    
                    if (image.GetStat() == GdPictureStatus.OK)
                    {
                        //if it is the first page
                        
                        if (pageId == 1)
                        {
                            image.TiffAddToMultiPageFile(imgIdTgt, imgIdSrc);

                            image.AddBorderTop(imgIdTgt, 50, Color.White);
                            image.DrawText(imgIdTgt, code, 20, 1, 16, FontStyle.FontStyleRegular, Color.Black, "Arial", false);

                            image.TiffSaveAsMultiPageFile(imgIdTgt, Path.ChangeExtension(pathToTiff, "TIF"), TiffCompression.TiffCompressionCCITT4);
                        }
                        else
                        {
                            image.TiffAddToMultiPageFile(imgIdTgt, imgIdSrc);
                        }
                    }
                    else
                    {
                        Logger.Write("Error: " + image.GetStat());
                        break;
                    }
                }

                image.ReleaseGdPictureImage(imgIdSrc);
                image.ReleaseGdPictureImage(imgIdTgt);

                image.Dispose();
            }
        }
Thank you in advance for your help.

coloboxp
Posts: 6
Joined: Fri Jul 29, 2016 12:12 pm

Re: Watermark text on multitiff image

Post by coloboxp » Tue Jul 03, 2018 1:34 pm

I'll answer myself, and leave the solution in case someone else needs it.

While still looking for ideas, I've landed on this page (Draw page number on each page of a multipage tiff imagehttps://guides.gdpicture.com/content/Gd ... Image.html), translating the code from VB into C#, and adapting it into my needs worked perfectly.
  • *The parameter pathToTiff has the full path to a multi tiff file, with extension TF.
    *The parameter code, has some text to watermark and embed a barcode 50 pixels to the right of the text, on the image.
The output is the TF file as TIF, with the watermarked text and barcode on top of it.

Code: Select all

public void EmbedIdentificationToTiffHeader(string pathToTiff, string code)
{
            
    using (GdPictureImaging gdp = new GdPictureImaging())
    {
        gdp.TiffOpenMultiPageForWrite(true);
        int imgSrcId = gdp.TiffCreateMultiPageFromFile(pathToTiff);
            
        int pages = gdp.TiffGetPageCount(imgSrcId);
                
        for (int pageId = 1; pageId <= pages; pageId++)
        {
            gdp.TiffSelectPage(imgSrcId, pageId);

            if (pageId == 1)
            {
                gdp.AddBorderTop(imgSrcId, 50, Color.White);
                var bcLen = gdp.GetTextWidth(imgSrcId, code, "Arial", 16, FontStyle.FontStyleRegular);
                gdp.DrawText(imgSrcId, code, 20, 1, 16, FontStyle.FontStyleRegular, Color.Black, "Arial", true);
                gdp.Barcode1DWrite(imgSrcId, Barcode1DWriterType.Barcode1DWriterCode93, code, (int)bcLen + 50, 5, 400, 40, Color.Black, BarcodeAlign.BarcodeAlignCenter, 0);
            }

            gdp.ConvertTo1Bpp(imgSrcId);
        }

        gdp.TiffSaveMultiPageToFile(imgSrcId, Path.ChangeExtension(pathToTiff, "TIF"), TiffCompression.TiffCompressionCCITT4);
        gdp.ReleaseGdPictureImage(imgSrcId);
    }
}
Cheers

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests