.NET open PDF in winform without external dependencies

后端 未结 9 1057
孤城傲影
孤城傲影 2020-12-08 22:46

Is there a FREE library which will allow me to open a pdf and show it on a winform project. I know I could open it in adobe reader or something but it always seems so bloate

9条回答
  •  孤街浪徒
    2020-12-08 23:00

    First you need to reference the Adobe Reader ActiveX Control

    Adobe Acrobat Browser Control Type Library 1.0

    %programfiles&\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll

    Then you just drag it into your Windows Form from the Toolbox.

    And use some code like this to initialize the ActiveX Control.

    private void InitializeAdobe(string filePath)
    {
        try
        {
            this.axAcroPDF1.LoadFile(filePath);
            this.axAcroPDF1.src = filePath;
            this.axAcroPDF1.setShowToolbar(false);
            this.axAcroPDF1.setView("FitH");
            this.axAcroPDF1.setLayoutMode("SinglePage");
            this.axAcroPDF1.Show();
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    

    Make sure when your Form closes that you dispose of the ActiveX Control

    this.axAcroPDF1.Dispose();
    this.axAcroPDF1 = null;
    

    otherwise Acrobat might be left lying around.

提交回复
热议问题