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.