PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDFSharp Image Transparency
https://forum.pdfsharp.net/viewtopic.php?f=4&t=886
Page 1 of 1

Author:  mgillesp [ Fri Sep 25, 2009 2:03 pm ]
Post subject:  PDFSharp Image Transparency

One feature that would be nice would be a property on the XImage to set the transparency of the image.

In order to accomplish this, I added an alpha property to the XImage, then I changed the DrawImage in XGraphicsPdfRenderer. It works, but I am fairly new to the PDF spec, so I was wondering if it is a standard way to apply the transparency.

Thanks

public void DrawImage(XImage image, double x, double y, double width, double height)
{
string transparencyGraphicsState = "";

if (image.Alpha < 1.0F)
{
PdfExtGState extGState = this.Owner.ExtGStateTable.GetExtGStateNonStroke(image.Alpha);
string gs = this.Resources.AddExtGState(extGState);
transparencyGraphicsState = String.Format("{0} gs\n", gs);

// Must create transparany group
if (this.page != null)
this.page.transparencyUsed = true;
}

string name = Realize(image);
if (!(image is XForm))
{
if (this.gfx.PageDirection == XPageDirection.Downwards)
{
AppendFormat("{6}q {2:0.####} 0 0 -{3:0.####} {0:0.####} {4:0.####} cm {5} Do Q\n",
x, y, width, height, y + height, name, transparencyGraphicsState);
}
else
{
AppendFormat("{5}q {2:0.####} 0 0 {3:0.####} {0:0.####} {1:0.####} cm {4} Do Q\n",
x, y, width, height, name, transparencyGraphicsState);
}
}
else
{
BeginPage();

XForm form = (XForm)image;
form.Finish();

PdfFormXObject pdfForm = Owner.FormTable.GetForm(form);

double cx = width / image.PointWidth;
double cy = height / image.PointHeight;

if (cx != 0 && cy != 0)
{
if (this.gfx.PageDirection == XPageDirection.Downwards)
{
AppendFormat("{6}q {2:0.####} 0 0 -{3:0.####} {0:0.####} {4:0.####} cm 100 Tz {5} Do Q\n",
x, y, cx, cy, y + height, name, transparencyGraphicsState);
}
else
{
AppendFormat("{5}q {2:0.####} 0 0 {3:0.####} {0:0.####} {1:0.####} cm {4} Do Q\n",
x, y, cx, cy, name, transparencyGraphicsState);
}
}
}
}

Author:  Soldier-B [ Fri Nov 20, 2009 4:28 pm ]
Post subject:  Re: PDFSharp Image Transparency

I personally use the .NET ColorMatrix class to apply transparency to an image before I render that image to the pdf.

Link to the MSDN article: http://msdn.microsoft.com/en-us/library ... atrix.aspx

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/