C# 使用 PDFSharp 将图像叠加到 PDF 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18854935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:25:54  来源:igfitidea点击:

Overlay image onto PDF using PDFSharp

c#asp.net-mvc-4pdfsharp

提问by user948060

Can't seem to find much out there for this. I've a PDF, onto which I'd like to overlay an image of an electronic signature. Any suggestions on how to accomplish that using PDFSharp?

似乎无法找到太多关于此的信息。我有一个 PDF,我想在上面覆盖一个电子签名的图像。关于如何使用 PDFSharp 实现这一目标的任何建议?

Thanks

谢谢

采纳答案by Kami

Try the following

尝试以下

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);
}

This will generate a new PDF with the specified image near the top of the page. If you need to use an existing document change the PdfDocumentconstructor to

这将在页面顶部附近生成具有指定图像的新 PDF。如果您需要使用现有文档,请将PdfDocument构造函数更改为

PdfDocument document = new PdfDocument(filename);

where filenameis the name of the file to load and change the PdfPageline to

在哪里filename是要加载的文件名并将PdfPage行更改为

PdfPage page = document.Pages[pageNum];

where pageNumis the number of the page on which you need to add the image.

哪里pageNum是您需要添加图像的页面编号。

After that, it just a matter of getting the positioning on the page by altering the parameters for DrawImageto suit.

之后,只需通过更改参数来获取页面上的定位DrawImage即可。

DrawImage(gfx, imageLoc, 50, 50, 250, 250);

Good luck!

祝你好运!

回答by ffffff01

This will help you:

这将帮助您:

    PdfDocument document = pdf;

    // Create a new page        
    PdfPage page = document.Pages[0];
    page.Orientation = PageOrientation.Portrait;

    XGraphics gfx = XGraphics.FromPdfPage(page, XPageDirection.Downwards);

    // Draw background
    gfx.DrawImage(XImage.FromFile("pdf_overlay.png"), 0, 0);

Just add the path to the image you want, and specify the position of the image.

只需将路径添加到您想要的图像,并指定图像的位置。