使用IText writer检查是否已打开pdf

使用IText writer检查是否已打开pdf,itext,corrupt,Itext,Corrupt,我正在寻找一种方法来检查存储在共享网络上的PDF文件是否由用户X的另一个进程打开 我的搜索结果并不令人满意,我现在正尝试使用iText7 PDFwriter来检查PDF是否正在使用中。这是可行的,但当PDF没有使用时,我的理论就失败了 如果PDF未在使用中,则关闭写入程序会损坏我的PDF 我的代码: Public Function IsOpen(ByVal oPath As String) As Boolean Try Dim oWriter As New PdfWrit

我正在寻找一种方法来检查存储在共享网络上的PDF文件是否由用户X的另一个进程打开

我的搜索结果并不令人满意,我现在正尝试使用iText7 PDFwriter来检查PDF是否正在使用中。这是可行的,但当PDF没有使用时,我的理论就失败了

如果PDF未在使用中,则关闭写入程序会损坏我的PDF

我的代码:

Public Function IsOpen(ByVal oPath As String) As Boolean
    Try
        Dim oWriter As New PdfWriter(oPath)
        oWriter.Close()
        Return False
    Catch ex As Exception
        Return True
    End Try
End Function

所以我的问题是。我可以在不对PDF做任何操作的情况下关闭PDFwriter吗。取消写入?

可以将相同的内容写入文件,而不是取消写入

首先,让我们将pdf写入内存:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourcePath), new PdfWriter(baos));
    pdfDocument.close();
然后让我们用存储在内存中的字节重写文件:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(baos.toByteArray()), new ReaderProperties()),
            new PdfWriter(sourcePath), new StampingProperties().useAppendMode());
    pdfDoc.close();
副作用:在重写pdf的过程中,可以通过iText进行一些优化