下面是一个实现的示例代码,点击按钮后会读取一个指定的PDF文件,并将其内容通过网络发送到指定IP的客户端展示。

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace PDFReader
{
    public partial class MainForm : Form
    {
        private const int Port = 12345; // 指定的网络端口号
        private const string DestinationIp = "192.168.0.100"; // 指定的目标IP地址

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnReadPDF_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
            openFileDialog.Title = "Select a PDF File";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                byte[] fileBytes = File.ReadAllBytes(filePath);

                // 发送PDF文件内容到指定IP的客户端
                SendPDFContents(fileBytes);
            }
        }

        private void SendPDFContents(byte[] fileBytes)
        {
            try
            {
                // 创建TCP客户端套接字
                TcpClient client = new TcpClient(DestinationIp, Port);

                // 获取网络流
                NetworkStream networkStream = client.GetStream();

                // 发送文件内容
                networkStream.Write(fileBytes, 0, fileBytes.Length);

                // 关闭网络流和套接字
                networkStream.Close();
                client.Close();

                MessageBox.Show("PDF文件内容已发送到指定IP的客户端!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("发送文件内容出错:" + ex.Message);
            }
        }
    }
}

请注意,这只是一个基本的示例,需要根据实际需求进行修改和完善。例如,你可能需要添加错误处理、输入验证、网络连接状态检查等功能

C# 点击按钮读取一个pdf文件并将内容展示然后通过网络发送到指定ip的客户端展示

原文地址: https://www.cveoy.top/t/topic/inC9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录