public string PdfToText() { string pdfText = String.Empty; PDFParser parser = new PDFParser(new BufferedInputStream(new FileInputStream(PdfFile))); parser.parse(); PDDocument originialPdfDoc = parser.getPDDocument(); bool isOriginalDocEncrypted = originialPdfDoc.isEncrypted(); if (isOriginalDocEncrypted) { originialPdfDoc.openProtection(new StandardDecryptionMaterial(PdfPassword)); } PDFTextStripper stripper = new PDFTextStripper(); try { pdfText = stripper.getText(originialPdfDoc); } catch (java.io.IOException ex) { throw ex; } return(pdfText); }
public string PdfFields() { string pdfText = String.Empty; PDFParser parser = new PDFParser(new BufferedInputStream(new FileInputStream(PdfFile))); parser.parse(); PDDocument originialPdfDoc = parser.getPDDocument(); bool isOriginalDocEncrypted = originialPdfDoc.isEncrypted(); if (isOriginalDocEncrypted) { originialPdfDoc.openProtection(new StandardDecryptionMaterial(PdfPassword)); } try { PDDocumentCatalog docCatalog = originialPdfDoc.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); PDField field = acroForm.getField("Name"); if (field != null) { field.setValue("name"); } } catch (java.io.IOException ex) { throw ex; } return(pdfText); }
/// <summary> /// Tries to decrypt a document with the given password /// </summary> /// <param name="doc">Document of type PDDocument</param> /// <param name="password">Password of type string</param> /// <returns>decrypted document (PDDocument) or null if decryption fails</returns> private static PDDocument Decrypt(PDDocument doc, string password) { var standardDecryptionMaterial = new StandardDecryptionMaterial(password); try { doc.openProtection(standardDecryptionMaterial); return doc; } catch (Exception ex) { Log.Debug("PdfUtil :: Decryption failed", ex); return null; } }