PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Aug 17, 2018 10:46 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri Sep 25, 2009 2:03 pm 
Offline

Joined: Fri Sep 25, 2009 1:57 pm
Posts: 1
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);
}
}
}
}


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 20, 2009 4:28 pm 
Offline
User avatar

Joined: Tue Oct 14, 2008 6:15 pm
Posts: 32
Location: USA
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group