日期:2014-05-17 浏览次数:21083 次
class ImageToPdf : FileToPdf
{
private Document document = null;
private string sourceFilePath = "";
private System.Drawing.Imaging.ImageFormat imageFormat = null;
public ImageToPdf(Document _document, string _sourceFilePath,System.Drawing.Imaging.ImageFormat _imageFormat)
{
document = _document;
sourceFilePath = _sourceFilePath;
imageFormat = _imageFormat;
}
public override void ConvertFile()
{
System.Drawing.Image sourceImg = null;
iTextSharp.text.Image pdfImage = null;
try
{
document.Open();
sourceImg = System.Drawing.Image.FromFile(sourceFilePath);
pdfImage = iTextSharp.text.Image.GetInstance(sourceImg, imageFormat);
pdfImage.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
float height = document.Top - document.TopMargin;
//图片原始大小
pdfImage.ScaleToFit(sourceImg.Width > document.Right ? document.Right : sourceImg.Width, sourceImg.Height > height ? height : sourceImg.Height);
//pdfImage.ScaleToFit(document.Right ,height);
document.Add(pdfImage);
}