编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

283.C# PDFiumCore: 读取Pdf文件(c# 显示pdf文档)

wxchong 2024-08-03 02:54:27 开源技术 109 ℃ 0 评论

摘要


PDFiumCore就是这样一个库,它是Google的PDFium库的C#绑定。PDFium是一个开源的PDF渲染引擎,最初由Foxit Software提供,并被Google采用在其Chrome浏览器中以提供PDF查看功能。PDFiumCore使得C#开发人员可以在他们的应用程序中轻松地实现PDF相关的功能。

正文


PDFiumCore的特点

PDFiumCore通常具有以下特点:

  • 跨平台支持:由于PDFium本身就是跨平台的,PDFiumCore也可以在不同的操作系统上运行。
  • 高性能:PDFiumCore可以快速渲染PDF页面,使得它适合需要实时处理PDF的应用程序。
  • 丰富的功能:它提供了从基本的PDF渲染到更高级的功能,如表单填充、注释和文本提取。
  • 易于集成:PDFiumCore可以作为一个库集成到C#项目中,使得开发人员可以使用熟悉的C#语法和工具来处理PDF。

nuget 安装PDFiumCore

案例

public partial class Form1 : Form
{
    FpdfDocumentT document;

    int curPage = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnLoadPdf_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFile = new OpenFileDialog();
        openFile.Filter = "PDF|*.pdf";
        if (openFile.ShowDialog() == DialogResult.OK)
        {
            fpdfview.FPDF_InitLibrary();

            // 打开PDF文档
            document = fpdfview.FPDF_LoadDocument(openFile.FileName, null);

            lblCount.Text = fpdfview.FPDF_GetPageCount(document).ToString();

            LoadPdf();


        }
    }

    private void LoadPdf()
    {

        var page = fpdfview.FPDF_LoadPage(document, curPage);

        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);

        picPdf.Image = image;
    }

    private void btnPre_Click(object sender, EventArgs e)
    {
        if (curPage > 0)
        {
            curPage--;
            LoadPdf();
        }

    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (curPage < int.Parse(lblCount.Text))
        {
            curPage++;
            LoadPdf();
        }
    }
}

安装PdfiumViewer.Native.x86_64.v8-xfa与PdfiumViewer.Updated

private void Form1_Load(object sender, EventArgs e)
{
    DisplayPdf();
}

public void DisplayPdf()
{
    PdfViewer pdfViewer = new PdfViewer();
    this.Controls.Add(pdfViewer);
    pdfViewer.Document = PdfDocument.Load(@"D:\book\系统之美.pdf");
    pdfViewer.Dock=DockStyle.Fill;

}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表