0

我使用 Itext7 在 c# 中创建了一个 pdf,但是在向其添加标题后我无法覆盖 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

然后我使用这个函数来添加标题

 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。谁能帮我解决这个问题?

4

1 回答 1

1

您正在尝试在同一个文件上创建 aPdfReader和 aPdfWriter工作:

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

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

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

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

于 2020-02-14T17:23:20.040 回答