Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to open pdf in the cefsharp browser itself. But nothing is getting displayed when I open the pdf. I have set the logs to error level but there are no errors in it.

This is how I am registering the CefCustomScheme :

 settings.RegisterScheme(new CefCustomScheme
                    SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
                    SchemeHandlerFactory = new CefSharpSchemeHandlerFactory(),
                    IsCSPBypassing = true,

This is my CefSharpSchemeHandlerFactory :

public class CefSharpSchemeHandlerFactory : ISchemeHandlerFactory
        public const string SchemeName = "local";
        public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
            if (schemeName == SchemeName && request.Url.EndsWith("CefSharp.Core.xml", System.StringComparison.OrdinalIgnoreCase))
                //Convenient helper method to lookup the mimeType
                var mimeType = ResourceHandler.GetMimeType(".xml");
                //Load a resource handler for CefSharp.Core.xml
                //mimeType is optional and will default to text/html
                return ResourceHandler.FromFilePath("CefSharp.Core.xml", mimeType);
            return new CefSharpSchemeHandler();

This is my CefSharpSchemeHandler :

    internal class CefSharpSchemeHandler : IResourceHandler
        private static readonly IDictionary<string, string> ResourceDictionary;
        private string mimeType;
        private MemoryStream stream;
        static CefSharpSchemeHandler()
        bool IResourceHandler.ProcessRequest(IRequest request, ICallback callback)
            // The 'host' portion is entirely ignored by this scheme handler.
            var uri = new Uri(request.Url);
            var fileName = uri.AbsolutePath;
            string resource;
            if (ResourceDictionary.TryGetValue(fileName, out resource) && !string.IsNullOrEmpty(resource))
                Task.Run(() =>
                    using (callback)
                        var bytes = Encoding.UTF8.GetBytes(resource);
                        stream = new MemoryStream(bytes);
                        var fileExtension = Path.GetExtension(fileName);
                        mimeType = ResourceHandler.GetMimeType(fileExtension);
                        callback.Continue();
                return true;
                callback.Dispose();
            return false;
        void IResourceHandler.GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl)
            responseLength = stream == null ? 0 : stream.Length;
            redirectUrl = null;
            response.StatusCode = (int)HttpStatusCode.OK;
            response.StatusText = "OK";
            response.MimeType = mimeType;
        bool IResourceHandler.ReadResponse(Stream dataOut, out int bytesRead, ICallback callback)
            callback.Dispose();
            if(stream == null)
                bytesRead = 0;
                return false;
            //Data out represents an underlying buffer (typically 32kb in size).
            var buffer = new byte[dataOut.Length];
            bytesRead = stream.Read(buffer, 0, buffer.Length);
            dataOut.Write(buffer, 0, buffer.Length);
            return bytesRead > 0;
        bool IResourceHandler.CanGetCookie(Cookie cookie)
            return true;
        bool IResourceHandler.CanSetCookie(Cookie cookie)
            return true;
        void IResourceHandler.Cancel()
        void IDisposable.Dispose()

And this how I am opening My pdf file from some other class :

 string pdfPath = @"D:\Magic\10774.pdf";
 var mimeType = ResourceHandler.GetMimeType(".pdf");
 ResourceHandler.FromFilePath(pdfPath, mimeType);

I am not getting a hit at CefSharpSchemeHandlerFactory create method.

See github.com/cefsharp/CefSharp.MinimalExample/commit/… for an example, little old, the idea is the same. – amaitland Nov 19, 2019 at 8:16 There is also cefsharp.github.io/api/75.1.x/html/… if you only wish to load files from disk, there's an example in the project source. – amaitland Nov 19, 2019 at 8:17 @amaitland Thanks now I am able to open local files in browser. I am directly giving the local path of the document to load method of browser and it is getting open. I am not making use of CefSharpSchemeHandler . One thing I need to ask what is the use of this registering CefCustomScheme when directly I am able to open the pdf file by just passing the path of the pdf to browser.load method. – Akshay Verma Nov 19, 2019 at 13:09 @amaitland : I am not able to open ppt, docx, xlsx types of file in browser. These types of file are getting downloaded instead of rendering in browser. If there is any way to open docx inside cefsharp browser itself ? – Akshay Verma Nov 19, 2019 at 13:11

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.