FreeSpire.PDF 操作类的使用方式
public static class PDFPressClass
{
public static string PDFCreate(string ModelPath, object obj)
{
string FilePath = "";
if (File.Exists(ModelPath))
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(ModelPath);
//PdfPageBase page = doc.Pages[0];
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
PropertyInfo[] properties = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
List<Image> imageList = new List<Image>();
List<PdfTextBoxFieldWidget> ImageWidaget = new List<PdfTextBoxFieldWidget>();
//Bitmap NewPicBitmap = new Bitmap(DemoImage.Width, DemoImage.Height);//创建新图片
//Graphics NewPic = Graphics.FromImage(NewPicBitmap);
//Rectangle PlayerPicStangle = new Rectangle(175, 50, 250, 188);//选手照片范围
//NewPic.DrawImage(PlayerImage, PlayerPicStangle);
//Rectangle DiejiaPicStangle = new Rectangle(0, 0, NewPicBitmap.Width, NewPicBitmap.Height);//整个两张照片合并
//NewPic.DrawImage(DemoImage, DiejiaPicStangle);
foreach (PdfTextBoxFieldWidget field in formWidget.FieldsWidget)
{
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("微软雅黑", 11f), true);
//PdfFont newfont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Italic | PdfFontStyle.Bold);
field.Font = (PdfFontBase)font;
if (field.Name.Contains("Image"))//放入image的域
{
ImageWidaget.Add(field);
foreach (PropertyInfo pt in properties)
{
Object temstr = pt.GetValue(obj, null);
if (pt.Name == field.Name)
{
Image image = temstr as Image;
imageList.Add(image);
}
}
}
else//字符串类型放入下面
{
foreach (PropertyInfo pt in properties)
{
Object temstr = pt.GetValue(obj, null);
//string TypeName = pt.GetType().Name;
if (pt.Name == field.Name)
{
//if (pt.PropertyType.Name.Contains("Image"))//将image放入imageList中
//{
// Image image = temstr as Image;
// imageList.Add(image);
//}
if (temstr != null)
{
field.Text = temstr.ToString();
}
}
//else
//{
//}
}
}
//field.Flatten = true;
// field.ReadOnly = true;
}
List<PdfTextBoxFieldWidget> ImageWidagetSetIn = ImageWidaget.OrderBy(x => x.Name).ToList();
int CountImage = ImageWidagetSetIn.Count >= imageList.Count ? imageList.Count : ImageWidagetSetIn.Count;//选少的那个
for (int i = 0; i < CountImage; i++)//贴图管理
{
using (MemoryStream stream = new MemoryStream())//使用内存读取则可以少出bug
{
Bitmap bitmap = new Bitmap(imageList[i]);
bitmap.Save(stream, ImageFormat.Jpeg);
PdfImage pdfImage = PdfImage.FromStream(stream);
RectangleF pt = ImageWidagetSetIn[i].Bounds;
doc.Pages[0].Canvas.DrawImage(pdfImage, pt);
}
}
string Filename = DateTime.Now.ToShortTimeString().Replace(":", "");
FilePath = string.Format("{0}\\{1}.pdf", Environment.CurrentDirectory, Filename);
doc.SaveToFile(FilePath, FileFormat.PDF);
System.Diagnostics.Process.Start(FilePath);
}
//File.Delete(FilePath);//删除临时文件
return FilePath;
}
}
//////image放到对应的RectangleF 中

1017

被折叠的 条评论
为什么被折叠?



