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();
}
}
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; }
}
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"));
}
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;
}
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;
}
}
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);
}
/// <summary>
/// 字体
/// </summary>
private Font _font;
/// <summary>
/// 文档大小
/// </summary>
private Rectangle _rect;
/// <summary>
/// 文档对象
/// </summary>
private readonly Document _document;
/// <summary>
/// 基础字体
/// </summary>
private BaseFont _basefont;
/// <summary>
/// 构造函数
/// </summary>
public PDFOperation()
{
_rect = PageSize.A4;
_document = new Document(_rect);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="type">页面大小(如"A4")</param>
public PDFOperation(string type)
{
if (string.IsNullOrEmpty(type))
{
throw new ArgumentNullException(type);
}
SetPageSize(type);
_document = new Document(_rect);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="type">页面大小(如"A4")</param>
/// <param name="marginLeft">内容距左边框距离</param>
/// <param name="marginRight">内容距右边框距离</param>
/// <param name="marginTop">内容距上边框距离</param>
/// <param name="marginBottom">内容距下边框距离</param>
public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
{
if (string.IsNullOrEmpty(type))
{
throw new ArgumentNullException(type);
}
SetPageSize(type);
_document = new Document(_rect, marginLeft, marginRight, marginTop, marginBottom);
}
/// <summary>
/// 设置字体
/// </summary>
public void SetBaseFont(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
_basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
/// <summary>
/// 设置字体
/// </summary>
/// <param name="size">字体大小</param>
public void SetFont(float size)
{
_font = new Font(_basefont, size);
}
/// <summary>
/// 设置页面大小
/// </summary>
/// <param name="type">页面大小(如"A4")</param>
public void SetPageSize(string type)
{
if (string.IsNullOrEmpty(type))
{
throw new ArgumentNullException(type);
}
switch (type.Trim())
{
//枚举需要的文档纸张大小
case "A3":
_rect = PageSize.A3;
break;
case "A4":
_rect = PageSize.A4;
break;
case "A8":
_rect = PageSize.A8;
break;
}
}
/// <summary>
/// 实例化文档
/// </summary>
/// <param name="os">文档相关信息(如路径,打开方式等)</param>
public void GetInstance(Stream os)
{
if (os == null)
{
throw new ArgumentNullException("os");
}
PdfWriter.GetInstance(_document, os);
}
/// <summary>
/// 打开文档对象
/// </summary>
/// <param name="os">文档相关信息(如路径,打开方式等)</param>
public void Open(Stream os)
{
if (os == null)
{
throw new ArgumentNullException("os");
}
GetInstance(os);
_document.Open();
}
/// <summary>
/// 关闭打开的文档
/// </summary>
public void Close()
{
_document.Close();
}
/// <summary>
/// 添加段落
/// </summary>
/// <param name="content">内容</param>
/// <param name="fontsize">字体大小</param>
public void AddParagraph(string content, float fontsize)
{
SetFont(fontsize);
var pra = new Paragraph(content, _font);
_document.Add(pra);
}
/// <summary>
/// 添加段落
/// </summary>
/// <param name="content">内容</param>
/// <param name="fontsize">字体大小</param>
/// <param name="alignment">对齐方式(1为居中,0为居左,2为居右)</param>
/// <param name="spacingAfter">段后空行数(0为默认值)</param>
/// <param name="spacingBefore">段前空行数(0为默认值)</param>
/// <param name="multipliedLeading">行间距(0为默认值)</param>
public void AddParagraph(string content, float fontsize, int alignment, float spacingAfter, float spacingBefore, float multipliedLeading)
{
SetFont(fontsize);
var pra = new Paragraph(content, _font)
{
Alignment = alignment
};
if (spacingAfter != 0)
{
pra.SpacingAfter = spacingAfter;
}
if (spacingBefore != 0)
{
pra.SpacingBefore = spacingBefore;
}
if (multipliedLeading != 0)
{
pra.MultipliedLeading = multipliedLeading;
}
_document.Add(pra);
}
/// <summary>
/// 添加图片
/// </summary>
/// <param name="path">图片路径</param>
/// <param name="alignment">对齐方式(1为居中,0为居左,2为居右)</param>
/// <param name="newWidth">图片宽(0为默认值,如果宽度大于页宽将按比率缩放)</param>
/// <param name="newHeight">图片高</param>
public void AddImage(string path, int alignment, float newWidth, float newHeight)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
var img = Image.GetInstance(path);
img.Alignment = alignment;
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (newWidth != 0)
{
img.ScaleAbsolute(newWidth, newHeight);
}
else
{
if (img.Width > PageSize.A4.Width)
{
img.ScaleAbsolute(_rect.Width, img.Width * img.Height / _rect.Height);
}
}
_document.Add(img);
}
/// <summary>
/// 添加链接
/// </summary>
/// <param name="content">链接文字</param>
/// <param name="fontSize">字体大小</param>
/// <param name="reference">链接地址</param>
public void AddAnchorReference(string content, float fontSize, string reference)
{
if (string.IsNullOrEmpty(content))
{
throw new ArgumentNullException(content);
}
SetFont(fontSize);
var auc = new Anchor(content, _font)
{
Reference = reference
};
_document.Add(auc);
}
/// <summary>
/// 添加链接点
/// </summary>
/// <param name="content">链接文字</param>
/// <param name="fontSize">字体大小</param>
/// <param name="name">链接点名</param>
public void AddAnchorName(string content, float fontSize, string name)
{
if (string.IsNullOrEmpty(content))
{
throw new ArgumentNullException(content);
}
SetFont(fontSize);
var auc = new Anchor(content, _font)
{
Name = name
};
_document.Add(auc);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有