与iText的(iTextSharp的)更换一个PDF模板页面上的多个不同的图像 [英] Replace multiple different images on one PDF template page with itext (itextsharp)

查看:2083
本文介绍了与iText的(iTextSharp的)更换一个PDF模板页面上的多个不同的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用户使用生成某些报告ASP.NET应用程序。到目前为止,我们有这样的它有一个像一个PDF模板,我们只是取代我们的编程生成的一个(图)该图像。结果
我们使用code从该网站为:HTTP://blog.rubypdf.com/2007/12/12/how-to-replace-images-in-a-pdf/

We have an ASP.NET application that users use to generate certain reports. So far we had one PDF template that had one image on it, and we would just replace that image with our programatically generated one (graph).
We have used code from this site for that:http://blog.rubypdf.com/2007/12/12/how-to-replace-images-in-a-pdf/

现在的问题是,我们有一个PDF页面上有两个不同的图像,并从链接code在一个页面上选择上述两个图像,并利用我们生成的图像一次更换它们。

Problem now is that we have two different images on one PDF page, and the code from link above selects both images on one page and replaces them all at once with our generated image.

有没有人有任何想法如何与iText的替换一个页面上有多个不同的图像?

Does anyone have any idea how to replace multiple different images on one page with itext?

感谢

推荐答案

唉。首先,让我改写一些源。

Ugh. First, let me rewrite some of that source.

PdfReader pdf = new PdfReader("in.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("c:\\out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance("image.png");
PdfDictionary pg = pdf.getPageN(1);
PdfDictionary res = pg.getAsDict.get(PdfName.RESOURCES);
PdfDictionary xobj = res.getAsDict(PdfName.XOBJECT);
if (xobj != null) {
  for (Iterator<PdfName> it = xobj.getKeys().iterator(); it.hasNext(); ) {
    PdfObject obj = xobj.get(it.next());
    if (obj.isIndirect()) {
      PdfDictionary tg = (PdfDictionary)PdfReader.getPdfObject(obj);
      PdfName type = tg.getAsName(PdfName.SUBTYPE));
      if (PdfName.IMAGE.equals(type)) {
        PdfReader.killIndirect(obj);
        Image maskImage = img.getImageMask();
        if (maskImage != null)
          writer.addDirectImageSimple(maskImage);
        writer.addDirectImageSimple(img, (PRIndirectReference)obj);
        break;
      }
    }
  }
}

哇。在 getAs 功能可以为您节省相当多的关节,油脂,使您的code更清晰。

Whew. the getAs functions can save you quite a bit of knuckle-grease and make your code much clearer.

现在。你需要能够在各种图像之间进行区分。如果你愿意的硬code的事情,你可以找出资源名称是什么,走这条路线:

Now. You need to be able to differentiate between the various images. If you're willing to hard-code things, you could find out what the resource names are and go that route:

String imageResName[] = {"Img1", "Img2" ... };
Image img[] = {Image.getInstance("foo.png"), Image.getInstance("bar.png"), ... };
for (int i = 0; i < imageResName.length; ++i) {
  PdfName curKey = new PdfName(imageResName[i]);
  PdfIndirectReference ref = xobj.getAsIndirect(curKey);
  PdfReader.killIndirect( ref );
  Image maskImage = img[i].getImageMask();
  if (maskImage != null) {
    writer.addDirectImageSimple(maskImage);
  }
  writer.addDirectImageSimple(img[i], (PRIndirectReference)ref);
}

如果你不愿意去硬codeD资源名称(没有人会指责你,恰恰相反,特别是当它们出现的顺序(因此)上到底有多少取决于他们的顺序哈希地图... [不寒而栗]),您可以根据图像的宽度和高度来区分。

If you're not willing to go with hardcoded resource names (and no one would fault you, quite the opposite, particularly when the order they appear (and thus the number on the end) depends on their order in a hash map... [shudder]), you may be able to differentiate based on image width and height.

//keep the original for loop, stepping through resource names
if (PdfName.IMAGE.equals(type)) {
  float width = tg.getAsNumber(PdfName.WIDTH).floatValue();
  float height = tg.getAsNumber(PdfName.HEIGHT).floatValue();

  Image img = getImageFromDimensions(width, height);

  Image maskImage = img.getImageMask();
  ...
}

这篇关于与iText的(iTextSharp的)更换一个PDF模板页面上的多个不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