public UploadPicModel UploadBannerImg() { UploadPicModel uploadPicModel = new UploadPicModel(); string info = string.Empty; try { //获取客户端上传的文件集合 HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; //判断是否存在文件 if (files.Count > 0) { int userID = Convert.ToInt32(System.Web.HttpContext.Current.Request.Form["Id"]); //获取文件集合中的第一个文件(每次只上传一个文件) HttpPostedFile file = files[0]; //定义文件存放的目标路径 string targetDir = System.Web.HttpContext.Current.Server.MapPath("/Img/UploadPic/BannerImg"); //创建目标路径 //ZFiles.CreateDirectory(targetDir); //组合成文件的完整路径 string path = System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file.FileName)); //保存上传的文件到指定路径中 file.SaveAs(path); //上传之后之后更新数据库信息 bool result = Config_BannerInfoBLL.UpdateBannerImg(new Config_BannerInfo() { CBannerId = userID, CBannerUrl = "/Img/UploadPic/BannerImg/" + file.FileName }); if (result) { uploadPicModel.IsSucceed = true; } else { uploadPicModel.IsSucceed = false; uploadPicModel.ErrorMsg = "更新数据库失败"; } } else { uploadPicModel.IsSucceed = false; uploadPicModel.ErrorMsg = "未获取到文件"; } } catch (Exception ex) { uploadPicModel.IsSucceed = false; uploadPicModel.ErrorMsg = "上传异常:" + ex.Message; } return(uploadPicModel); }
public IHttpActionResult UploadPic(UploadPicModel uploadPicModel) { var fullPath = Path.Combine(Config.Configuration.WorkingDirectory, "Editor", uploadPicModel.documentId); var downloadFileName = "document.pdf"; var documentFileName = Path.Combine(Config.Configuration.WorkingDirectory, "Editor", uploadPicModel.documentId, downloadFileName); Document doc = new Document(documentFileName); //Create image stamp ImageStamp imageStamp = new ImageStamp(HttpContext.Current.Server.MapPath("/img/Test.png")); for (int i = 0; i < uploadPicModel.shapes.Count; i++) { //create stamp Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp(); float shapeX = (uploadPicModel.shapes[i].x * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio); float shapeY = (uploadPicModel.shapes[i].y * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio); float shapeW = (uploadPicModel.shapes[i].w * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio); float shapeH = (uploadPicModel.shapes[i].h * 72 / 150) / (float)Convert.ToDouble(uploadPicModel.shapes[i].ratio); double yaxis = (float)(doc.Pages[uploadPicModel.shapes[i].p].Rect.URY - (shapeH + shapeY)); var isSpecial = true; //regexItem.IsMatch(uploadPicModel.shapes[i].imName); if (uploadPicModel.shapes[i].Itype == "highlight" || uploadPicModel.shapes[i].Itype == "image") { imageStamp = uploadPicModel.shapes[i].Itype == "highlight" ? new ImageStamp(HttpContext.Current.Server.MapPath("/img/test.png")) : new ImageStamp(fullPath + uploadPicModel.shapes[i].imName); imageStamp.Background = false; imageStamp.XIndent = (float)(shapeX); imageStamp.YIndent = (float)(yaxis); imageStamp.Height = shapeH; imageStamp.Width = shapeW; //Add stamp to particular page doc.Pages[uploadPicModel.shapes[i].p].AddStamp(imageStamp); } else if (uploadPicModel.shapes[i].Itype == "text") { //create text stamp TextStamp textStamp = new TextStamp(uploadPicModel.shapes[i].t); textStamp.XIndent = (float)(shapeX); textStamp.YIndent = (float)(yaxis); //rotate stamp textStamp.RotateAngle = 360 - (Convert.ToDouble(uploadPicModel.shapes[i].fieldType) * 180 / Math.PI); //set text properties textStamp.TextState.Font = FontRepository.FindFont(uploadPicModel.shapes[i].n); textStamp.TextState.FontSize = Convert.ToInt32(uploadPicModel.shapes[i].s) * 0.75f; if (uploadPicModel.shapes[i].wt == "bold") { textStamp.TextState.FontStyle = FontStyles.Bold; } if (uploadPicModel.shapes[i].st == "italic") { textStamp.TextState.FontStyle = FontStyles.Italic; } textStamp.TextState.ForegroundColor = GetColor(uploadPicModel.shapes[i].c); //add stamp to particular page doc.Pages[uploadPicModel.shapes[i].p].AddStamp(textStamp); } else if (uploadPicModel.shapes[i].Itype == "field" && isSpecial) { if (uploadPicModel.shapes[i].fieldType == "Text") { // Get a field TextBoxField textBoxField = doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)] as TextBoxField; // Modify field value textBoxField.Value = uploadPicModel.shapes[i].t; } else if (uploadPicModel.shapes[i].fieldType == "CheckBox") { // Get a field CheckboxField checkBoxField = doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)] as CheckboxField; if (uploadPicModel.shapes[i].t != "") { // Modify field value checkBoxField.Checked = Convert.ToBoolean(uploadPicModel.shapes[i].t); } } else if (uploadPicModel.shapes[i].fieldType == "Radio") { RadioButtonOptionField field = (RadioButtonOptionField)doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)]; RadioButtonField rbf = (RadioButtonField)field.Parent; if (Convert.ToBoolean(uploadPicModel.shapes[i].t)) { rbf.Selected = rbf.Options[field.OptionName].Index; } else { field.ActiveState = "Off"; } } else if (uploadPicModel.shapes[i].fieldType == "ComboBox") { // Get a field ComboBoxField comboBoxField = doc.Form.Fields[Convert.ToInt32(uploadPicModel.shapes[i].imName)] as ComboBoxField; var values = uploadPicModel.shapes[i].t.Split(new[] { "^^^" }, StringSplitOptions.None)[0]; foreach (var item in comboBoxField.Options.Cast <Option>()) { if (item.Value == values) { comboBoxField.Selected = item.Index; } } } } } doc.Save(Config.Configuration.OutputDirectory + "/Export.pdf"); return(Ok(new DocStatusModel())); }