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.
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.
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.
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 ");
}
}
private void Clearppt_Click(object sender, EventArgs e)
{
{
pictureBox.Image = null;
}
}
The same Library file works to display the PDF files as well. You just need to add the button code and initialize the PDFviewer.
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);
}
}
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
tlover tonet 4 weeks ago
Some really marvelous work on behalf of the owner of this website , absolutely great written content.
ReplyHealXO 1 month ago
I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts
Replyhealxo 1 month ago
Wow wonderful blog layout How long have you been blogging for you make blogging look easy The overall look of your site is great as well as the content
Replyglobesimregistration 3 months 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山田 4 months ago
it helped me
Reply