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")); }
该方法根据参数获取图片实例的方式比较多,例如:Image,PdfTemplate,PRIndirectReference,byte[],Stream,string ,Uri等等,以上给出了根据Image和byte[]获取ItextSharp的image实例。
4.Image的ScaleAbsolute():设置图片信息。
public void ScaleAbsolute(float newWidth, float newHeight) { this.plainWidth = newWidth; this.plainHeight = newHeight; float[] matrix = this.Matrix; this.scaledWidth = matrix[6] - matrix[4]; this.scaledHeight = matrix[7] - matrix[5]; this.WidthPercentage = 0f; }
以上代码可以看出,设置图片的信息主要包括高度、宽度、排列等信息。
5.Anchor类的Process()方法:重写链接的处理方法。
public override bool Process(IElementListener listener) { try { bool flag = (this.reference != null) && this.reference.StartsWith("#"); bool flag2 = true; foreach (Chunk chunk in this.Chunks) { if (((this.name != null) && flag2) && !chunk.IsEmpty()) { chunk.SetLocalDestination(this.name); flag2 = false; } if (flag) { chunk.SetLocalGoto(this.reference.Substring(1)); } else if (this.reference != null) { chunk.SetAnchor(this.reference); } listener.Add(chunk); } return true; } catch (DocumentException) { return false; } }
以上方法可以看到,该方法是在本类中被重写,用以处理链接的相关信息。
6.PageSize:设置纸张的类型。
public class PageSize { // Fields public static readonly Rectangle _11X17; public static readonly Rectangle A0; public static readonly Rectangle A1; public static readonly Rectangle A10; public static readonly Rectangle A2; public static readonly Rectangle A3; public static readonly Rectangle A4; public static readonly Rectangle A4_LANDSCAPE; public static readonly Rectangle A5; public static readonly Rectangle A6; public static readonly Rectangle A7; public static readonly Rectangle A8; public static readonly Rectangle A9; public static readonly Rectangle ARCH_A; public static readonly Rectangle ARCH_B; public static readonly Rectangle ARCH_C; public static readonly Rectangle ARCH_D; public static readonly Rectangle ARCH_E; public static readonly Rectangle B0; public static readonly Rectangle B1; public static readonly Rectangle B10; public static readonly Rectangle B2; public static readonly Rectangle B3; public static readonly Rectangle B4; public static readonly Rectangle B5; public static readonly Rectangle B6; public static readonly Rectangle B7; public static readonly Rectangle B8; public static readonly Rectangle B9; public static readonly Rectangle CROWN_OCTAVO; public static readonly Rectangle CROWN_QUARTO; public static readonly Rectangle DEMY_OCTAVO; public static readonly Rectangle DEMY_QUARTO; public static readonly Rectangle EXECUTIVE; public static readonly Rectangle FLSA; public static readonly Rectangle FLSE; public static readonly Rectangle HALFLETTER; public static readonly Rectangle ID_1; public static readonly Rectangle ID_2; public static readonly Rectangle ID_3; public static readonly Rectangle LARGE_CROWN_OCTAVO; public static readonly Rectangle LARGE_CROWN_QUARTO; public static readonly Rectangle LEDGER; public static readonly Rectangle LEGAL; public static readonly Rectangle LEGAL_LANDSCAPE; public static readonly Rectangle LETTER; public static readonly Rectangle LETTER_LANDSCAPE; public static readonly Rectangle NOTE; public static readonly Rectangle PENGUIN_LARGE_PAPERBACK; public static readonly Rectangle PENGUIN_SMALL_PAPERBACK; public static readonly Rectangle POSTCARD; public static readonly Rectangle ROYAL_OCTAVO; public static readonly Rectangle ROYAL_QUARTO; public static readonly Rectangle SMALL_PAPERBACK; public static readonly Rectangle TABLOID; // Methods static PageSize(); public PageSize(); public static Rectangle GetRectangle(string name); }