0

I have a custom template with a field of type "File" in which a content editor can upload a file from media library.

I read the item as shown below and then i have a media item holding the file.

Sitecore.Data.Fields.FileField fileField = item.Fields["File"];

        Sitecore.Data.Items.Item file = fileField.MediaItem;

        if (fileField == null)
        {
            // File field not found
        }

        else if (file == null)
        {
            // No file selected
        }
        else
        {
            Sitecore.Data.Items.MediaItem media = new Sitecore.Data.Items.MediaItem(file);
            // Process media item

        }

Is there a straight forward way to read the content of this PDF file into one string?

  • 1
    What will you do with the string? – Michael West Jun 2 at 4:01
2

I used the following code to read the content of html media item, check if it works for pdf media item:

Sitecore.Data.Items.Item item = Sitecore.Context.Database.GetItem("/sitecore/media library/Files/yourfile");
Sitecore.Data.Items.Item mediaItem = new Sitecore.Data.Items.MediaItem(sampleItem);
using(var reader = new StreamReader(MediaManager.GetMedia(mediaItem).GetStream().Stream))
{
    string text = reader.ReadToEnd();
}

If didn't work you may need something like adobe PDF IFilter:

Adobe PDF IFilter is a free downloadable Dynamic Link Library (DLL) file that provides a bridge between a Microsoft indexing client and a library of Adobe PDF files. It consists of code that understands the Adobe Portable Document Format as well as code that can interface with the indexing client. When an indexing client needs to index content from PDF files, it will look in its registry for an appropriate DLL and will find the Adobe PDF IFilter. Adobe PDF IFilter will return text to the indexing client. The indexing client will then index the results and return the appropriate results to the user.

Code example of using IFilter can be found here

or something like itextsharp, following is a code sample:

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace PdfParser
{
    public static class PdfTextExtractor
    {
        public static string pdfText(string path)
        {
            PdfReader reader = new PdfReader(path);
            string text = string.Empty;
            for(int page = 1; page <= reader.NumberOfPages; page++)
            {
                text += PdfTextExtractor.GetTextFromPage(reader,page);
            }
            reader.Close();
            return text;
        }   
    }
}
| improve this answer | |
  • Unfortunately this isn't an answer, this is just a bunch of code snippets pasted together. While you're on the right track, if you'd like to help the OP, you're going to need to actually put together a coherent answer. – Matthew FitzGerald-Chamberlain Jun 1 at 20:47
  • 1
    @MatthewFitzGerald-Chamberlain, I used the first snippet to read html file content, not sure if it is working for pdf though (much simpler if it is working), else, you will need to use a third party library to help with extracting text from pdf, thought this is should be enough to answer, if you can point something wrong in the answer I would be more than happy to update or even remove. Thank you! – Mohammed Syam Jun 1 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.