i try to split a pdf file using dynamicPDF. I got stuck on this error, need help to resolve it.
exception happened on line 165, I put comment on that line.
System.IO.IOException: The process cannot access the file '\\DVW-APPDEV01\POMFiles\split\13373474220.001.nbis\Divider.pdf' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at ceTe.DynamicPDF.Document.Draw(String filePath)
at FileWatcher.Business.FileProcessor.SplitPDF(PageInfo[] pageInfo) in c:\Projects\FileWatcher\FileWatcher\Business\FileProcessor.cs:line 165
at FileWatcher.FileWatcherSvc.<>c__DisplayClass3.<OnFileChanged>b__1() in c:\Projects\FileWatcher\FileWatcher\FileWatcherSvc.cs:line 105
here's my code:
public Dictionary<string, string> SplitPDF(PageInfo[] pageInfo)
{
var pdfToSplit = new PdfDocument(fileInfo.PDFFile);
var totalPages = pdfToSplit.Pages.Count;
var folderPath = ConfigurationManager.AppSettings["SplitFolder"] + fileInfo.FileName + @"\";
PomHelper.CheckCreateFolder(folderPath);
var pdfList = new Dictionary<string, string>();
var policyfile = new Dictionary<string, PageInfo>();
for (int i = 0; i < pageInfo.Length; i++)
{
pageInfo[i].PageNumber = i == pageInfo.Length - 1 ? totalPages - pageInfo[i].StartPage : pageInfo[i + 1].StartPage - pageInfo[i].StartPage;
if (pageInfo[i].StartPage == 1 && pageInfo[i].PageNumber == 1)
{
pageInfo[i].Name = "DIVIDER";
}
if (string.Equals(pageInfo[i].Name, ConfigurationManager.AppSettings["Divider"], StringComparison.OrdinalIgnoreCase))
{
policyfile.Add("Divider", pageInfo[i]);
}
else if (string.Equals(pageInfo[i].Name, ConfigurationManager.AppSettings["StatCoding"], StringComparison.OrdinalIgnoreCase))
{
policyfile.Add("StatCoding", pageInfo[i]);
}
else if (string.Equals(pageInfo[i].Name, ConfigurationManager.AppSettings["InsuredCopy"], StringComparison.OrdinalIgnoreCase))
{
policyfile.Add("InsuredCopy", pageInfo[i]);
}
else if (string.Equals(pageInfo[i].Name, ConfigurationManager.AppSettings["InsuredSupplemental"], StringComparison.OrdinalIgnoreCase))
{
policyfile.Add("InsuredSupplemental", pageInfo[i]);
}
else if (string.Equals(pageInfo[i].Name, ConfigurationManager.AppSettings["RatingWorksheet"], StringComparison.OrdinalIgnoreCase))
{
policyfile.Add("RatingWorksheet", pageInfo[i]);
}
else if (string.Equals(pageInfo[i].Name, ConfigurationManager.AppSettings["PolicyWritingIndex"], StringComparison.OrdinalIgnoreCase))
{
policyfile.Add("PolicyWritingIndex", pageInfo[i]);
}
}
MergeOptions options = new MergeOptions(true);
options.Outlines = false;
var mergePDFs = new Dictionary<string, string>();
foreach (var pair in policyfile)
{
var filePath = folderPath + pair.Key + ".pdf";
var tmpPDF = new MergeDocument(pdfToSplit, pair.Value.StartPage, pair.Value.PageNumber <= 0 ? 1 : pair.Value.PageNumber, options);
tmpPDF.Draw(filePath); // this is the line 165
mergePDFs.Add(pair.Key, filePath);
}
return pdfList;
}
canno split file
Re: canno split file
Forum member · Jul 30, 2014Do you have the pdf document open in acrobat reader or some program like that? The Adobe Acrobat Reader lockes the file so it can not be edited.
Re: canno split file
Forum member · Jul 30, 2014I do not have the file open in adobe reader. i even restart the server.
Re: canno split file
Support · Jul 31, 2014Hello,
The error you are seeing could be thrown if the PDF file is being used by another process at the same time you are trying to update that PDF. When a file is passed to the MergeDocument object it holds a lock on the file until the Draw method is called. In your code you are working with multiple MergeDocument objects on the same file. Please make sure that these objects are not conflicting with each other. For example a MergeDocument object could be trying to draw to a file that is locked by another MergeDocument object.
Thanks,
ceTe Software Support Team.