该文件创建和电子邮件就好了。但是,当我尝试在不确定的时间内删除或修改它时,我会收到可怕的“无法访问文件,因为它正在被另一个程序使用”消息。我之前在 itextsharp 中创建的文档中看到过它,但这是因为 PdfStamper 处于打开状态。不知道它现在锁定了什么。尝试使用 WhoLockMe 来查看发生了什么,但它在 Windows 7 X 64 上不起作用。尝试使用 LockHunter 但它只是说该文件没有被任何进程锁定。该文件的位置仅限于管理员,并且我已确认在发生这种情况时文件中没有其他管理员。
public string SubmitWeekly(string store, string techId, string date, WeeklyHeader header, string latLon, string device, List<CPEquipment> equipment)
{
PdfReader templateReader = null;
PdfReader overFlowReader = null;
PdfStamper templateStamper = null;
PdfStamper overFlowStamper = null;
try
{
string result = "Success";
string year = date.Right(4);
string footerText = string.Format("Submitted {0} near {1} on device {2}", date, latLon, device);
string template = @"C:\Templates\WeeklyInspectionFormCombined.pdf";
string overflowTemplate = @"C:\Templates\WeeklyInspectionOverflowForm.pdf";
string newFile = string.Format(@"C:\Documents\{0}\{1}\Weekly Inspection {2} {3}.pdf", year, techId, store, date);
string overFlowFile = string.Format(@"C:\Documents\{0}\{1}\Weekly Inspection Pg 2 {2} {3}.pdf", year, techId, store, date);
string directory = string.Format(@"C:\Documents\{0}\{1}", year, techId);
templateReader = new PdfReader(template);
//Check for directory and create if doesn't exist
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
templateStamper = new PdfStamper(templateReader, new FileStream(newFile, FileMode.Create));
AcroFields templateFields = templateStamper.AcroFields;
AcroFields overFlowFields = null;
int equipCnt = 1;
int attachCnt = 1;
//Extract stand alone items from equipment list
List<CPEquipment> seUnits = (from s in equipment
where s.Serial == s.AttachedTo && s.Status != "ATTACHMENT"
select s).ToList();
//Extract attachments from equipment list
List<CPEquipment> attachments = (from a in equipment
where a.Status == "ATTACHMENT"
select a).ToList();
//Set header and footer info in new file
templateFields.SetField("Store Number", store);
templateFields.SetField("Technician", techId);
templateFields.SetField("Week Of", date.WeekOf());
templateFields.SetField("Street Address", stAddress);
templateFields.SetField("City", stCity);
templateFields.SetField("Comments", header.Comments + "\nTrained:\t" + header.Trained + "\nOn:\t" + header.Topics);
templateFields.SetField("Footer", footerText);
//Set security info in new file
templateFields.SetField("WL", header.WL == "True" ? "Yes" : "No");
templateFields.SetField("TL", header.TL == "True" ? "Yes" : "No");
templateFields.SetField("CC", header.CC == "True" ? "Yes" : "No");
//Set equipment lines in new file
int maxLines = seUnits.Count > 20 ? 20 : seUnits.Count;
while (equipCnt <= maxLines)
{
CPEquipment current = seUnits.ElementAt(equipCnt - 1);
CPEquipment trailer = (from t in equipment
where t.AttachedTo == current.Serial && t.AttachedTo != t.Serial && t.Status != "ATTACHMENT"
select t).FirstOrDefault();
templateFields.SetField("ModelRow" + equipCnt, current.Model);
templateFields.SetField("SerialRow" + equipCnt, current.Serial);
templateFields.SetField("StatusRow" + equipCnt, current.Status);
templateFields.SetField("Tool TurnedRow" + equipCnt, current.ToolTurned);
templateFields.SetField("Last PMRow" + equipCnt, current.LastPmDate + " " + current.LastPmHours);
templateFields.SetField("Current HoursRow" + equipCnt, current.CurrentHours);
templateFields.SetField("TagRow" + equipCnt, current.Tag);
templateFields.SetField("StExpiresRow" + equipCnt, current.State + " " + current.Expiration);
if (trailer != null)
{
templateFields.SetField("Trailer ModelRow" + equipCnt, trailer.Serial);
templateFields.SetField("Trailer VINRow" + equipCnt, trailer.Model);
templateFields.SetField("TagRow" + equipCnt, trailer.Tag);
templateFields.SetField("StExpiresRow" + equipCnt, trailer.State + " " + trailer.Expiration);
}
equipCnt++;
}
//Set overflow lines if any
if (seUnits.Count > 20)
{
overFlowReader = new PdfReader(overflowTemplate);
overFlowStamper = new PdfStamper(overFlowReader, new FileStream(overFlowFile, FileMode.Create));
overFlowFields = overFlowStamper.AcroFields;
overFlowFields.SetField("Store", store);
overFlowFields.SetField("Footer", footerText);
while (equipCnt > 20 && equipCnt < seUnits.Count)
{
CPEquipment current = seUnits.ElementAt(equipCnt - 1);
CPEquipment trailer = (from t in equipment
where t.AttachedTo == current.Serial && t.AttachedTo != t.Serial
select t).FirstOrDefault();
overFlowFields.SetField("ModelRow" + (equipCnt - 20), current.Model);
overFlowFields.SetField("SerialRow" + (equipCnt - 20), current.Serial);
overFlowFields.SetField("StatusRow" + (equipCnt - 20), current.Status);
overFlowFields.SetField("Tool TurnedRow" + (equipCnt - 20), current.ToolTurned);
overFlowFields.SetField("Last PMRow" + (equipCnt - 20),
current.LastPmDate + " " + current.LastPmHours);
overFlowFields.SetField("Current HoursRow" + (equipCnt - 20), current.CurrentHours);
overFlowFields.SetField("TagRow" + (equipCnt - 20), current.Tag);
overFlowFields.SetField("StExpiresRow" + (equipCnt - 20),
current.State + " " + current.Expiration);
if (trailer != null)
{
overFlowFields.SetField("Trailer ModelRow" + (equipCnt - 20), trailer.Serial);
overFlowFields.SetField("Trailer VINRow" + (equipCnt - 20), trailer.Model);
overFlowFields.SetField("TagRow" + (equipCnt - 20), trailer.Tag);
overFlowFields.SetField("StExpiresRow" + (equipCnt - 20),
trailer.State + " " + trailer.Expiration);
}
equipCnt++;
}
overFlowStamper.FormFlattening = true;
overFlowReader.Close();
overFlowStamper.Close();
}
//Set attachment fields and update attachment in db
foreach (CPEquipment attach in attachments)
{
templateFields.SetField("SNRow" + attachCnt, attach.Serial);
templateFields.SetField("MR" + attachCnt, attach.Model);
attachCnt++;
}
//Flatten forms and close readers and stampers
templateStamper.FormFlattening = true;
templateReader.Close();
templateStamper.Close();
//Set up email
MailMessage mail = new MailMessage(new MailAddress("me@mydomain.com"),
new MailAddress(techMail));
mail.Attachments.Add(new Attachment(newFile));
if (equipCnt > 20)
mail.Attachments.Add(new Attachment(overFlowFile));
mail.Subject = "Weekly Inspection";
SmtpClient client = new SmtpClient(//Hidden);
client.Send(mail);
return result;
}
catch (Exception e)
{
if (templateStamper != null)
{
templateStamper.FormFlattening = true;
templateStamper.Close();
}
if (overFlowStamper != null)
{
overFlowStamper.FormFlattening = true;
overFlowStamper.Close();
}
if (templateReader != null)
templateReader.Close();
if (overFlowReader != null)
overFlowReader.Close();
return "Submit Weekly: " + e.Message;
}
}