function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohammed AzarudeenMohammed Azarudeen 

Paragraph Sections and Sub Sections Numbering in Visualforce PDF

Am rendering vfp as PDF. I want to show sections and subsections Numbering dynamically to the paragraph for example
1. First Section
   1.1 First Subsection for first section
       1.1.1 child for first subsection
       1.1.2 child for first subsection
   1.2 Second Subsection for first section
       1.2.1 child for first subsection
       1.2.2 child for first subsection
2. Second Section
   2.1 First subsection for second section
       2.1.1 child for first subsection
   2.2 second subsection for second section
       2.2.1 child for second subsection

While typing paragraph in field i'll mention h1# or h2# or h3# before starting paragraph, the meaning of that is,
      h1# ----> first section
      h2# ----> Sub section of first section
      h3# ----> child for sub section
so am typing like,

       h1# This is First Section
           h2# This is Sub section for first section
                h3# This is child for sub section
       h1# This is Second section
            h2# This is Sub section for Second section
                h3# This is child for sub section.

I tried with below extension,
 
public class Apex_Class_Name {
    public string str{get;set;}
//  public integer temp=0;
    public integer i,j,k;
    public integer h1Hash = 0, h2Hash = 0, h3Hash = 0;
    public string Level1, Level2, Level3;
    public Patient__c sal;
    public Apex_Class_Name(ApexPages.StandardController controller){
        sal = [SELECT Paragraph__c from Patient__c where id=:Apexpages.currentpage().getParameters().get('id') limit 1];
        str = sal.Paragraph__c;
        Pattern p1 = Pattern.compile('h1#');
        Pattern p2 = Pattern.compile('h2#');
        Pattern p3 = Pattern.compile('h3#');
        Matcher m1 = p1.matcher(str);
        Matcher m2 = p2.matcher(str);
        Matcher m3 = p3.matcher(str);
        while (m1.find()) {
            h1Hash++;
        }
        while (m2.find()) {
            h2Hash++;
        }
        while (m3.find()) {
            h3Hash++;
        }
        system.debug('==============>'+h1Hash);
        system.debug('==============>'+h2Hash);
        system.debug('==============>'+h3Hash);


 CallMethod(str);
    }
    public string CallMethod(string substr){
        if(h1Hash != 0){
            for(i = 1; i <= h1Hash; i++){
                Level1 = string.valueOf(i);
                str = str.replaceFirst('h1#',Level1);
                for(j = 1; j <= h2Hash; j++){
                    Level2 = Level1+'.'+string.valueOf(j);
                    str = str.replaceFirst('h2#',Level2);
                    for(k = 1; k <= h3Hash; k++){
                        Level3 = Level2+'.'+string.valueOf(k);
                        str = str.replaceFirst('h3#',Level3);
                    }
                }
            }
        }
        return str;
    }
}

My present output is,
1. First Section
   1.1 Sub section one
        1.1.1 child for sub section
        1.1.2 child for sub section
   1.2 Sub section two 
         1.1.3 child for sub section **(coming 1.1.3 instead of 1.2.1)**
         1.1.4 child for sub section **(coming 1.1.4 instead of 1.2.2)**
 2. Second Section
    1.3 Sub section one **(coming 1.3 instead of 2.1)**
        1.1.5 child for sub section **(coming 1.1.5 instead of 1.3.1)**
    1.4 Sub section two **(coming 1.4 instead of 2.2)**
        1.1.6 child for sub section **(coming 1.1.6 instead of 1.4.1)**
Please see the errors above, I mentioned in brackets.

What is the error I did?

How can I overcome this?

Any help is appreciable!! Thanks.

 
Best Answer chosen by Mohammed Azarudeen
AnjithKumarAnjithKumar
Mohammed, 

There is wrong login in CallMethod(), First time only it will replace all h2#, h3# tags because h2Hash, h3Hash values are 4 and 6.

So it will replace all at first loop only. I have updated the method.
 
public string CallMethod(string substr){
        if(h1Hash != 0){
            
            for(i = 1; i <= h1Hash; i++){
                Level1 = string.valueOf(i);
                str = str.replaceFirst('h1#',Level1);
                String strb1 = str.split('h1#')[0];
                Integer substringNos1 = strb1.split('h2#').size();
                for(j = 1; j <substringNos1; j++){
                    Level2 = Level1+'.'+string.valueOf(j);
                    str = str.replaceFirst('h2#',Level2);
                    String strb = str.split('h2#')[0];
                    Integer substringNos = strb.split('h3#').size();
                    for(k = 1; k < substringNos; k++){
                        Level3 = Level2+'.'+string.valueOf(k);
                        str = str.replaceFirst('h3#',Level3);
                    }
                }
            }
        }
        return str;
    }

Hope it helps you, let me know if it helps you.

Thanks,
Anjith kumar