How to Set a Full-Page Background Image with Specified Opacity in QuestPDF? #698
Unanswered
ReshmaSpeeHive
asked this question in
Q&A
Replies: 1 comment
-
You can do this with what @MarcinZiabek did in #569, which I also used in my PDFs. Basically, you have to use SkiaSharp: Image LoadImageWithTransparency(string fileName, float transparency)
{
using var originalImage = SKImage.FromEncodedData(fileName);
using var surface = SKSurface.Create(originalImage.Width, originalImage.Height, SKColorType.Rgba8888, SKAlphaType.Premul);
using var canvas = surface.Canvas;
using var transparencyPaint = new SKPaint
{
ColorFilter = SKColorFilter.CreateBlendMode(SKColors.White.WithAlpha((byte)(transparency * 255)), SKBlendMode.DstIn)
};
canvas.DrawImage(originalImage, new SKPoint(0, 0), transparencyPaint);
var encodedImage = surface.Snapshot().Encode(SKEncodedImageFormat.Png, 100).ToArray();
return Image.FromBinaryData(encodedImage);
} Use it in the foreground page layer: var document = Document.Create(container =>
{
container.Page(page =>
{
page.Content()
.AlignCenter()
.AlignMiddle()
.Text(paragraph =>
{
paragraph.Span("QuestPDF").Bold().FontSize(50);
paragraph.Span("\nQuestPDF is an open-source .NET library for PDF generation. Utilizing several new patterns and practices, QuestPDF seeks to resolve the challenges inherent in this process.");
});
// Watermark Image with Transparency
var image = LoadImageWithTransparency("questpdf.png", 0.25f);
page.Foreground().Image(image);
});
}); Result: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm currently working on a project where I need to set a full-page background image with a specified level of opacity using QuestPDF, and I'm looking for guidance on how to achieve this.
My goal is to create a PDF document where a background image spans the entire page, similar to a watermark, but with a transparency or opacity setting. I want to ensure that this background image covers the entire page, including any content I add on top of it, while allowing the content to be visible through the background image with the specified opacity.
Beta Was this translation helpful? Give feedback.
All reactions