试试下面的
private void GeneratePDF(string filename, string imageLoc)
{
PdfDocument document = new PdfDocument();
// Create an empty page or load existing
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
DrawImage(gfx, imageLoc, 50, 50, 250, 250);
// Save and start View
document.Save(filename);
Process.Start(filename);
}
void DrawImage(XGraphics gfx, string jpegSamplePath, int x, int y, int width, int height)
{
XImage image = XImage.FromFile(jpegSamplePath);
gfx.DrawImage(image, x, y, width, height);
}
这将生成一个新的 PDF,其中指定的图像位于页面顶部附近。如果您需要使用现有文档,请将 PdfDocument 构造函数更改为
PdfDocument document = new PdfDocument(filename);
其中filename 是要加载的文件的名称并将PdfPage 行更改为
PdfPage page = document.Pages[pageNum];
其中pageNum 是您需要添加图片的页面编号。
之后,只需更改DrawImage 的参数以适应页面位置即可。
DrawImage(gfx, imageLoc, 50, 50, 250, 250);
祝你好运!