Showing posts with label iTextSharp. Show all posts
Showing posts with label iTextSharp. Show all posts

Wednesday, 16 December 2015

Password protected PDF using iTextSharp

        private void ProtectPDF()
        {
            string InputFile = @"~\NewFolder1\Worksheet.pdf";
            string OutputFile = @"~\NewFolder1\Confidential.pdf";

            using (Stream input = new FileStream(InputFile, FileMode.Open
, FileAccess.Read
, FileShare.Read))
            {
                using (FileStream output = new FileStream(OutputFile
 , FileMode.Create
 , FileAccess.Write
, FileShare.None))
                {
                    PdfReader reader = new PdfReader(input);
                    PdfEncryptor.Encrypt(reader, output, true
, "Your Password"
, "owner password"
, PdfWriter.ALLOW_SCREENREADERS);
                }
            }
        }

Tuesday, 25 February 2014

Read content of pdf using iTextSharp

How to read content from pdf file ?
Here is little code to do this stuff.

using iTextSharp.text.pdf.parser;
using System.Text;

namespace TestApplication
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string Path = @"C:\Users\Ajay\Downloads\Anish jesani.pdf";

            iTextSharp.text.pdf.PdfReader pr = new iTextSharp.text.pdf.PdfReader(Path);
            ITextExtractionStrategy pes = new SimpleTextExtractionStrategy();
            int pageCnt = pr.NumberOfPages; // get number of Pages
            string str = PdfTextExtractor.GetTextFromPage(pr, 1);    // 1 = Page number

            str = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default,
 Encoding.UTF8, Encoding.Default.GetBytes(str)));
        }
    }
}

This is done using iTextSharp.dll