在C#中,PDFsharp库使用(一):PDF合并

在C#中,PDFsharp库使用(一):PDF合并

PDF合并功能制作教程

PDFsharp 是一个流行的 C# 库,用于创建和处理 PDF 文档。它提供了一套丰富的 API,允许你以编程方式生成、编辑和渲染 PDF 文件

有朋友提出要仔细解析代码,所以在此记录一下:

  • PDF合并--界面

  • 1860f260649eae4dc2c51b7c2794f09d.png

  • PDF合并--代码

//添加文件button,

//>>这里可以添加多个PDF文件到Listbox列表中,运行时,会读取列表,一个一个PDF合并(拼接在成一个PDF文件)

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true; 
// 允许选择多个文件
openFileDialog.Filter = "pdf Files (*.pdf)|*.pdf"; 
// 设置文件过滤器
if (openFileDialog.ShowDialog() == DialogResult.OK) 
// 如果用户选择取消或者关闭,ShowDialog会返回Cancel
{
  listBox1.Items.Clear();
  foreach (string file in openFileDialog.FileNames)
  {
    listBox1.Items.Add(file); 
    // 将文件路径添加到Listbox中
  }
}
}

//删除button,

//>>对Listbox列表进行操作,如果添加文件不想要了可以选中它,再删除

private void button4_Click(object sender, EventArgs e)
 {
     // 确保至少有一个项被选中
      if (listBox1.SelectedItems.Count > 0)
      {
         // 删除选定的项
        listBox1.Items.Remove(listBox1.SelectedItems[0]);
      }
 }

//输出文件button,

//>>选择要输出的文件目录,文件名可以自己输入的

private void button2_Click(object sender, EventArgs e)
 {
  string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
   string filePath = SaveFileAs(myDocumentsPath, "合并文件名", "PDF Files (*.PDF)|*.pdf");//"C:\\Users\\User\\Documents"
    if (filePath != null)
      {
          textBox1.Text = filePath;
       }
 }

//执行合并Button,

//>>运行后,先读取Listbox中的文件,逐个添加拼接起来,合并成一个文件

private void button3_Click(object sender, EventArgs e)
{
      if (string.IsNullOrEmpty(textBox1.Text))
       {
          MessageBox.Show("没设置输出文件");
                return;
       }
     string outputFileName = textBox1.Text;
            // 创建一个新的PDF文档,用于合并
     using (PdfDocument mergedDocument = new PdfDocument())
      {
                // 遍历ListBox中的PDF文件列表
                foreach (string pdfFile in listBox1.Items)
                {
                    // 读取每个PDF文件
                    PdfDocument inputDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);


                    // 将每个PDF文档的页面添加到合并后的文档中
                    foreach (PdfPage page in inputDocument.Pages)
                    {
                        mergedDocument.AddPage(page);
                    }


                    // 关闭输入文档
                    inputDocument.Close();
                }
                // 保存合并后的PDF文件到指定目录
          mergedDocument.Save(outputFileName);
     }
            MessageBox.Show("PDF文件合并完成,并已保存到 " + outputFileName);
}

完成,

你按图制作WinForm,再复制粘贴代码就可以完成的。

学会了,记得转发给朋友免费学习哦

====================================
关注我的订阅号(公众号):看更多的文章

====================================

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#如何使用PDFsharpC#创建PDF文档? PDFsharp是一个开源的.NET,可用于创建和处理PDF文档。 要使用PDFsharp创建PDF文档,请按照以下步骤操作: 1. 下载和安装PDFsharp。 你可以从NuGet上下载PDFsharp。 2. 创建一个新的C#控制台应用程序。 3. 在项目添加PDFsharp参考。 4. 在代码创建PDF文档对象。 5. 添加一个页面到文档。 6. 将文本和图形元素添加到页面。 7. 保存PDF文档。 以下是一些示例代码: using PdfSharp.Pdf; using PdfSharp.Drawing; // Create a new PDF document PdfDocument document = new PdfDocument(); // Add a new page to the document PdfPage page = document.AddPage(); // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(page); // Draw some text XFont font = new XFont("Verdana", 20, XFontStyle.Bold); gfx.DrawString("Hello, world!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); // Draw a rectangle gfx.DrawRectangle(XPens.Black, new XRect(100, 100, 200, 200)); // Save the document document.Save("output.pdf"); // Open the document Process.Start("output.pdf"); 这将创建一个PDF文档,其包含一个页面,其包含一些文本和一个矩形。 在这个例子,我们使用XGraphics对象来绘制文本和图形元素。 然后,我们将文档保存到一个文件,并使用Process.Start()方法打开该文件。 希望这能帮助你开始使用PDFsharp创建PDF文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值