Reputation:
Is there any function in Itextsharp whether pdf has password or watermark.
I have written below code but Contains("Downloaded By") will be dynamic every time.
byte[] bytes = Encoding.ASCII.GetBytes(FilePassword);
int page1;
if (FilePassword.Equals(""))
{
PdfReader pdfReader = new PdfReader(CurrentPath, bytes);
countWaterMarkFound = 0;
// Calculate whether watermark exist in the pdf
for (page1 = 1; page1 <= pdfReader.NumberOfPages; page1++)
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentPageText = PdfTextExtractor.GetTextFromPage(pdfReader, page1, strategy);
if (currentPageText.Contains("Downloaded By"))
{
countWaterMarkFound++;
// adding new WaterMark here
}
}
pdfReader.Close();
}
Upvotes: 2
Views: 464
Reputation: 95888
A watermark is not necessarily marked as such in a PDF, you won't get a certain check for it.
First of all, there are types of passwords in PDFs:
If a PDF is encrypted and you open it with the owner password, you have full access to the PDF in PDF processors. If you merely open it with the user password, the PDF processor may limit your access according to the PDF specification.
There is a default password value given in the PDF specification. If a PDF is encrypted with that password as user password, you can usually open it without entering a password at all or entering the empty string as password.
If the user password is not the default password, you need to enter a password to open the PDF.
Thus,
new PdfReader(CurrentPath)
, it is encrypted; in particular it is protected with a non-default user password (or a certificate);pdfReader = new PdfReader(CurrentPath)
, check pdfReader.isEncrypted()
; if that returns true
, the PDF is encrypted using the default user password; otherwise it is not encrypted.Upvotes: 1