1

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
  • 1
    What will you do with the string? Commented Jun 2, 2020 at 4:01

3 Answers 3

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;
        }   
    }
}
2
  • 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. Commented Jun 1, 2020 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! Commented Jun 1, 2020 at 20:51
2

You can use the MediaItem.GetMediaStream() method to retrieve the binary data of the PDF file, and then use iTextSharp library to extract text from the PDF

using Sitecore.Data.Items;
using Sitecore.Resources.Media;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.IO;
using System.Text;

MediaItem media = new MediaItem(file);
Stream mediaStream = media.GetMediaStream();
StringBuilder text = new StringBuilder();
using (PdfReader reader = new PdfReader(mediaStream))
{
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
    }
}
mediaStream.Close();
string pdfContent = text.ToString();
2

There are two parts to this question.

  1. How to read content of a PDF file stored in Sitecore Media Library?
  2. How to parse text from a PDF file stored in Sitecore Media Library?

The first question is easy and answered above where as the second question is tricky in the sense that it will depend on your requirements. Most free PDF to Text are not good enough for complicated use cases and in that case you will need to use a Paid Library that can meet your requirements.

Here is a quick code sample using PDFBox.NET library http://www.squarepdf.net/how-to-convert-pdf-to-text-in-net

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Fields;
using Sitecore.Resources.Media;
using Sitecore.Links;
using Sitecore.Configuration;
using Sitecore.Data;
using System.IO;
using System.Text;
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;


Database master = Sitecore.Configuration.Factory.GetDatabase("master");

var item = master.GetItem("/sitecore/content/Home/Test");
if (item == null) return;

Sitecore.Data.Fields.FileField fileField = item.Fields["File"];
Sitecore.Data.Items.Item file = fileField.MediaItem;

var media = new MediaItem(file);
var mediaStream = media.GetMediaStream();

PDDocument doc = null;
try
{
    java.io.InputStream javaStream = new ikvm.io.InputStreamWrapper(mediaStream);
    doc = PDDocument.load(javaStream);
    var stripper = new PDFTextStripper();
    var pdfContent = stripper.getText(doc);
    Response.Write(pdfContent);
}
finally
{
    if (doc != null)
    {
        doc.close();
    }
}

See full working examples

  1. Using iTextSharp
  2. Using PDFBox.NET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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