Xfinium PDF:如何自动调整图像大小

Xfinium PDF: How to auto-resize images

我正在使用 Xfinium PDF 库以编程方式创建 PDF 文档。我正在导入不同尺寸的图片,这些图片需要作为页面添加到 PDF 中。 DrawImage documentation 状态

If both width and height are negative then the image is scaled automatically to fit the given area and the original aspect ratio is kept.

但是,当我尝试这样做时,我在页面上看不到任何内容。我必须将图像添加到指定高度和宽度的父容器中吗?如果是这样,我该怎么做?我假设它会占用父页面的高度和宽度。这是我的代码片段:

var document = new PdfFixedDocument();
using (var imageStream = new FileStream(fileToOpen, FileMode.Open, FileAccess.Read))
{
    var image = new PdfPngImage(imageStream);
    var page = document.Pages.Add();
    page.Graphics.DrawImage(image, 0, 0, -1, -1);
}

DrawImage方法的最后2个参数指定了图像显示区域的大小。
如果你想覆盖整个页面但保持原始图像的纵横比,你必须按如下方式调用它:

page.Graphics.DrawImage(image, 0, 0, -page.Width, -page.Height);

免责声明:我在开发 XFINIUM.PDF 库的公司工作。