How do I detect if a PDF page contais transparency?

513 views
Skip to first unread message

Support

unread,
Feb 29, 2012, 5:29:36 PM2/29/12
to pdfne...@googlegroups.com
Q:
 
How do I detect if a PDF page contais transparency using PDFNet?
 
-------------------
A:

 

You can detect if a PDF page has transparency using ElementReader (as shown in ElementReader  rample - http://www.pdftron.com/pdfnet/samplecode.html#ElementReader).

 

To detect if there is transparency on a page you would need to check if there is any

 

a)      Transparency groups

b)      Blend modes

c)       Opacity values smaller than 1

 

For this you can extend ProcessElements() along the following lines: 


// Pseudo-code to check PDF page for transparency…

 

static bool CheckIfPageHasTransparency(ElementReader reader)

{

      Element element;

      while ((element = reader.Next()) != null)       // Read page contents

      {

            if (ProcessGSChanges(element)) return true;

 

            if (element.GetType() == Element.Type.e_form)

            {

                  reader.FormBegin();

 

                  Obj grp = element.GetXObject().FindObj("Group");

                  if (grp != null) {

                        Obj grp_subtype = grp.FindObj("S");

                        if(grp_subtype!=null && grp_subtype.IsName() && grp_subtype.GetName() == "Transparency"))

                              return true;

                  }

 

                  ProcessElements(reader);

                  reader.End();

                  break;

            }

      }

}

 

static bool ProcessGSChanges(Element element)

{

      GState gs=element.GetGState();

      for(GSChangesIterator itr=m_reader.GetChangesIterator(); itr.HasNext(); itr.Next())

      {

            switch(itr.Current()) {

            case GState.GStateAttribute.e_opacity_fill :

                  if (gs.GetFillOpacity()<1) return true;

                  break;

            case GState.GStateAttribute.e_opacity_stroke :

                  if (gs.GetStrokeOpacity()<1) return true;

                  break;

            case GState.GStateAttribute.e_soft_mask :

                  if(gs.GetSoftMask() != null) return true;

                  break;

            case GState.GStateAttribute.e_blend_mode:

                  if(gs.GetBlendMode() != GState::e_bl_normal)

                        return true;                 

                  break;

            default:

                  break;

            }

      }

 

      return false;

}

Reply all
Reply to author
Forward
0 new messages