【问题标题】:Overlay image onto PDF using PDFSharp使用 PDFSharp 将图像叠加到 PDF 上
【发布时间】:2013-09-17 16:20:13
【问题描述】:

似乎找不到太多关于此的内容。我有一个 PDF,我想在其上叠加电子签名的图像。有关如何使用 PDFSharp 完成此任务的任何建议?

谢谢

【问题讨论】:

  • 您是要在文本中添加水印还是仅添加图像?
  • Kami,我正在寻找添加我从 SignaturePad 渲染的 JPG 图像,用户在其中电子签名了他们的名字。我想在 PDF 的正确位置覆盖签名。

标签: c# asp.net-mvc-4 pdfsharp


【解决方案1】:

试试下面的

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

祝你好运!

【讨论】:

  • 当调用 XGraphics.FromPdfPage 时,您可以指定新的绘图指令是在现有的前面还是附加。如果图像和文本重叠,使用 Prepend 可能会很有用。 Watermark 示例演示了 Prepend 和 Append(带有文本,而不是图像):pdfsharp.net/wiki/Watermark-sample.ashx
  • 天哪。如果 jpg 文件源取自 My.Resource 怎么办?我认为我们没有绝对的路径可以采取...
  • @gumuruh 上帝与它无关。还有其他可用的方法 - 也许 XImage.FromGdiPlusImage() ?它允许System.Drawing.Image 可以从文件或流等中加载。
  • 对于所有使用这个漂亮代码并打算在使用后将加载的图片加载到另一个文档甚至删除它的人,请在从文件加载后添加image.Dispose()并将其绘制到PDF格式。
  • 添加@MongZhu的评论--PdfDocument、XGraphics和XImage都支持IDisposable(使用“使用”)。
【解决方案2】:

这将对您有所帮助:

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

只需添加你想要的图片的路径,并指定图片的位置即可。

【讨论】:

  • f01 谢谢。这会将图像覆盖在现有 PDF 文档上还是新的空白文档上?
  • 这将覆盖指定的页面。
【解决方案3】:

为了大致保持纵横比,我使用了@Kami 的答案并将其“大致”居中。 假设pdf宽度为600,pdf高度为800,我们将只使用页面的中间500和700,留下4边至少50的边距。

    public static void GeneratePdf(string outputPath, string inputPath)
    {
        PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.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, inputPath);

        // Save and start View
        document.Save(outputPath);
        Process.Start(outputPath);
    }

    public static void DrawImage(XGraphics gfx, string imagePath)
    {
        XImage image = XImage.FromFile(imagePath);
        var imageHeight = image.PixelHeight;
        var imageWidth = image.PixelWidth;
        int height;
        int width;
        int x;
        int y;

        width = 500;
        height = (int) Math.Ceiling((double) width * imageHeight / imageWidth);

        x = 50;
        y = (int) Math.Ceiling((800 - height) / 2.0);
        
        if(height > 700)
        {
            height = 700;
            width = (int) Math.Ceiling(imageWidth * (double) height / imageHeight);

            y = 50;
            x = (int) Math.Ceiling((600 - width) / 2.0);
        }
        
        gfx.DrawImage(image, x, y, width, height);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2017-03-23
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多