Search code examples
c#pdfitextpdf-generationsharing-violation

Path Sharing Violation Error when modifying existing pdf using iText7


I created a pdf in c# using Itext7 but I am unable to overwrite the pdf after adding the header to it, it gives path sharing violation error. I am also closing the document properly.

I create a Pdf first of at least 10 pages using this code:

           if(FileBrowser.Success) 
           {
            PdfWriter writer = new PdfWriter(path);


            PdfDocument pdf = new PdfDocument(writer);


            Document document = new Document(pdf, size);


            PdfPage page1 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            PdfPage page2 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            PdfPage page3 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            PdfPage page4 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            PdfPage page5 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content              

            PdfPage page6 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            PdfPage page7 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            PdfPage page8 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            PdfPage page9 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content           

            PdfPage page10 = pdf.AddNewPage();
            document.Add(new iText.Layout.Element.AreaBreak());
            //Add Content

            document.Close();
           }
           Header();  //Calling Header Function

Then I am using this function to Add Header

 void Header()
    {

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(path), new PdfWriter(path));

        Document document = new Document(pdfDoc);


        Rectangle pageSize;
        PdfCanvas canvas;
        int n = pdfDoc.GetNumberOfPages();
        for (int i = 1; i <= n; i++)
        {
            PdfPage page = pdfDoc.GetPage(i);
            pageSize = page.GetPageSize();

            canvas = new PdfCanvas(page);
            canvas.BeginText().SetFontAndSize(PdfFontFactory.CreateFont(FontConstants.HELVETICA), 7)
           .MoveText(pageSize.GetWidth() / 2 - 24, pageSize.GetHeight() - 10)
           .ShowText("Header Text")
           .EndText();

        }
        document.Close();

      }

It gives me this error:

IOException: Sharing violation on path C:\Users\dell-pc\Desktop\ty1234.pdf
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <7ba07f088431485bb722f3b3373e87ee>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode) (at <7ba07f088431485bb722f3b3373e87ee>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode)
iText.IO.Util.FileUtil.GetBufferedOutputStream (System.String filename) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.Kernel.Pdf.PdfWriter..ctor (System.String filename, iText.Kernel.Pdf.WriterProperties properties) (at <3dc307d472b9422b8d3082a4addd20b6>:0)
iText.Kernel.Pdf.PdfWriter..ctor (System.String filename) (at <3dc307d472b9422b8d3082a4addd20b6>:0)
(wrapper remoting-invoke-with-check) iText.Kernel.Pdf.PdfWriter..ctor(string)
Panel.HeaderFooter () (at Assets/Scripts/Panel.cs:6253)
Panel+<ShowSaveDialogCoroutine>d__100.MoveNext () (at Assets/Scripts/Panel.cs:6244)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

The pdf is created but when it calls Header() function it gives error of PdfDocument line instead of replacing the existing pdf. Can anyone help me in fixing this issue?


Solution

  • You're trying to create both a PdfReader and a PdfWriter working on the same file:

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(path), new PdfWriter(path));
    

    That cannot work; if constructed for a file, the PdfReader requires that file to remain the same for as long as it works with it but the PdfWriter immediately starts writing to it, truncating it.

    If you want to replace the original file with an edited version of it, you should use a second file temporarily.

    Alternatively, if a temporary file is not desired at all, you can read the file into memory (a byte[]) first, point the reader to that byte[] and the writer to the original file name. Or point the writer to a ByteArrayOutputStream and after editing write the contents of that to the file.