简体   繁体   中英

How to open PowerPoint Presentation in New Tab in ASP.NET MVC C# project

I am working on an ASP.NET MVC application which have a some power point which I want to display it on new tab.

I had used openxml to read files but I don't know how to open it.

My code for reading the files:

    public ActionResult Index()
    {
        StringBuilder sb = new StringBuilder();
        string file = Server.MapPath("~/PPTFiles/Test.pptx");

        int numberOfSlides = CountSlides(file);

        string slideText;

        for (int i = 0; i < numberOfSlides; i++)
        {
            // Get slide text.
            GetSlideIdAndText(out slideText, file, i);
            sb.Append(slideText);
        }

        return View();
    }

    public static int CountSlides(string presentationFile)
    {
        using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
        {
            return CountSlides(presentationDocument);
        }
    }

    public static int CountSlides(PresentationDocument presentationDocument)
    {
        if (presentationDocument == null)
        {
            throw new ArgumentNullException("presentationDocument");
        }

        int slidesCount = 0;

        PresentationPart presentationPart = presentationDocument.PresentationPart;

        if (presentationPart != null)
        {
            slidesCount = presentationPart.SlideParts.Count();
        }

        return slidesCount;
    }

    public static void GetSlideIdAndText(out string slideText, string docName, int index)
    {
        using (PresentationDocument ppt = PresentationDocument.Open(docName, false))
        {
            PresentationPart part = ppt.PresentationPart;
            OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
            string relId = (slideIds[index] as SlideId).RelationshipId;
            SlidePart slide = (SlidePart)part.GetPartById(relId);
            StringBuilder paragraphText = new StringBuilder();
            IEnumerable<A.Text> texts = slide.Slide.Descendants<A.Text>();

            foreach (A.Text text in texts)
            {
                paragraphText.Append(text.Text);
            }

            slideText = paragraphText.ToString();
        }
   }

Can you please help me with this?

You can't open pptx file in a browser. You should put your.pptx file in a directory on server. And link it to a button. When User Clicks on button. It Will open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM