Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PDF
Hello Friends,
 
I create a pdf reader using Itextsharp in C#.net windows application, and open successfully pdf file in the Richtextbox but this file open not in a actual file data these are open into other language like that compiler language or any other format.
 
Please solve my problem.
 
I have use this code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace rifakpdfcreator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public ArrayList RichTextBoxes;
        int d = 80;
        int i = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
 
           // int c = 40;
            this.RichTextBoxes = new ArrayList();
            this.RichTextBoxes.Add(new RichTextBox());
            ((RichTextBox)this.RichTextBoxes[i]).Location = new System.Drawing.Point(120, d);
            ((RichTextBox)this.RichTextBoxes[i]).Name = "RichTextBoxName" + i.ToString();
            ((RichTextBox)this.RichTextBoxes[i]).Size = new System.Drawing.Size(1000,600);
            panel2.Controls.Add(((RichTextBox)this.RichTextBoxes[i]));
            ((RichTextBox)this.RichTextBoxes[i]).ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
            ((RichTextBox)this.RichTextBoxes[i]).TextChanged += new EventHandler(Form1_TextChanged);
            ((RichTextBox)this.RichTextBoxes[i]).ContextMenuStrip = contextMenuStrip1;
          
            
        }
 
        private void Form1_TextChanged(object sender, EventArgs e)
        {
            int c = 120;
 
            Point pos = new Point(0, 0);
            pos.X = ClientRectangle.Width;
            pos.Y = ClientRectangle.Height;
            RichTextBox aa = ((RichTextBox)this.RichTextBoxes[i]);
            int a = aa.GetCharIndexFromPosition(pos);
            int line = aa.GetLineFromCharIndex(a);
 

            if (line > 40)
            {
                i = i + 1;
                this.RichTextBoxes.Add(new RichTextBox());
                ((RichTextBox)this.RichTextBoxes[i]).Location = new System.Drawing.Point(c, d + 620);
                ((RichTextBox)this.RichTextBoxes[i]).Name = "RichTextBoxName" + i.ToString();
                ((RichTextBox)this.RichTextBoxes[i]).Size = new System.Drawing.Size(1000,600);
                panel2.Controls.Add(((RichTextBox)this.RichTextBoxes[i]));
                RichTextBox ab = ((RichTextBox)this.RichTextBoxes[i]);
                ((RichTextBox)this.RichTextBoxes[i]).Focus();
                ((RichTextBox)this.RichTextBoxes[i]).ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
                ((RichTextBox)this.RichTextBoxes[i]).TextChanged += new EventHandler(Form1_TextChanged);
            }
        }
 
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
 
        }
              
        
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((RichTextBox)this.RichTextBoxes[i]).Copy();
        }
 
        
        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((RichTextBox)this.RichTextBoxes[i]).SelectAll();
        }
 
        private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            float zoom = ((RichTextBox)this.RichTextBoxes[i]).ZoomFactor;
            if (zoom * 2 < 64)
                ((RichTextBox)this.RichTextBoxes[i]).ZoomFactor = zoom * 2;
            ((RichTextBox)this.RichTextBoxes[i]).Focus();
        }
 
        private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            float zoom = ((RichTextBox)this.RichTextBoxes[i]).ZoomFactor;
            if (zoom / 2 > 0.015625)
                ((RichTextBox)this.RichTextBoxes[i]).ZoomFactor = zoom / 2;
            ((RichTextBox)this.RichTextBoxes[i]).Focus();
        }
 
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult drs = openFileDialog1.ShowDialog();
            openFileDialog1.Filter = "Pdf File|*.pdf";
            openFileDialog1.Title = "Open pdf file";
 
            if (drs == DialogResult.OK)
            {
                string file = openFileDialog1.FileName;
                byte[] byt = File.ReadAllBytes(file);
                ((RichTextBox)this.RichTextBoxes[i]).Text = Convert.ToBase64String(byt);
                
 
            }
        }
 
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            
            saveFileDialog1.DefaultExt = ".pdf";
            saveFileDialog1.Title = "Save";
            saveFileDialog1.Filter = "Pdf file|*.pdf";
            saveFileDialog1.FileName = "Name";
            saveFileDialog1.ShowDialog();
            string str = saveFileDialog1.FileName;
            Document myDocument = new Document(PageSize.A4.Rotate());
            PdfWriter.GetInstance(myDocument, new FileStream(str, FileMode.Create));
            myDocument.Open();
            string file = ((RichTextBox)this.RichTextBoxes[i]).Text;
            myDocument.Add(new Paragraph(file));
            myDocument.Close();
        }
 
out put is
i open a pdf file which text is abhishek tyagi
but the file open is text is (JVBERi0xLjQKJeLjz9MKMiAwIG9iago8PC9MZW5ndGggODMvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KeJwzUDBU0DVUMFAwtTQFksm5XIVcTiFcxmYKpqaWCiEpXAZAaQsQQ9/NUMHQSCEkjUsjMSkjszgjNVuhpDIxPVMzJAuoygCkxjWEK5ALAO+3E1EKZW5kc3RyZWFtCmVuZG9iago0IDAgb2JqCjw8L1R5cGUvUGFnZS9NZWRpYUJveFswIDAgNTk1IDg0Ml0vUmVzb3VyY2VzPDwvUHJvY1NldCBbL1BERiAvVGV4dCAvSW1hZ2VCIC9JbWFnZUMgL0ltYWdlSV0vRm9udDw8L0YxIDEgMCBSPj4+Pi9Sb3RhdGUgOTAvQ29udGVudHMgMiAwIFIvUGFyZW50IDMgMCBSPj4KZW5kb2JqCjEgMCBvYmoKPDwvVHlwZS9Gb250L1N1YnR5cGUvVHlwZTEvQmFzZUZvbnQvSGVsdmV0aWNhL0VuY29kaW5nL1dpbkFuc2lFbmNvZGluZz4+CmVuZG9iagozIDAgb2JqCjw8L1R5cGUvUGFnZXMvQ291bnQgMS9LaWRzWzQgMCBSXS9JVFhUKDUuMS4yKT4+CmVuZG9iago1IDAgb2JqCjw8L1R5cGUvQ2F0YWxvZy9QYWdlcyAzIDAgUj4+CmVuZG9iago2IDAgb2JqCjw8L1Byb2R1Y2VyKGlUZXh0U2hhcnAgNS4xLjIgXChjXCkgMVQzWFQgQlZCQSkvQ3JlYXRpb25EYXRlKEQ6MjAxMTExMjUwNzA4MDItMDgnMDAnKS9Nb2REYXRlKEQ6MjAxMTExMjUwNzA4MDItMDgnMDAnKT4+CmVuZG9iagp4cmVmCjAgNwowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAzMzEgMDAwMDAgbiAKMDAwMDAwMDAxNSAwMDAwMCBuIAowMDAwMDAwNDE5IDAwMDAwIG4gCjAwMDAwMDAxNjQgMDAwMDAgbiAKMDAwMDAwMDQ4MiAwMDAwMCBuIAowMDAwMDAwNTI3IDAwMDAwIG4gCnRyYWlsZXIKPDwvU2l6ZSA3L1Jvb3QgNSAwIFIvSW5mbyA2IDAgUi9JRCBbPDU4YWIxNmIyMjljMDYxMzdjYThjMzZhMjY0ZDY1ZmUzPjw3ZTBjMjAxZDQyYWJiYTQ1ZDVjNmMyOTYzMmFlNzNlMj5dPj4Kc3RhcnR4cmVmCjY2MgolJUVPRgo)
Posted 25-Nov-11 1:11am
Edited 25-Nov-11 2:42am
v3
Comments
Richard MacCutchan at 25-Nov-11 7:34am
   
Your question is not clear, please explain where and what is the problem.
RaviRanjankr at 25-Nov-11 15:54pm
   
Please be more specific while asking question. we need more information about your question.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your comment is still not very clear. however, I assume you are referring to the following lines of code:
                string file = openFileDialog1.FileName;
                byte[] byt = File.ReadAllBytes(file);
                ((RichTextBox)this.RichTextBoxes[i]).Text = Convert.ToBase64String(byt);
You are reading the raw bytes of a PDF file and passing it to a RichTextBox[^]. This will not work as the two are not directly compatible. You need to use iTextSharp (or similar) to read the data content of the PDF file, and present it in a form acceptable to the RichtextBox.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 Richard MacCutchan 310
2 OriginalGriff 290
3 Sharmanuj 146
4 Jared Sanow 145
0 Sergey Alexandrovich Kryukov 5,969
1 OriginalGriff 5,005
2 CPallini 2,473
3 Richard MacCutchan 1,547
4 Abhinav S 1,507


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 25 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100