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
Overlay image onto PDF using PDFSharp
提问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 PdfDocument
constructor to
这将在页面顶部附近生成具有指定图像的新 PDF。如果您需要使用现有文档,请将PdfDocument
构造函数更改为
PdfDocument document = new PdfDocument(filename);
where filename
is the name of the file to load and change the PdfPage
line to
在哪里filename
是要加载的文件名并将PdfPage
行更改为
PdfPage page = document.Pages[pageNum];
where pageNum
is 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 DrawImage
to 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.
只需将路径添加到您想要的图像,并指定图像的位置。