刚开始的时候以为不是很好做,后来网上搜了一下才发现有现成的dll,就是iTextsharp.dll, 主要是用来处理文档的。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace ReadingSystem
{
class ImageToPdf
{
//固定高宽,便于后续处理
const int WWidth = 600;
const int HHeight = 800;
List<System.Drawing.Image> AllName = new List<System.Drawing.Image>();
string FileName = "C://result.pdf";
//It is the construction function;
public ImageToPdf()
{
//if (File.Exists(FileName))
//{
// File.Delete(FileName);
//}
}
public void TurnTheImageToPdf(ref List<string> SourceImage)
{
ChangeTheImageToS(ref SourceImage);
Document document = new Document();
document.SetPageSize(new iTextSharp.text.Rectangle(WWidth + 72f, HHeight + 72f));
PdfWriter write = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write));
document.Open();
iTextSharp.text.Image jpg;
for (int i = 0; i < AllName.Count; ++i)
{
jpg = iTextSharp.text.Image.GetInstance(AllName[i], ImageFormat.Jpeg);
document.NewPage();
document.Add(jpg);
}
if (document != null && document.IsOpen())
{
document.Close();
}
if (write != null)
{
write.Close();
}
}
//change all the image to standard
private void ChangeTheImageToS(ref List<string> ImageName)
{
for (int i = 0; i < ImageName.Count; ++i)
{
Bitmap src = new Bitmap(ImageName[i]);
Bitmap bmImage = new Bitmap(WWidth, HHeight);
Graphics g = Graphics.FromImage(bmImage);
g.InterpolationMode = InterpolationMode.Low;
g.DrawImage(src, new System.Drawing.Rectangle(0, 0, bmImage.Width, bmImage.Height), new System.Drawing.Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);
g.Dispose();
AllName.Add(bmImage);
}
}
}
}
这个可以很好的实现将不同大小的图片一起放在一个pdf文档中。