/// <summary> /// Checks to see if a file has a user password. /// </summary> /// <param name="fileName">File to check</param> /// <returns>true if the PDF file is encrypted; otherwise, it is false</returns> public static bool IsEncrypted(string fileName) { var fileType = PDFFile.GetPDFFileType(fileName, true); switch (fileType) { case PDFFileType.Unknown: case PDFFileType.EncapsulatedPostscript: case PDFFileType.Postscript: Console.WriteLine("Not a valid PDF file"); return(false); case PDFFileType.PDF17: case PDFFileType.PDF16: case PDFFileType.PDF15: case PDFFileType.PDF14: case PDFFileType.PDF13: case PDFFileType.PDF12: case PDFFileType.PDF11: case PDFFileType.PDF10: return(PDFFile.IsEncrypted(fileName)); default: throw new ArgumentOutOfRangeException(); } }
private void GetFileType() { _fileTypeLabel.Visible = true; Application.DoEvents(); using (WaitCursor wait = new WaitCursor()) { PDFFileType fileType = PDFFile.GetPDFFileType(_fileName, true); _fileTypeValueLabel.Visible = true; _fileTypeValueLabel.Text = DemosGlobalization.GetResxString(GetType(), "resx_Finished"); Application.DoEvents(); } }
public void SetFileProperties(PDFFile document) { try { _isEncryptedTextBox.Text = PDFFile.IsEncrypted(document.FileName) ? "Yes" : "No"; } catch { _isEncryptedTextBox.Text = "No"; } try { _versionTextBox.Text = GetPDFFileTypeName(PDFFile.GetPDFFileType(document.FileName, true)); } catch { _versionTextBox.Text = GetPDFFileTypeName(PDFFileType.PDF14); } _numberOfPagesTextBox.Text = string.Format("{0} pages", document.Pages.Count); double inchesWidth = document.Pages[0].Width / 72.0; double inchesHeight = document.Pages[0].Height / 72.0; _pageSizeTextBox.Text = string.Format("{0} by {1} inches", inchesWidth, inchesHeight); try { _isLinearizedTextBox.Text = PDFFile.IsLinearized(document.FileName, null) ? "Yes" : "No"; } catch { _isLinearizedTextBox.Text = "No"; } }
private PDFFile BrowsePDFDocument(bool pdfOnly, out bool isPostscript) { isPostscript = false; string fileName = RunPDFOpenFileDialog(pdfOnly); if (fileName == null) { return(null); } try { PDFFileType fileType = PDFFile.GetPDFFileType(fileName, pdfOnly); if (fileType == PDFFileType.Unknown) { if (pdfOnly) { Messager.ShowError(this, string.Format("Not a valid PDF file.\n\n{0}", fileName)); } else { Messager.ShowError(this, string.Format("Not a valid PDF or Postscript file.\n\n{0}", fileName)); } return(null); } if (fileType == PDFFileType.Postscript || fileType == PDFFileType.EncapsulatedPostscript) { isPostscript = true; } if (!isPostscript) { string password = null; // Check if it is encrypted if (PDFFile.IsEncrypted(fileName)) { using (GetPasswordDialog dlg = new GetPasswordDialog()) { if (dlg.ShowDialog(this) == DialogResult.OK) { password = dlg.Password; } else { return(null); } } } using (WaitCursor wait = new WaitCursor()) { PDFFile document = new PDFFile(fileName, password); document.Load(); return(document); } } else { PDFFile document = new PDFFile(fileName); return(document); } } catch (Exception ex) { Messager.ShowError(this, ex.Message); } return(null); }
protected override void OnLoad(EventArgs e) { if (!DesignMode) { try { PDFDocumentProperties pdfProperties; string yes = DemosGlobalization.GetResxString(GetType(), "resx_Yes"); if (string.IsNullOrEmpty(yes)) { yes = "Yes"; } string no = DemosGlobalization.GetResxString(GetType(), "resx_No"); if (string.IsNullOrEmpty(no)) { no = "No"; } if (_document != null) { _fileNameTextBox.Text = _document.FileName; _isEncryptedTextBox.Text = _document.IsEncrypted ? yes : no; _isLinearizedTextBox.Text = _document.IsLinearized ? yes : no; _isPdfATextBox.Text = _document.IsPdfA ? yes : no; _versionTextBox.Text = GetPDFFileTypeName(_document.FileType); _numberOfPagesTextBox.Text = string.Format("{0} {1}", _document.Pages.Count, DemosGlobalization.GetResxString(GetType(), "resx_Pages")); double inchesWidth = _document.Pages[0].WidthInches; double inchesHeight = _document.Pages[0].HeightInches; _pageSizeTextBox.Text = string.Format("{0} {1} {2} {3}", inchesWidth, DemosGlobalization.GetResxString(GetType(), "resx_By"), inchesHeight, DemosGlobalization.GetResxString(GetType(), "resx_Inches")); pdfProperties = _document.DocumentProperties; } else { _fileNameTextBox.Text = _file.FileName; _isEncryptedTextBox.Text = PDFFile.IsEncrypted(_file.FileName) ? yes : no; try { _isLinearizedTextBox.Text = PDFFile.IsLinearized(_file.FileName, null) ? yes : no; } catch { _isLinearizedTextBox.Text = no; } _isPdfATextBox.Text = PDFFile.IsPdfA(_file.FileName) ? yes : no; _versionTextBox.Text = GetPDFFileTypeName(PDFFile.GetPDFFileType(_file.FileName, true)); _numberOfPagesTextBox.Text = string.Format("{0} pages", _file.Pages.Count); double inchesWidth = _file.Pages[0].Width; double inchesHeight = _file.Pages[0].Height; _pageSizeTextBox.Text = string.Format("{0} {1} {2} {3}", inchesWidth, DemosGlobalization.GetResxString(GetType(), "resx_By"), inchesHeight, DemosGlobalization.GetResxString(GetType(), "resx_Inches")); pdfProperties = _file.DocumentProperties; } _documentPropertiesControl.SetDocumentProperties(pdfProperties, true); } catch (Exception ex) { Messager.ShowError(this, ex); } } base.OnLoad(e); }