KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: SVG containing images require visibility to be hidden when opacity is at 0 for printing
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: May 4, 2018

An SVG can contain images with an opacity attribute that is set to "0" by using the SVG_SET_OPACITY command and will look hidden to the form picture object. The example below shows 3 images where 2 of them have an opacity that is set to "0" in which they are not visible:



When the form object is printed, the expected result is to see what is actually displayed on the form but it actually prints all 3 images as shown below:



The SVG that is exported to XML displays the 2 images where the opacity is set to "0":




In order to actually make those images hidden when printing, an attribute of "visibility" needs to be set or created as "hidden". The following utility method will search objects in an SVG picture that contain opacity attribute of "0" and make visibility to "hidden":

// ----------------------------------------------------------------------
// Name: SVG_SET_VISIBILITY_HIDDEN
// Description: Method will add or change visibility of an object in SVG
// Picture to hidden if the Opacity is at 0. It will convert the SVG to
// XML and to do the adjustment and return as a picture so that the
// picture printed will not show hidden objects.
//
// Parameters:
// $1 (PICTURE) - SVG picture to set objects visibility to hidden
//
// Output:
// $0 (PICTURE) - SVG picture modified with objects visibility to hidden
// ----------------------------------------------------------------------
C_PICTURE($1;$inputSVGPic)
C_PICTURE($0)
C_TEXT($xmlText;$svg_id;$str;$loc)
C_LONGINT($i;$pos;$pos2)

If (Count parameters=1)
 $inputSVGPic:=$1

 $svg_id:=SVG_Open_picture ($inputSVGPic)
 DOM EXPORT TO VAR($svg_id;$xmlText)
 SVG_CLEAR ($svg_id)
 $i:=0

 Repeat
  $pos:=Position("opacity=\"0\"";$xmlText;$i)
  $pos2:=Position("/>";$xmlText;$pos+1)

  If ($pos#0)
   $str:=Substring($xmlText;$pos;$pos2-$pos+2)
   $pos:=Position("visibility=";$str)

    If ($pos>0)
     $pos2:=Position("hidden";$str;$pos)

     If ($pos2=0)
      $xmlText:=Replace string($xmlText;"visible";"hidden")
     End if

    Else
      $xmlText:=Insert string($xmlText;" visibility=\"hidden\"";$pos2)
    End if

    $i:=$pos2+2
   Else
    $pos2:=0 // exit loop
   End if

 Until (($pos=0) & ($pos2=0))

 $loc:=Get 4D folder(Database folder)+"xmlText2SVG.xml"

 If (Test path name($loc)#Is a document)
  Create document($loc)
 End if

 TEXT TO DOCUMENT($loc;$xmlText)
 $svg_id:=SVG_Open_file ($loc)
 $0:=SVG_Export_to_picture ($svg_id;0)
 SVG_CLEAR ($svg_id)
End if


Here is an example of using the method:

C_PICTURE($svgPicture)

$svgPicture:=[Table_1]svgPicture // Assume a record containg an SVG picture

$svgPicture:=SVG_SET_VISIBILITY_HIDDEN($svgPicture)