使用 iText7 修改现有 pdf 时出现路径共享冲突错误

Path Sharing Violation Error when modifying existing pdf using iText7

我使用 Itext7 在 c# 中创建了一个 pdf,但在向其添加 header 后我无法覆盖该 pdf,它给出了路径共享冲突错误。我也在正确关闭文档。

我使用以下代码首先创建了至少 10 页的 Pdf:

           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

然后我用这个函数添加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();

      }

它给我这个错误:

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)

pdf 已创建,但当它调用 Header() 函数时,它给出了 PdfDocument 行的错误,而不是替换现有的 pdf。 谁能帮我解决这个问题?

您正在尝试创建一个 PdfReader 和一个 PdfWriter 处理同一个文件:

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

那不行;如果为文件构造,PdfReader 要求该文件在使用它时保持不变,但 PdfWriter 立即开始写入它,截断它。

如果你想用它的编辑版本替换原始文件,你应该暂时使用第二个文件。

或者,如果根本不需要临时文件,您可以先将文件读入内存 (a byte[]),将 reader 指向那个 byte[],然后writer 到原始文件名。或者将编写器指向 ByteArrayOutputStream 并在编辑后将其内容写入文件。