C# 使用PrintDocument类生成PDF并加水印

C#使用PrintDocument类生成PDF并加水印

例如:根据现有的纸质文档设计其电子文档PDF,并能上抛。


一、需求分析

需求:

  1. 根据现有的生产打印小票,设计电子文档PDF并将数据显示
  2. 直接生成到本地文件或者使用对话框,需加LOGO以及文字水印

提示:以下是本篇文章正文内容,下面案例可供参考

二、具体实现

step1.引入库和命名空间

引入System.Drawing命名空间:using System.Drawing;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

step2.已经封装好的方法

两个关键函数:PrintDocumentPrint()和PrintDocumentPrintPage
具体的注释都在代码中

//两个全局变量
PDFPoundEntity throwFDFDataShow = new PDFPoundEntity();//显示打印内容全局变量
string upFildName = "";//存储本地pdf路径
/// <summary>
/// 调用PrintController和printDocument生成PDF文件
/// </summary>
void PrintDocumentPrint()
{
   
    //PrintController:控制一个PrintDocument是如何打印的。
    PrintDocument printDocument1 = new PrintDocument();
    //需要为当前页打印内容发生的函数
    printDocument1.PrintPage += new PrintPageEventHandler(PrintDocumentPrintPage);
    PrintController printController = new StandardPrintController();
    //获取或设置指导打印进程的打印控制器
    printDocument1.PrintController = printController;
    // 加入打印页面的设置处理
    //设置为PDF打印
    //printDocument1.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    //printDocument1.DocumentName = "计量单.pdf";
    try
    {
   
        string pathName = @"C:\NTPoundPDF";
        string fileName = @"C:\NTPoundPDF\磅单号为 " + throwFDFDataShow.measureListNo + @" 南通计量单 " + throwFDFDataShow.BusinessTypeText + ".pdf";
        upFildName = fileName;
        //string fileName = @"C:\NTPoundPDF\TestPrinting2.pdf";
        if (!Directory.Exists(pathName))
        {
   
            Directory.CreateDirectory(pathName);//在指定路径创建文件夹
        }
        if (!File.Exists(fileName))
        {
   
            printDocument1.PrinterSettings.PrintFileName = fileName;//设置保存文件路径和名字
        }
        else
        {
   
            LogFile.WriteLogLine($"磅单号为:{
     throwFDFDataShow.measureListNo}  的计量单的pdf已存在!");
            return;
        
C#中,精确地打印特定的PDF文件通常需要借助第三方库,因为`PrintDocument`主要用于控制打印机和文本输出,而不是直接处理PDF内容。一个常用的方法是使用.NET Framework中的`PdfiumViewer`库,它允许读取、查看和打印PDF文件。 以下是使用`PdfiumViewer`的基本步骤: 1. **安装NuGet包**: 首先,你需要在项目中添`PdfiumViewer.WinForms` NuGet包,这将提供一个`PdfiumViewerControl`控件用于显示PDF。 ```bash Install-Package PdfiumViewer.WinForms ``` 2. **PDF文件**: 在你的代码中,创建一个`PdfDocument`实例打开PDF文件。 ```csharp using PdfiumViewer; ... var document = new PdfDocument(); await document.OpenAsync(filePath); ``` 3. **创建PrintDocument**: 创建一个新的`PrintDocument`对象,设置其属性如纸张大小、页面范围等。 ```csharp PrintDocument printDoc = new PrintDocument(); printDoc.DocumentName = "PDF File"; ``` 4. **事件处理器**: 注册`PrintPage`事件,在这个事件中,你可以从PDF文档中获取页面数据绘制到控制上,然后通过`PrintDocument`发送到打印机。 ```csharp printDoc.PrintPage += (sender, e) => { var pdfView = PdfiumViewerControl.Document as PdfiumViewer.PdfiumViewer; if (pdfView == null) return; // 获取PDF页数 int pageCount = document.Pages.Count; int currentPageIndex = e.PageNumber; // 将PDF页面转换为图像绘制到e.Graphics using (var pageImage = pdfView.GetPageBitmap(pdfView.Document.GetPage(currentPageIndex))) { e.Graphics.DrawImage(pageImage, 0, 0); } }; ``` 5. **开始打印**: 当准备好后,可以调用`PrintDocument`的`BeginPrint`、`EndPrint`方法以及`Print`方法来开始和结束打印过程。 ```csharp printDoc.PrinterSettings.PrintFileName = "Output.pdf"; // 如果你想保存为PDF printDoc.Print(); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值