image - iTextSharp - 文本重叠图像

标签 image text event-handling itextsharp

我正在使用 iTextSharpText 构建 PDF。

我被要求在图像上添加一些动态文本。 我已经尝试了一些我在这个论坛和其他网站上找到的例子,但我似乎做错了,这真的让我抓狂。

我关注了这个话题 ( iTextSharp Adding Text plus Barcode in a single cell? ),并尝试了这两个答案。

首先我尝试了:

            var img_operacao = Image.GetInstance(String.Format("{0}{1}", Settings.Default.SiteRootURL, "/PublishingImages/PDF_operacao.png"));
            img_operacao.ScaleToFit(150, 41);

            img_operacao.Alignment = Image.UNDERLYING;

            var ph0 = new Phrase(title0, pageHeader);
            var cell = new PdfPCell();

            cell.AddElement(ph0);
            cell.AddElement(new Phrase(new Chunk(img_operacao, 0, 0)));
            table.AddCell(cell);

我得到了这个: http://imageshack.us/photo/my-images/515/25265113.png/

我的第二次尝试:

            var img_operacao = Image.GetInstance(String.Format("{0}{1}", Settings.Default.SiteRootURL, "/PublishingImages/PDF_operacao.png"));
            img_operacao.ScaleToFit(150, 41);

            img_operacao.Alignment = Image.UNDERLYING;

            var cell = new PdfPCell
                       {
                           Border = Rectangle.NO_BORDER,
                           PaddingTop = 0,
                           PaddingBottom = 0,
                           HorizontalAlignment = Element.ALIGN_CENTER,
                           VerticalAlignment = Element.ALIGN_MIDDLE
                       };

            var ph0 = new Phrase();
            ph0.Add(new Chunk(title0, pageHeader));
            ph0.Add(new Chunk(img_operacao, 0, 0, true));

            cell.AddElement(ph0);
            table.AddCell(cell);

我得到了: http://imageshack.us/photo/my-images/688/89424619.png/

我已经尝试了其他几种包含 block 、段落等的变体,但我无法将文本覆盖在图像上!!!

如果有人能帮助我,我将不胜感激。 我现在卡住了...

非常感谢

宝拉

最佳答案

您似乎想向 PdfPCell 添加背景图像。推荐的方法是实现 IPdfPCellEvent界面。如文档所述,在编写实现 IPdfPCellEvent 的自定义类时,您只需编写一个方法 CellLayout。从您的代码来看,您似乎处于网络环境中,因此这个简单的示例 HTTP 处理程序 (.ashx) 应该很容易理解:

<%@ WebHandler Language="C#" Class="cellEvent" %>
using System;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;

public class cellEvent : IHttpHandler {
  public void ProcessRequest (HttpContext context) {
    HttpServerUtility Server = context.Server;
    HttpResponse Response = context.Response;
    Response.ContentType = "application/pdf";
    // replace with your image
    string imagePath = Server.MapPath("./Image1.png");
    Image image = Image.GetInstance(imagePath);
    using (Document document = new Document()) {
      PdfWriter.GetInstance(document, Response.OutputStream);      
      document.Open();
      PdfPTable table = new PdfPTable(2);
      for (int i = 1; i < 10; ++i) {
        PdfPCell cell = new PdfPCell(new PdfPCell());
        // add the text
        cell.Phrase = new Phrase(string.Format("CELL 1, ROW {0}", i));
        // your class that adds the background image
        cell.CellEvent = new TestCellEvent() { 
          CellImage = image
        };
        table.AddCell(cell);
        // add __new__ cell; CellEvent no longer applies;
        // (no background image)
        table.AddCell(new PdfPCell(new Phrase(
          string.Format("CELL 2, ROW {0}", i
        ))));
      }
      document.Add(table);
    }
  }
  public bool IsReusable { get { return false; } }

// custom class to implement a cell background image
  class TestCellEvent : IPdfPCellEvent {
    public Image CellImage;
    public void CellLayout(
      PdfPCell cell, Rectangle position, PdfContentByte[] canvases
    ) 
    {
      PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
      // scale image to the cell's __full__ height
      CellImage.ScaleAbsoluteHeight(cell.Height);
      // scale image to the cell's __full__ width
      CellImage.ScaleAbsoluteWidth(cell.Width);
      // you must also explcitly set the image position!
      CellImage.SetAbsolutePosition(position.Left, position.Bottom);
      // add the image to the cell
      cb.AddImage(CellImage);
    }
  }
}

希望行内评论有意义。请注意,Image 对象被实例化了一次。如果您重复使用相同的图像,这样做会大大减小文件大小,而不是为每个单元格重复实例化 Image,因为图像数据只添加到 PDF 一次。

祝你好运。

关于image - iTextSharp - 文本重叠图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657857/

相关文章:

java - 如何删除图像的白色背景 - java

iphone - 如何在 iOS 4+ 上正确使用 CGImageProperties.h

python - Matplotlib 添加文本,使其在点结束

java - 如何在我的 SWT 应用程序中检测 ctrl-f

java - JTable:检测单元格数据变化

C++如何获取png文件的图像大小(在目录中)

image - thumbnailImageAtTime : now deprecated - What's the alternative?

cocoa - 将普通字体更改为粗体字体

python - 如何在 Matplotlib 中为文本设置动画?

javascript - ExtJs 6.0 - Ext.application 内的 Javascript 事件处理函数作用域