【问题标题】:How to insert an image into a pdf document with C#?如何使用 C# 将图像插入到 pdf 文档中?
【发布时间】:2017-10-16 14:14:39
【问题描述】:

我正在寻找一种使用免费 dll 库修改 pdf 的方法。 我想将图片插入到pdf文档的某个位置。

我已经找到了 iTextSharp,但这只是在我的文档前添加了一个页面。

如何将图像插入到特定位置?

var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
iTextSharp.text.Rectangle r_01 = new iTextSharp.text.Rectangle(30,10);
PdfContentByte page1 = stamper.GetOverContent(1);
image.SetAbsolutePosition(0,0);
page1.AddImage(image);
stamper.Close();

【问题讨论】:

  • “想要将图像插入到某个位置”,在您的代码中执行image.SetAbsolutePosition(0,0),但不知道接下来要尝试什么?此外,您说“这只在我的文档之前添加一个页面”,但您显示的代码根本没有添加页面。
  • 感谢您的评论,我只是放了一个实际上很好的代码。我通过 SetAbsP 参数播放,现在可以正常工作。我只是无法调整我放置的图像的大小
  • 您可能想查看ScaleAbsoluteScalePercentScaleToFit 重载。

标签: c# image pdf


【解决方案1】:

ITextSharp 是一个不错的工具,您实际上可以将图像添加到现有页面。我们使用它来自动生成我们的产品模板并添加二维码。

在此处查看 Darin Dimitrov 的回答: How can I insert an image with iTextSharp in an existing PDF?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2011-07-11
    • 1970-01-01
    • 2013-04-04
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多