このファイルは問題なく作成され、電子メールで送信されます。しかし、その後不確定な時間削除または変更しようとすると、「別のプログラムで使用されているため、ファイルにアクセスできません」という恐ろしいメッセージが表示されます。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;
}
}