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;
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.
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;
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");
}
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;
}
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.
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…
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.
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.
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?
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.
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.