使用 IText writer 检查 pdf 是否打开
Use IText writer to check if pdf is opened
我正在寻找一种方法来检查存储在共享网络上的 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
所以我的问题是。我可以关闭 PDFwriter 而不对 PDF 做任何事情吗?取消写入?
可以将相同的内容写入文件而不是取消写入。
首先让我们将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 可能会稍微优化一下。
我正在寻找一种方法来检查存储在共享网络上的 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
所以我的问题是。我可以关闭 PDFwriter 而不对 PDF 做任何事情吗?取消写入?
可以将相同的内容写入文件而不是取消写入。
首先让我们将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 可能会稍微优化一下。