If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. I am trying to write on an existing pdf and get the following error:
System.IO.IOException: The process cannot access the file 'c:\www\vhosts\domain.com\httpdocs\certificates_out\####xxxxx.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
The weird thing is it still creates it and appears to be working just I keep getting the error.
Any thoughts? I tried having a Using but that didn't work.
Code:
''' <summary> ''' fnCreateCertificate is to create a certificate file and save on the server. ''' </summary> ''' <returns>saved file and full path</returns> ''' <remarks></remarks> Public Function fnCreateCertificate() As String 'determine the full path of target file _TargetFullFolderFilePath = _TargetFolderPath & _TargetFilename '------ READ and WRITE TO PDF -------- Dim reader As PdfReader = New PdfReader(_SourcePDFBaseFile) Dim n As Integer = reader.NumberOfPages Dim psize As iTextSharp.text.Rectangle = reader.GetPageSize(1) Dim width As Single = psize.Width Dim height As Single = psize.Height Dim doc As Document = New Document(psize, 50, 50, 50, 50) ''create file stream 'Dim fsFile As Stream = New FileStream(_TargetFullFolderFilePath, FileMode.Create) 'The writer object is responsible for creating the actual PDF file. Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(_TargetFullFolderFilePath, FileMode.Create)) '<------- THIS IS WHERE THE ERROR IS POINTING TO 'Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(_TargetFullFolderFilePath, FileMode.Create)) 'open document doc.Open() Dim cb As PdfContentByte = writer.DirectContent Dim page1 As PdfImportedPage = writer.GetImportedPage(reader, 1) cb.AddTemplate(page1, 0, 0) 'tell the ContentByte we're ready to draw text cb.BeginText() Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED) Dim bfBold As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED) cb.SetFontAndSize(bfBold, 18) 'draw some text on a certain position cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _NameOnCertificate, psize.GetRight(0) / 2, _iYPos, 0) cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _CertificateDateScore, psize.GetRight(0) / 2, 282, 0) 'tell the contentByte, we've finished drawing text cb.EndText() 'fsFile.Dispose() Catch de As Exception Dim oSE As clsSendEmail = New clsSendEmail oSE.FormSendEmail(System.Configuration.ConfigurationManager.AppSettings("EMAIL-ADMIN"), "The following error occured: " & de.ToString, "Error") Finally 'close the document doc.Close() 'fsFile.Dispose() End Try Return _TargetFullFolderFilePath End Function Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.