pdfvscode-extensions

VS Code Extension: open PDF side-by-side


Problem

I am writing a VS Code extension for VS Code 1.94.2 which shall open a PDF file side-by-side and I got this far using vscode.openWith:

vscode.commands.executeCommand(
    'vscode.openWith',
    uri_pointing_to_my_pdf_file,
    'default',
    vscode.ViewColumn.Beside
)

This opens an editor just as expected but the editor says:

The file is not displayed in the text editor because it is either binary or uses an unsupported text encoding.

If I click the Open Anyway button, I am offered the two options:

Choosing vscode-pdf then lets the side-by-side editor show the PDF. However, I have to manually choose that every time.

Question

How can I change my code so that the editor will show the PDF without the user choosing the vscode-pdf editor manually every time?

Unsuccessful Attempts

change viewId

Changing the viewId from 'default' to 'vscode-pdf' had no effect. Documentation says:

viewId - Custom editor view id or 'default' to use VS Code's default editor

'vscode.open' command

Using the 'vscode.open' command will open an editor which shows the PDF properly. But the editor opens in a new tab and not side-by-side.


Solution

  • I actually found the answer myself by looking at the source of the vscode-pdf extension: Their implementation of the vscode.CustomReadonlyEditorProvider interface defines viewType = 'pdf.preview' in this source file.

    I changed my code to:

    vscode.commands.executeCommand(
        'vscode.openWith',
        uri_pointing_to_my_pdf_file,
        'pdf.preview', // use correct editor provider name here: "pdf.preview"
        vscode.ViewColumn.Beside
    )
    

    This appears to be the correct way to use the 'vscode.openWith' command when opening a PDF.