【内部资料】C#窗体打开ppt和pdf文件
作者:野牛程序员:2024-01-02 11:10:01C#阅读 2971
【内部资料】C#窗体打开ppt和pdf文件
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using PDFiumCore;
using System.Drawing.Imaging;
namespace PPTorPDF
{
public partial class MainForm : Form
{
private PowerPoint.Application pptApplication;
private PowerPoint.Presentation pptPresentation;
private FpdfDocumentT document;
private int pageCount;
private int pageIndex;
private string documentstr;
public MainForm()
{
InitializeComponent();
InitializePowerPoint();
fpdfview.FPDF_InitLibrary();
fpdfview.FPDF_InitLibrary();
// document = fpdfview.FPDF_LoadDocument("sample.pdf", null);
// 订阅鼠标滚轮事件
this.MouseWheel += MainForm_MouseWheel;
}
private void InitializePowerPoint()
{
// pptApplication = new PowerPoint.Application();
pptApplication = new PowerPoint.Application();
}
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "PDF Files|*.pdf;*.PDF",
Title = "Select a PDF File"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
documentstr = openFileDialog.FileName;
document = fpdfview.FPDF_LoadDocument(openFileDialog.FileName, null);
//using var fs = File.OpenRead(openFileDialog.FileName);
//var fileBytes = new byte[fs.Length];
//using var ms = new MemoryStream(fileBytes);
// Copy file the underlying byte stream.
//fs.CopyTo(ms);
pageCount = fpdfview.FPDF_GetPageCount(document);
pageIndex = 0;
ShowPage();
}
}
//private void OpenPresentation(string filePath)
//{
// try
// {
// pptPresentation = pptApplication.Presentations.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
// pptPresentation.SlideShowSettings.Run();
// }
// catch (Exception ex)
// {
// MessageBox.Show($"Error opening PowerPoint presentation: {ex.Message}",
// "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// }
//}
private void OpenPresentation(string filePath)
{
try
{
pptPresentation = pptApplication.Presentations.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
// 获取幻灯片范围
PowerPoint.Slides slides = pptPresentation.Slides;
// 设置幻灯片范围,例如,从第一张到最后一张
int startingSlideIndex = 1;
int endingSlideIndex = slides.Count;
// 设置 SlideShowSettings
PowerPoint.SlideShowSettings slideShowSettings = pptPresentation.SlideShowSettings;
slideShowSettings.StartingSlide = startingSlideIndex;
slideShowSettings.EndingSlide = endingSlideIndex;
slideShowSettings.RangeType = PowerPoint.PpSlideShowRangeType.ppShowSlideRange;
// 运行幻灯片播放
slideShowSettings.Run();
}
catch (Exception ex)
{
MessageBox.Show($"Error opening PowerPoint presentation: {ex.Message}",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
ReleasePowerPoint();
}
private void ReleasePowerPoint()
{
if (pptPresentation != null)
{
pptPresentation.Close();
pptPresentation = null;
}
if (pptApplication != null)
{
pptApplication.Quit();
pptApplication = null;
}
}
private void OpenPDFFile(string filePath)
{
if (System.IO.File.Exists(filePath))
{
Process.Start(new ProcessStartInfo
{
FileName = filePath,
UseShellExecute = true
});
}
else
{
MessageBox.Show("File not found: " + filePath, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ShowPage()
{
// 获取页面
var page = fpdfview.FPDF_LoadPage(document, pageIndex);
// 获取页面的图像
var width = (int)fpdfview.FPDF_GetPageWidth(page);
var height = (int)fpdfview.FPDF_GetPageHeight(page);
var bitmap = fpdfview.FPDFBitmapCreate(width, height, 0);
fpdfview.FPDFBitmapFillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
fpdfview.FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
var stride = fpdfview.FPDFBitmapGetStride(bitmap);
var scan0 = fpdfview.FPDFBitmapGetBuffer(bitmap);
var image = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, scan0);
// 显示页面
pictureBox1.Image = image;
}
private void btnPrev_Click(object sender, EventArgs e)
{
if (pageIndex > 0)
{
pageIndex--;
ShowPage();
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (pageIndex < pageCount - 1)
{
pageIndex++;
ShowPage();
}
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
fpdfview.FPDF_DestroyLibrary();
}
private void panel2_SizeChanged(object sender, EventArgs e)
{
// 计算新的按钮大小和位置
// btnNext.Width = panel2.Width / 2; // 适当的计算方式
// btnNext.Height = panel2.Height / 2; // 适当的计算方式
// btnNext.Location = new System.Drawing.Point((panel2.Width - btnNext.Width) / 2, (panel2.Height - btnNext.Height) / 2);
}
private void openPPTButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "PowerPoint Files|*.ppt;*.pptx",
Title = "Select a PowerPoint File"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
OpenPresentation(openFileDialog.FileName);
}
}
private void MainForm_MouseWheel(object sender, MouseEventArgs e)
{
// 判断滚轮的方向,执行翻页操作
if (e.Delta > 0 && pageIndex > 0)
{
pageIndex--;
ShowPage();
}
else if (e.Delta < 0 && pageIndex < pageCount - 1)
{
pageIndex++;
ShowPage();
}
}
}
}野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

