Home > Net >  Is there a way to get the full path (or even just the file name) of a PdfDocument object using itext
Is there a way to get the full path (or even just the file name) of a PdfDocument object using itext

Time:10-02

I have a list of PdfDocument objects that were split from one large original PDF file by page number (one doc for each page in the original).

All I want to do is iterate those documents

foreach (PdfDocument doc in splitDocuments)

And get the file path/doc name of each, but I have not been able to find a property that gives me that basic info. I need to copy the files to a different location based on some info inside the file, but so far I have not been able to get the file path.

Thanks for any help!

CodePudding user response:

In general there is no unique file name for a PdfDocument.

First of all recall that a PdfDocument can be created based on a PdfReader, a PdfWriter, or a PdfReader / PdfWriter pair. Both the reader and the writer may be file based. Thus, in general it is not clear what you mean by "the" file name, there may be two. But both may also be based on a non-file object, then there is no file name.

If the PdfWriter is file based, you can determine the associated file name from the underlying FileStream, for a PdfDocument doc that is

doc.GetWriter().GetOutputStream()

For the PdfReader the equivalent is not easy (if possible at all): Depending on the options used the file might have been read into a byte array and then forgotten. But even otherwise, i.e. if the reader still is based on the file stream, it is hidden under two or three layers of internal or private information. Using reflection you can work through those layers but that of course wouldn't be appropriate for production purposes.

Also you mention those PdfDocument objects were split from one large original PDF. If that happened by means of the PdfSplitter utility class, those documents don't have a PdfReader to start with.

  • Related