Overlay image onto PDF using PDFSharp

筅森魡賤 提交于 2019-11-27 15:52:49

问题


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?

Thanks


回答1:


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

PdfDocument document = new PdfDocument(filename);

where filename is the name of the file to load and change the PdfPage line to

PdfPage page = document.Pages[pageNum];

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

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

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

Good luck!




回答2:


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.



来源:https://stackoverflow.com/questions/18854935/overlay-image-onto-pdf-using-pdfsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!