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
KSusan CoxKSusan Cox 

how can I read the content of doc, .docx, .xls, .pdf of uploaded file in apex.

Hi,

Can you plaese give me suggestion how can I read the content of  doc, .docx, .xls, .pdf of uploaded file in apex.

Thanks.
Ramesh KallooriRamesh Kalloori
we can upload the csv file only.

please go through the below link.

http://www.forcetree.com/2010/08/read-and-insert-records-from-csv-file.html
KSusan CoxKSusan Cox
Hi Ramesh,

Thanks for your suggestions. But I want to read the content of all files. I can read the content of text file but I am facing problem to read the content of other files such as docx, doc, pdf.

Thanks.


Ramesh KallooriRamesh Kalloori
Hi Ksusan,

i have bo idea on docx, doc, pdf uploaders i have worked on csv only.

thanks,
RAmesh
KSusan CoxKSusan Cox
Hi,

I am near to the solution to read the content of file such as doc, xls, ppt, pdf. And it works fine.
But it consumes more CPU time so I am working on that.

Here I provide you the solution whihc works for me:

public static String blobToString(Blob input, String inCharset){
    String hex = EncodingUtil.convertToHex(input);
    System.assertEquals(0, hex.length() & 1);
    final Integer bytesCount = hex.length() >> 1;
    String[] bytes = new String[bytesCount];
    for(Integer i = 0; i < bytesCount; ++i)
       bytes[i] =  hex.mid(i << 1, 2);
    return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
    }

Thanks.
Diamond EmporiumDiamond Emporium
@Susan

Could you please let us know what is the value of the string variable, 'inCharset'.
jagan nathanjagan nathan
@KSusan Cox 

Even we have similar requirement which needs to be implemeneted ASAP.Could you please  share your code and findings so that it will be useful for me to proceed further.

Inaddition to it can you please share your findings on this.

Thanks inadvance :)


KSusan CoxKSusan Cox

Hi Jagan nathan

I have alrady shared code related to read the content of file.

Here I provide you the solution which works for me:

public static String blobToString(Blob input, String inCharset){
    String hex = EncodingUtil.convertToHex(input);
    System.assertEquals(0, hex.length() & 1);
    final Integer bytesCount = hex.length() >> 1;
    String[] bytes = new String[bytesCount];
    for(Integer i = 0; i < bytesCount; ++i)
       bytes[i] =  hex.mid(i << 1, 2);
    return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
    }

this code is used to convert content blob to string and then you have to call it in your relavent method.

For e.g. :  contnent = blobToString(your variable, 'ISO 8859-1');

Thanks
jagan nathanjagan nathan
@KSusan Cox
how to find  the encoding of the blob data?
@param inCharset

Feroz Khan 13Feroz Khan 13
Hi KSusan Cox,

I had tried this method am also able to read the doc format file.but am not able to read docx file format.

and also getting some blob code in the string like as ��ࡱ�>�� .0����-���������������������������������������
���������������������������������������
���������������������������������������
���������������������������

 and also getting all the doc file format means fornt format size format all those things.

how can we get exact file content with out any other extra data.


Thank you for the help,