C# Tutorials

How to open PDF/PPT File in Visual c# app

03 September, 2024 SamkarTech
pdf

Welcome to another new tutorial on Visual C#. In this tutorial, we will create an application in Visual C# that can display PDF and PPT files. We will add buttons to show PDF and PPT/PPTX files. We will use the FreeSpire.Office library, which is a free version, so we can only display PDF and PPT files up to 10 pages. This application does not require Microsoft Office to be pre-installed on your PC.

PDf
Fig:- Sample App to display PPT and PDF Files

Design PowerPoint viewer in ASP.NET Core application

Step 1: Create a new application in Visual Studio using C# .NET Framework and save it in your desired location.

Step 2: After the form appears on your screen, go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

Step 3: In the Browse section, search for FreeSpire.Office and install it in your project. If you want to display only one format, either PDF or PPT, search for FreeSpire.PDF for PDFs and FreeSpire.Presentation for PPTs.

PDF
Fig:-NUGet packet manager for solution

Step4. Add a 4 button Named ShowPPt, ClearPPT, Show PDF,Clear PDF.

To Display the PPT we need to convert the PPT file either into Images or in PDF, Here we convert the PPT slides into images and display the image in Picture Box. If you have multiple slides then you can add timer for sliding the images.

ShowPPT_Click

private void Showppt_Click(object sender, EventArgs e)
{
    pictureBox.Visible = true;
    pdfViewer.Visible = false;
    string pptfilepath = "C:\\Users\\user\\Desktop\\ppt\\121.pptx";
    try
    {
        if (!System.IO.File.Exists(pptfilepath))
        {
            MessageBox.Show("ppt not find");
        }
        using (Presentation presentation= new Presentation())
        {
            presentation.LoadFromFile(pptfilepath);

            if (presentation.Slides.Count>0)
            {
                Image slideimage = presentation.Slides[0].SaveAsImage();
                if (slideimage!=null)
                {
                    pictureBox.Image = slideimage;

                }
                else
                {
                    MessageBox.Show("Error in converting into image");
                }
            }
            else
            {
                MessageBox.Show("File not found");
            }
        }
    }
    catch
    {
        MessageBox.Show("Error loading ");
    }


}
pdf

Clearppt_Click

private void Clearppt_Click(object sender, EventArgs e)
{
   
    {
        pictureBox.Image = null;
    }
}

Design PDF viewer in ASP.NET Core application

The same Library file works to display the PDF files as well. You just need to add the button code and initialize the PDFviewer.

Showpdf_Click

 private void Showpdf_Click(object sender, EventArgs e)
 {
     pictureBox.Visible = false;
     pdfViewer.Visible = true;
     string pdfFilePath = "C:\\aapdf\\Screen_No.2.pdf";
     try
     {
         if (System.IO.File.Exists(pdfFilePath))
         {
             pdfViewer.LoadFromFile(pdfFilePath);
         }
         else
         {
             MessageBox.Show("PDF file not found.");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error loading PDF: " + ex.Message);
     }
 }
pdf

Initialize PictureBox To Display PPT and pdfViewer to display PDF

 private PictureBox pictureBox;
 private Spire.PdfViewer.Forms.PdfViewer pdfViewer;
 public Form1()
 {
     InitializeComponent();
     InitializepictureBox();
     InitializePdfViewer();
 }
 private void InitializePdfViewer()
 {
     pdfViewer = new Spire.PdfViewer.Forms.PdfViewer
     {
         Dock = DockStyle.Fill,
         Visible = false,
         Size = new Size(this.ClientSize.Width, this.ClientSize.Height)

     };
     this.Controls.Add(pdfViewer);
 }
 private void InitializepictureBox()
 {
     pictureBox = new PictureBox
     {
         Dock = DockStyle.Fill,
         Visible = false,
         SizeMode=PictureBoxSizeMode.Zoom
     };
     this.Controls.Add(pictureBox);
     
 }

Also Read :- C# App to Read/Write Switch PLC Bits

Demonstration Videos For PPT and PDF files

Display PPT File
Display PDF File


About author

SamkarTech

SamkarTech



2 Comments

globesimregistration 1 month ago

I do not even know how I ended up here but I thought this post was great I dont know who you are but definitely youre going to a famous blogger if you arent already Cheers

Reply

山田 1 month ago

it helped me

Reply

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *