我使用 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:Usersdell-pcDesktopty1234.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
立即开始写入它,截断它。
如果要将原始文件替换为其编辑版本,则应暂时使用第二个文件。
或者,如果根本不需要临时文件,则可以先将文件读入内存(byte[]
(,将读取器指向该byte[]
,将编写器指向原始文件名。或者将编写器指向一个ByteArrayOutputStream
,并在编辑后将其内容写入文件。