We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add Transparent Text in PDF using C# | Aspose.PDF for .NET

Aspose. Pdf, Text TextFragment set the foreground color, background color is not transparent.

   textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.Black;
   //Don't work
   textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Transparent;

@Diego2020

Please try assigning the transparent color as below and let us know in case you still notice any issue:

int alpha = 10;
int green = 0;
int red = 100;
int blue = 0;
Color alphaColor = Color.FromArgb(alpha, red, green, blue);

var textFragment = new TextFragment("Transparent Text");
textFragment.TextState.ForegroundColor = alphaColor;

@asad.ali

Hi,

We are having the same issue and we tried the suggested code above to set ARGB, but issue still exists, alpha is not working.

Please advise.

Regards,
Regie

@vitro

Can you please share the source file, generated file and used sample code that we may try on our end. Please also share that which product version you have used on your end.

@mudassir.fayyaz

We tried the suggested code snippet above and also using Aspose.PDF 20.6.0. Attached is the sample result. sample.pdf (20.7 KB)

int alpha = 10;
int green = 0;
int red = 255;
int blue = 0;
Color alphaColor = Color.FromArgb(alpha, red, green, blue);

var textFragment = new TextFragment("Transparent Text");
textFragment.TextState.ForegroundColor = alphaColor;

Regards

@vitro

I have used following sample code on my end using Aspose.PDF for .NET 21.3 and able to obtain the transparent text. For your kind reference, I have attached the generated PDF as well.

    public static void TestFragment()
    {
        Document document = new DocumentFactory().CreateDocument();
        Page page = document.Pages.Add();
        page.SetPageSize(597.6, 842.4);

        FloatingBox topFloatingBox = new FloatingBox();
        topFloatingBox.Top=85;

        int alpha = 10;
        int green = 0;
        int red = 255;
        int blue = 0;
        Color alphaColor = Color.FromArgb(alpha, red, green, blue);

        var textFragment = new TextFragment("Transparent Text");
        textFragment.TextState.ForegroundColor = alphaColor;

        topFloatingBox.Paragraphs.Add(textFragment);
        page.Paragraphs.Add(topFloatingBox);

        document.Save("result1.pdf");

    } 

result1.pdf (2.2 KB)

Hi @mudassir.fayyaz

I can’t access the result file, although I was able to try your code and transparent works fine, but we are not using FloatingBox for adding text fragment. Can you please try the complete sample code below?

   private static void AddTransparentText(string dataDir)
    {
    var document = new Document();
    var page = document.Pages.Add();
    page.PageInfo.Margin.Bottom = 0;
    page.PageInfo.Margin.Top = 0;
    page.PageInfo.Margin.Left = 0;
    page.PageInfo.Margin.Right = 0;

    var b = new System.Drawing.Bitmap(dataDir + "Sample.jpg");
    page.SetPageSize(Aspose.Words.ConvertUtil.PixelToPoint(b.Width), Aspose.Words.ConvertUtil.PixelToPoint(b.Height));
    page.AddImage(dataDir + "Sample.jpg", new Aspose.Pdf.Rectangle(0, 0, Aspose.Words.ConvertUtil.PixelToPoint(b.Width), Aspose.Words.ConvertUtil.PixelToPoint(b.Height)));

    // Create TextBuilder object
    TextBuilder textBuilder = new TextBuilder(page);
    var list = new System.Collections.Generic.List<Field>();

    var field1 = new Field
    {
    Text = "Transparent Text",
    LocationX = 148,
    LocationY = 287,
    ActualFontSizeF = 11,
    ActualHeight = 215,
    ActualWidth = 436
    };
    list.Add(field1);

    foreach (Field field in list)
    {
    // Control font size is in pixel. Multiplier is an estimate to convert pixel to float.
    var fontSize = (float)(field.ActualFontSizeF * 0.755);

    var adjustment = 0;
    var pageHeight = Aspose.Words.ConvertUtil.PixelToPoint(b.Height - adjustment, b.VerticalResolution);

    var llx = Aspose.Words.ConvertUtil.PixelToPoint(field.LocationX + adjustment, b.HorizontalResolution);
    var lly = (pageHeight - Aspose.Words.ConvertUtil.PixelToPoint(field.LocationY + field.ActualHeight, b.VerticalResolution));
    var urx = Aspose.Words.ConvertUtil.PixelToPoint(field.LocationX + field.ActualWidth - adjustment, b.HorizontalResolution);
    var ury = (pageHeight - Aspose.Words.ConvertUtil.PixelToPoint(field.LocationY, b.VerticalResolution));

    var ctrlHeight = Aspose.Words.ConvertUtil.PixelToPoint(field.ActualHeight, b.HorizontalResolution);
    var ctrlWidth = Aspose.Words.ConvertUtil.PixelToPoint(field.ActualWidth - adjustment, b.HorizontalResolution);

    // create text paragraph
    TextParagraph paragraph = new TextParagraph();
    paragraph.VerticalAlignment = VerticalAlignment.Center;
    paragraph.Margin.Left = 2;
    paragraph.Rectangle = new Rectangle(llx, lly, llx + ctrlWidth, lly + ctrlHeight);

    // Create text fragment
    var textFragment = new TextFragment(field.Text);

    var alpha = 10;
    var green = 0;
    var red = 255;
    var blue = 0;
    Color alphaColor = Color.FromArgb(alpha, red, green, blue);
    textFragment.TextState.ForegroundColor = alphaColor;

    paragraph.AppendLine(textFragment);
    textBuilder.AppendParagraph(paragraph);
    }

    document.Save(dataDir + "Sample_out.pdf");
    }

     class Field
        {
            public int LocationX;
            public int LocationY;
            public int ActualWidth;
            public int ActualHeight;
            public float ActualFontSizeF;
            public string Text;
        }

