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); }
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 PDFValidator(string fileName, string password) { try { this.IsValid = true; if (!System.IO.File.Exists(fileName)) { Console.WriteLine("The PDF file does not Exist."); } else { PDDocument pDDocument = null; pDDocument = (!string.IsNullOrEmpty(password) ? PDDocument.load(new java.io.File(fileName), password) : PDDocument.load(new java.io.File(fileName))); if (pDDocument.isEncrypted()) { this.IsPasswordProtected = true; } this.CheckAllPages(pDDocument); if (pDDocument != null) { pDDocument.close(); } } } catch (InvalidPasswordException invalidPasswordException) { this.IsPasswordProtected = true; this.IsValid = false; } catch (Exception exception) { this.ErrorMessage = string.Format("PDF analysis failed With exception {0}", exception.Message); this.IsValid = false; } }
public void pdfExtract(string keyWord, string path) { PDDocument doc = null; try { doc = PDDocument.load(path); if (doc.isEncrypted()) { doc.close(); return; } PDFTextStripper stripper = new PDFTextStripper(); bool isExist = stripper.getText(doc).ToLower().Contains(keyword); if (isExist) { pdfCount++; Task.Run(() => { Add_Item(Path.GetFileNameWithoutExtension(path), path, Path.GetExtension(path)); }); Thread.Sleep(200); } doc.close(); } catch { try { if (doc != null) { doc.close(); } } catch { return; } return; } }
private void LoadPdf(byte[] bytes, ref PDDocument document) { try { InputStream ins = new ByteArrayInputStream(bytes); document = PDDocument.loadNonSeq(ins, null); } catch (Exception) { try { InputStream ins = new ByteArrayInputStream(bytes); document = PDDocument.load(ins, null, true); } catch (IOException e) { throw new PdfNotReadableException($"The pdf could not be loaded correctly. The fileformat might be corrupted.", e); } catch (Exception e) { throw new PdfReadException($"Could not load byte array of pdf", e); } } if (document.isEncrypted()) { try { document.decrypt(""); } catch (InvalidPasswordException e) { throw new PdfNotReadableException($"The pdf is password protected.'", e); } } }
public PDF2PDFaConverter(string fileName, string output, string password, AquaforestPDFAFlavour PDFAFlavour) { try { int pDFAFlavour = (int)PDFAFlavour; this.SetFlavour(pDFAFlavour); this.PDFAFlavour = pDFAFlavour; this.outPutFileName = output; this.inputFileName = fileName; string tempPath = Path.GetTempPath(); Guid guid = Guid.NewGuid(); this.tempFileName = Path.Combine(tempPath, string.Concat("aquaforest\\pdftoolkit\\", guid.ToString(), "\\", Path.GetFileName(output))); string str = Path.Combine(Path.GetDirectoryName(this.tempFileName), "password.pdf"); string directoryName = Path.GetDirectoryName(this.tempFileName); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } System.IO.File.Copy(this.inputFileName, str, true); PDDocument pDDocument = PDDocument.load(new java.io.File(str)); if (pDDocument.isEncrypted()) { pDDocument.setAllSecurityToBeRemoved(true); } pDDocument.save(this.tempFileName); this.doc = new PDDocument(); switch (this.pdfaversion) { case 1: { if (this.doc.getDocument().getVersion() < 1.4f) { this.doc.getDocument().setVersion(1.4f); } break; } case 2: { if (this.doc.getDocument().getVersion() < 1.7f) { this.doc.getDocument().setVersion(1.7f); } break; } case 3: { if (this.doc.getDocument().getVersion() < 1.7f) { this.doc.getDocument().setVersion(1.7f); } break; } } foreach (PDPage page in pDDocument.getPages()) { this.doc.addPage(page); } this.doc.setDocumentInformation(pDDocument.getDocumentInformation()); this.doc.getDocumentCatalog().setDocumentOutline(pDDocument.getDocumentCatalog().getDocumentOutline()); this.doc.getDocumentCatalog().setAcroForm(pDDocument.getDocumentCatalog().getAcroForm()); this.doc.getDocumentCatalog().setLanguage(pDDocument.getDocumentCatalog().getLanguage()); this.doc.getDocumentCatalog().setMetadata(pDDocument.getDocumentCatalog().getMetadata()); this.doc.getDocumentCatalog().setPageLabels(pDDocument.getDocumentCatalog().getPageLabels()); this.doc.getDocumentCatalog().setViewerPreferences(pDDocument.getDocumentCatalog().getViewerPreferences()); this.doc.getDocumentCatalog().setPageMode(pDDocument.getDocumentCatalog().getPageMode()); this.doc.getDocumentCatalog().setPageLayout(pDDocument.getDocumentCatalog().getPageLayout()); this.doc.save(this.tempFileName); this.doc.close(); this.doc = PDDocument.load(new java.io.File(this.tempFileName)); pDDocument.close(); pDDocument = null; try { Assembly executingAssembly = Assembly.GetExecutingAssembly(); Stream manifestResourceStream = executingAssembly.GetManifestResourceStream("Aquaforest.PDF.sRGB_IEC61966-2-1_black_scaled.icc"); using (FileStream fileStream = System.IO.File.Create("sRGB_IEC61966-2-1_black_scaled.icc", (int)manifestResourceStream.Length)) { byte[] numArray = new byte[checked (manifestResourceStream.Length)]; manifestResourceStream.Read(numArray, 0, (int)numArray.Length); fileStream.Write(numArray, 0, (int)numArray.Length); } manifestResourceStream = executingAssembly.GetManifestResourceStream("Aquaforest.PDF.font.xml"); using (FileStream fileStream1 = System.IO.File.Create("font.xml", (int)manifestResourceStream.Length)) { byte[] numArray1 = new byte[checked (manifestResourceStream.Length)]; manifestResourceStream.Read(numArray1, 0, (int)numArray1.Length); fileStream1.Write(numArray1, 0, (int)numArray1.Length); } } catch { } this.iccString = "sRGB_IEC61966-2-1_black_scaled.icc"; } catch (InvalidPasswordException invalidPasswordException) { Console.WriteLine("The Password Provided is Invalid."); Environment.Exit(104); } catch (Exception exception) { Console.WriteLine(exception.Message); Environment.Exit(104); } }