How to set the transparency solely for the watermark?

Discussions about PDF management.
Post Reply
ddtna21
Posts: 2
Joined: Wed Dec 13, 2023 12:39 pm

How to set the transparency solely for the watermark?

Post by ddtna21 » Wed Dec 13, 2023 12:44 pm

Hello, I am trying to adding watermark into document with this code. Now i am having a problem. If i put the transparency to 0, the watermark is on top of the document, but if i put the transparency to -128 like in this code, its affecting the transparency of the entire document. How can i fix the code to only apply to the watermark? Thank you

Code: Select all

public ApiResponseModel<InsertWatermarkModel> InsertWatermark(string inputPdf, string watermark, Guid guid)
{
    try
    {
        using (GdPicturePDF pdfGdPicture = new GdPicturePDF())
        {
            byte[] pdfBytes = Convert.FromBase64String(inputPdf);
            byte[] watermarkBytes = Convert.FromBase64String(watermark);
             
            using (MemoryStream inputMemoryStream = new MemoryStream(pdfBytes))
            {
                pdfGdPicture.LoadFromStream(inputMemoryStream, false);

                using (GdPictureImaging imagingGdPicture = new GdPictureImaging())
                {
                    int documentId = imagingGdPicture.CreateGdPictureImageFromByteArray(pdfBytes);
                    int watermarkId = imagingGdPicture.CreateGdPictureImageFromByteArray(watermarkBytes);

                    imagingGdPicture.SetTransparency(watermarkId, -128);
                    
                    float pageWidth = pdfGdPicture.GetPageWidth();
                    float pageHeight = pdfGdPicture.GetPageHeight();

                    int watermarkWidth = imagingGdPicture.GetWidth(watermarkId);
                    int watermarkHeight = imagingGdPicture.GetHeight(watermarkId);

                    float scaleFactorWidth = pageWidth / watermarkWidth;
                    float scaleFactorHeight = pageHeight / watermarkHeight;

                    // Use the smaller scaling factor to fit the entire watermark inside the page
                    float scaleFactor = Math.Min(scaleFactorWidth, scaleFactorHeight);

                    // Calculate new dimensions for the watermark
                    int newWidth = (int)(watermarkWidth * scaleFactor);
                    int newHeight = (int)(watermarkHeight * scaleFactor);

                    float xPos = (pageWidth - newWidth) / 2;
                    float yPos = (pageHeight - newHeight) / 2;

                    for (int i = 1; i <= pdfGdPicture.GetPageCount(); i++)
                    {
                        pdfGdPicture.SelectPage(i);
                        string imageRes = pdfGdPicture.AddImageFromGdPictureImage(watermarkId, false, false);
                        pdfGdPicture.DrawImage(imageRes, xPos, yPos, newWidth, newHeight);
                    }

                    using (MemoryStream outputMemoryStream = new MemoryStream())
                    {
                        pdfGdPicture.SaveToStream(outputMemoryStream);
                        byte[] outputPdfData = outputMemoryStream.ToArray();

                        imagingGdPicture.ReleaseGdPictureImage(watermarkId);

                        return this.CreateApiResponse(new InsertWatermarkModel(Convert.ToBase64String(outputPdfData)), 0, null);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        return this.CreateApiResponse(new InsertWatermarkModel(), 500, ex.Message);
    }
}

Hugo
Posts: 227
Joined: Tue Dec 18, 2018 10:09 am

Re: How to set the transparency solely for the watermark?

Post by Hugo » Thu Dec 14, 2023 2:20 pm

Hi there,

Thanks for your feedback.

Your issue is curious as from the code you have submitted this should not be the case.
I would however like to check a few things before we dive into this specificly:

Can you confirm this issue occurs in V14.2.53 (our latest version at the time of this message)

I suppose you have taken example from this section here and adjusted in to handle PDF files? https://www.gdpicture.com/guides/gdpict ... ments.html

Regards,
Hugo Cudd
Technical Support

ddtna21
Posts: 2
Joined: Wed Dec 13, 2023 12:39 pm

Re: How to set the transparency solely for the watermark?

Post by ddtna21 » Tue Dec 19, 2023 11:03 am

Hello,

Thank you for your response but i figured it out I just need to add one more line of code.
I'll leave it here in case anybody need it in the future.

Code: Select all

int watermarkId = imagingGdPicture.CreateGdPictureImageFromByteArray(watermarkBytes);
imagingGdPicture.SetTransparency(watermarkId, -128);
imagingGdPicture.SetTransparencyColor(watermarkId, Color.White);

Hugo
Posts: 227
Joined: Tue Dec 18, 2018 10:09 am

Re: How to set the transparency solely for the watermark?

Post by Hugo » Wed Dec 20, 2023 11:55 am

Hi,

Thanks for letting me know your issue is solved.

If you need us to look into any potential issue do not hesitate to contact us once more.

Have a great day.

Regards,
Hugo Cudd
Technical Support

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests