详解开源免费且稳定实用的.NET PDF打印组件itextSharp(.NET组件介绍

2019-05-26 01:11:55丽君

对于打印的“文档”,具体看一下宽泛的概念,文档包含元素和节点等等。在组织打印的时候,我们需要创建文档,写入元素和节点等信息,最后组合成为我们需要打印的内容。itextSharp组件可以插入段落、表格、图片等等信息,可以很方便的完成我们需要完成的功能。

Paragraph:报表中的文本;Image:报表中的图片;PdfPTable:表格;PdfPCell:单元格。

1.Document类Open()方法:打开文档对象。

public virtual void Open()
{
  if (!this.close)
  {
    this.open = true;
  }
  foreach (IDocListener listener in this.listeners)
  {
    listener.SetPageSize(this.pageSize);
    listener.SetMargins(this.marginLeft, this.marginRight, this.marginTop, this.marginBottom);
    listener.Open();
  }
}

以上的代码可以看到,我们在打开文档的时候,会设置文档大小,文档页边距等信息。

2.Paragraph类Add()方法:向段落添加元素。

public override bool Add(IElement o)
{
  if (o is List)
  {
    List element = (List) o;
    element.IndentationLeft += this.indentationLeft;
    element.IndentationRight = this.indentationRight;
    base.Add(element);
    return true;
  }
  if (o is Image)
  {
    base.AddSpecial((Image) o);
    return true;
  }
  if (o is Paragraph)
  {
    base.Add(o);
    IList<Chunk> chunks = this.Chunks;
    if (chunks.Count > 0)
    {
      Chunk chunk = chunks[chunks.Count - 1];
      base.Add(new Chunk("n", chunk.Font));
    }
    else
    {
      base.Add(Chunk.NEWLINE);
    }
    return true;
  }
  base.Add(o);
  return true;
}

public interface IElement
{
  // Methods
  bool IsContent();
  bool IsNestable();
  bool Process(IElementListener listener);
  string ToString();

  // Properties
  IList<Chunk> Chunks { get; }
  int Type { get; }
}

以上的add()方法是向段落添加元素,我们可以看到参数是个接口“IElement”,我们接下来看一下这个接口,接口主要元素是块。我们看到在向段落添加元素时,可以添加List,Image,Paragraph,Chunk。

   3.Image.GetInstance()获取图片实例。

public static Image GetInstance(Image image)
{
  if (image == null)
  {
    return null;
  }
  return (Image) image.GetType().GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(Image) }, null).Invoke(new object[] { image });
}

 
public static Image GetInstance(byte[] imgb)
{
  int num = imgb[0];
  int num2 = imgb[1];
  int num3 = imgb[2];
  int num4 = imgb[3];
  if (((num == 0x47) && (num2 == 0x49)) && (num3 == 70))
  {
    GifImage image = new GifImage(imgb);
    return image.GetImage(1);
  }
  if ((num == 0xff) && (num2 == 0xd8))
  {
    return new Jpeg(imgb);
  }
  if (((num == 0) && (num2 == 0)) && ((num3 == 0) && (num4 == 12)))
  {
    return new Jpeg2000(imgb);
  }
  if (((num == 0xff) && (num2 == 0x4f)) && ((num3 == 0xff) && (num4 == 0x51)))
  {
    return new Jpeg2000(imgb);
  }
  if (((num == PngImage.PNGID[0]) && (num2 == PngImage.PNGID[1])) && ((num3 == PngImage.PNGID[2]) && (num4 == PngImage.PNGID[3])))
  {
    return PngImage.GetImage(imgb);
  }
  if ((num == 0xd7) && (num2 == 0xcd))
  {
    return new ImgWMF(imgb);
  }
  if ((num == 0x42) && (num2 == 0x4d))
  {
    return BmpImage.GetImage(imgb);
  }
  if ((((num == 0x4d) && (num2 == 0x4d)) && ((num3 == 0) && (num4 == 0x2a))) || (((num == 0x49) && (num2 == 0x49)) && ((num3 == 0x2a) && (num4 == 0))))
  {
    RandomAccessFileOrArray s = null;
    try
    {
      s = new RandomAccessFileOrArray(imgb);
      Image tiffImage = TiffImage.GetTiffImage(s, 1);
      if (tiffImage.OriginalData == null)
      {
        tiffImage.OriginalData = imgb;
      }
      return tiffImage;
    }
    finally
    {
      if (s != null)
      {
        s.Close();
      }
    }
  }
  throw new IOException(MessageLocalization.GetComposedMessage("the.byte.array.is.not.a.recognized.imageformat"));
}