Sample.jpg (8.9 KB)

@vitro

I have used the sample code and it seems to be an issue that we need to further investigate. A ticket with ID PDFNET-49739 has been created to further investigate this on our end. We will share the feedback with you as soon as it will be fixed.

Hi guys, it’s been 7 months since last correspondent and was wondering what’s the status of this issue at the moment? I have upgraded to the latest version but seems like this is still occurring.

@vitro

The concerned issue is still unresolved. We request for your patience and will notify you once issue will be addressed.

We have tested this Color Alpha on ASPOSE.PDF .NET and found:

On Text Fragments in a Paragraph:
textFragment.TextState.ForegroundColor = asposeColor; works great.
textFragment.TextState.BackgroundColor = asposeColor; alpha is ignored

      var page = document.Pages.Add();
                page.Background = Aspose.Pdf.Color.Yellow;
                for (int p = 1; p < 17; p++) // for page testing... can set to greater than 2
                {
                    var alpha = (p - 1) * 16;
                    Color color = Color.FromArgb(alpha, 0, 0, 255);
                    var asposeColor = color.ToColor();
                    var textFragment = new TextFragment($"This test background is Blue at {color.A / 255m}:{asposeColor.A} alpha");
                    Assert.That(color.A, Is.EqualTo(alpha));
                    textFragment.TextState.BackgroundColor = asposeColor;
                    page.Paragraphs.Add(textFragment);
                }

It would be mission critical for the Background to honor the alpha… :slight_smile:

@johne42

Can you please share which version of the API are you using? The method color.ToColor(); was not recognized while testing the scenario with Aspose.PDF for .NET 22.1.

@asad.ali using the latest… it looks like it depends on the combination of the colors that you use for the Foreground and Background… I think the background color is always using the alpha of the Foreground color even if they are different.

@johne42

Thanks for writing back.

It looks like the textFragment.TextState.ForegroundColor and textFragment.TextState.BackgroundColor expects the Aspose.Pdf.Color Class Type. Whereas, in your code snippet, you are assigning it to System.Drawing.Color Class Type like below:

Color color = Color.FromArgb(alpha, 0, 0, 255);
var asposeColor = color.ToColor(); // <= This is System.Drawing.Color
var textFragment = new TextFragment($"This test background is Blue at {color.A / 255m}:{asposeColor.A} alpha");
Assert.That(color.A, Is.EqualTo(alpha));
textFragment.TextState.BackgroundColor = asposeColor; //  It expects Aspose.Pdf.Color

Could you please explain how did you manage to run the code snippet? Maybe we are missing something while testing the scenario and we could not get this code snippet compiled without any errors. We will again try to test the code after getting your feedback and proceed further accordingly.

@asad.ali
I have three Color extensions that I use:

Color = Aspose pdf Color

 public static System.Drawing.Color ToArgb(this Color value)
    {
        var toRbg = value.ToRgb();
        return System.Drawing.Color.FromArgb(value.A255(),toRbg.R, toRbg.G, toRbg.B);
    }
    public static Color ToColor(this System.Drawing.Color value)
    {
        return Color.FromArgb(value.A, value.R, value.G, value.B);
    }
    public static byte A255(this Color value)
    {
        return Convert.ToByte(value.A * byte.MaxValue);
    }

@johne42

We were still unable to successfully compile the code as below line gave an error:

return System.Drawing.Color.FromArgb(value.A255(),toRbg.R, toRbg.G, toRbg.B);

We request you please share a sample console application that can be run to reproduce the scenario so that we can further proceed accordingly.

Hi guys, it has been more than a year since this issue was first raised. Any progress on this bug on your end or if there’s still none, is there a way we can have this prioritised?

@vitro

We are afraid that the earlier logged ticket could not get resolved due to other issues in the queue logged prior to it. However, we have recorded your concerns and will surely update you once we have definite update regarding its fix. Furthermore, you can prioritize the issue in case you have paid support subscription.

for Aspose.PDF.Color alpha ranges are 0 to 1 and for System.Drawing.Color, values are 0 to 255?

if the above is true… how would the FromArgb fail… I will add a tests around this call for error free execution and get back to you

@johne42

Sure, please take your time to prepare a sample console application which we can use to replicate the issue in our environment. We will then address it accordingly.