PDF在浏览器中呈现为图像

时间:2018-04-05 18:22:18

标签: c# asp.net

我正在尝试将上传的PDF作为图像呈现到前端,以便我可以添加功能以添加带有html和javascript的注释。我将如何在c#中这样做?每次我尝试在浏览器中打开PDF时,浏览器的PDF查看器都会打开它。

1 个答案:

答案 0 :(得分:0)

如果您控制服务器,并且可能是您,只需Ghostscript

它可以从PDF中提取图像。将其安装在您的服务器上,并使用以下命令调用它:

/// <summary>
/// The command line arguments for generating a thumbnail
/// 
/// {0} is the input source path
/// {1} is the output destination folder path
/// {2} is the output horizontal resolution
/// {3} is the output vertical resolution
/// </summary>
private const string ThumbnailArguments = " -dBATCH -dNOPAUSE -sDEVICE=jpeg -dJPEGQ=95 -sOutputFile=\"{1}\\%03d.jpg\" \"{0}\"";

// Build the command line arguments
var arguments = string.Format(ThumbnailArguments, inputPath, outputFolderPath, imageWidth, imageHeight);

// Create the process to generate the thumbnail
using (var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "C:\\gswin64c.exe",
        Arguments = arguments,
        CreateNoWindow = true
    }
})
{
    // Attempt to start the process
    if (!process.Start())
        return;

    // Wait for the process to finish
    process.WaitForExit();
}

只需替换变量并找到适合您环境的路径。

您可以找到有关每个参数和命令here

的更多详细信息