How can I read PDF content with the itextsharp with the Pdfreader. Reading PDF content with itextsharp dll in VB.NET. Reading pdf content using iTextSharp in C#. Converting PDF to Text in C#. name for your application with the latest PDF IFilter implementation that comes with Acrobat Reader. 2. Parsing PDF using iTextSharp. How to read pdf files using C#. Read from a PDF file using C# on Lucian's Weblog shows you. to read the properties of a pdf file using iTextSharp: (1). Create/Read/Write Advance PDF Report using iTextSharp.DLL in Desktop, Mobile, Web Application; Author: Debopam Pal; Updated:; Section: C#; Chapter. ITextSharp is open source PDF solution. using System.Text; using iTextSharp.text.pdf.parser. 12 Responses to iTextSharp β few C# examples. Rotating PDF in C# Using iTextSharp. Upload Sign in Join. Download as PDF, TXT or read online from Scribd. Flag for inappropriate content. Show more Show less. Text. Sharp β few C# examples. Text. Sharp is open source PDF solution. In most of the examples below, I tried to alter,copy a template PDF and then save it into a brand new output PDF file. Itβs easy to work with PDFs , when we have a basic template (created externally using Adobe/Open. Office) in place, β instead of generating a new PDF from scratch. For any data which needs to be modified, I do it in the web page and then save the modified data/results to a pdf file. This prevents the need, for user to modify the pdf form fields. This web link was the basic source, for all my scenarios. Note: The examples in i. Text. Sharp site are in java)using System. System. Collections. Generic. using System. Linq. using System. Web. using System. Web. UI. using System. Web. UI. Web. Controls. System. IO. using i. Text. Sharp. text. System. Data. using System. Text. using i. Text. Sharp. text. pdf. System. util. collections. Text. Sharp. text. System. Net. Mail. PDFScenarios : System. Web. UI. Page. public string P_Input. Stream = "~/My. PDFTemplates/ex. P_Input. Stream. 2 = "~/My. PDFTemplates/Contact. Info. pdf". public string P_Input. Stream. 3 = "~/My. PDFTemplates/Mul. Pages. pdf". public string P_Input. Stream. 4 = "~/My. PDFTemplates/Company. Letter. Head. pdf". P_Output. Stream = "~/My. PDFOutputs/ex. 1_1. Read all 'Form values/keys' from an existing multi- page PDF document. Read. PDFform. Data. Page. Wise(). Pdf. Reader reader = new Pdf. Reader(Server. Map. Path(P_Input. Stream. Acro. Fields form = reader. Acro. Fields. for (int page = 1; page < = reader. Number. Of. Pages; page++). Key. Value. Pair< string, Acro. Fields. Item> kvp in form. Fields). switch (form. Get. Field. Type(kvp. Key)). case Acro. Fields. FIELD_TYPE_CHECKBOX. Acro. Fields. FIELD_TYPE_COMBO. Acro. Fields. FIELD_TYPE_LIST. Acro. Fields. FIELD_TYPE_RADIOBUTTON. Acro. Fields. FIELD_TYPE_NONE. Acro. Fields. FIELD_TYPE_PUSHBUTTON. Acro. Fields. FIELD_TYPE_SIGNATURE. Acro. Fields. FIELD_TYPE_TEXT. Type = form. Get. Field. Type(kvp. Key). Value = form. Get. Field(kvp. Key). string translated. File. Name = form. Get. Translated. Field. Name(kvp. Key). break. Close(). //Read and alter form values for only second and. PDF doc. //Save the changes in a brand new pdf file. Read. Alter. PDFform. Data. In. Selected. Pages(). Pdf. Reader reader = new Pdf. Reader(Server. Map. Path(P_Input. Stream. Select. Pages("1- 2"); //Work with only page# 1 & 2. Pdf. Stamper stamper = new Pdf. Stamper(reader, new File. Stream(Server. Map. Path(P_Output. Stream), File. Mode. Create))). Acro. Fields form = stamper. Acro. Fields. var field. Keys = form. Fields. Keys. foreach (string field. Key in field. Keys). Replace Address Form field with my custom data. Key. Contains("Address")). Set. Field(field. Key, "My. Custom. Address"). //The below will make sure the fields are not editable in. PDF. stamper. Form. Flattening = true. Extract text from an existing PDF's second page. Extract. Text(). Pdf. Reader reader = new Pdf. Reader(Server. Map. Path(P_Input. Stream. Pdf. Text. Extractor. Get. Text. From. Page(reader, 2, new Location. Text. Extraction. Strategy()). return txt. Create a brand new PDF from scratch and without a template. Create. PDFNo. Template(). Document pdf. Doc = new Document(). Pdf. Writer writer = Pdf. Writer. Get. Instance(pdf. Doc, new File. Stream(Server. Map. Path(P_Output. Stream), File. Mode. Open. Or. Create)). Doc. Open(). pdf. Doc. Add(new Paragraph("Some data")). Pdf. Content. Byte cb = writer. Direct. Content. cb. Move. To(pdf. Doc. Page. Size. Width / 2, pdf. Doc. Page. Size. Height / 2). Line. To(pdf. Doc. Page. Size. Width / 2, pdf. Doc. Page. Size. Height). Stroke(). pdf. Doc. Close(). private void fill. PDFForm(). string form. File = Server. Map. Path(P_Input. Stream). File = Server. Map. Path(P_Output. Stream). Pdf. Reader reader = new Pdf. Reader(form. File). Pdf. Stamper stamper = new Pdf. Stamper(reader, new File. Stream(new. File, File. Mode. Create))). Acro. Fields fields = stamper. Acro. Fields. // set form fields. Set. Field("name", "John Doe"). Set. Field("address", "xxxxx, yyyy"). Set. Field("postal_code", "1. Set. Field("email", "johndoe@xxx. Form. Flattening = true. Close(). //Helper functions. Send. Email(Memory. Stream ms). Mail. Address _From = new Mail. Address("XXX@domain. Mail. Address _To = new Mail. Address("YYY@a. com"). Mail. Message email = new Mail. Message(_From, _To). Attachment attach = new Attachment(ms, new System. Net. Mime. Content. Type("application/pdf")). Attachments. Add(attach). Smtp. Client mail. Sender = new Smtp. Client("Gmail- Server"). Sender. Send(email). Download. As. PDF(Memory. Stream ms). Response. Clear(). Response. Clear. Content(). Response. Clear. Headers(). Response. Content. Type = "application/pdf". Response. Append. Header("Content- Disposition", "attachment; filename=abc. Response. Output. Stream. Write(ms. Get. Buffer(), 0, ms. Get. Buffer(). Length). Response. Output. Stream. Flush(). Response. Output. Stream. Close(). Response. End(). ms. Close(). //Working with Memory Stream and PDF. Create. PDFFrom. Memory. Stream(). //(1)using PDFWriter. Document doc = new Document(). Memory. Stream memory. Stream = new Memory. Stream(). Pdf. Writer writer = Pdf. Writer. Get. Instance(doc, memory. Stream). doc. Open(). Add(new Paragraph("Some Text")). Close. Stream = false. Close(). //Get the pointer to the beginning of the stream. Stream. Position = 0. You may use this PDF in memorystream to send as an attachment in an email. OR download as a PDF. Send. Email(memory. Stream). Download. As. PDF(memory. Stream). Another way using Pdf. Stamper. Pdf. Reader reader = new Pdf. Reader(Server. Map. Path(P_Input. Stream. Memory. Stream ms = new Memory. Stream()). Pdf. Stamper stamper = new Pdf. Stamper(reader, ms). Acro. Fields fields = stamper. Acro. Fields. fields. Set. Field("Some. Field", "My. Value. From. DB"). stamper. Form. Flattening = true. Close(). Send. Email(ms). Burst- - Make each page of an existing multi- page PDF document. PDF document. private void PDFBurst(). Template. Path = Server. Map. Path(P_Input. Stream. 3). Pdf. Reader reader = new Pdf. Reader(pdf. Template. Path). //Pdf. Copy copy. Pdf. Smart. Copy copy. Number. Of. Pages; i++). Document d. 1 = new Document(). Pdf. Smart. Copy(d. File. Stream(Server. Map. Path(P_Output. Stream). Replace(". To. String() + ". File. Mode. Create)). Open(). copy. Add. Page(copy. Get. Imported. Page(reader, i)). Close(). //Copy a set of form fields from an existing PDF template/doc. PDF file. //The copied set of fields will have different values. Append. Set. Of. Form. Fields(). Pdf. Copy. Fields _copy = new Pdf. Copy. Fields(new File. Stream(Server. Map. Path(P_Output. Stream), File. Mode. Create)). _copy. Add. Document(new Pdf. Reader(a. 1("1"))). Add. Document(new Pdf. Reader(a. 1("2"))). Add. Document(new Pdf. Reader(new File. Stream(Server. Map. Path("~/My. PDFTemplates/Myaspx. File. Mode. Open))). Close(). //Concatenate. Forms. private byte[] a. To. Append). using (var existing. File. Stream = new File. Stream(Server. Map. Path(P_Input. Stream), File. Mode. Open)). using (Memory. Stream stream = new Memory. Stream()). // Open existing PDF. Reader = new Pdf. Reader(existing. File. Stream). var stamper = new Pdf. Stamper(pdf. Reader, stream). Acro. Fields. var field. Keys = form. Fields. Keys. foreach (string field. Key in field. Keys). Rename. Field(field. Key, field. Key + _To. Append). // "Flatten" the form so it wont be editable/usable anymore. Form. Flattening = true. Close(). pdf. Reader. Close(). return stream. To. Array(). //Working with Image. Add. An. Image(). Pdf. Stream = new File. Stream(@"C: \My. Input. File. Mode. Open)). Image. Stream = new File. Stream(@"C: \img. File. Mode. Open)). Pdf. Stream = new File. Stream(@"C: \My. Output. File. Mode. Create)). Pdf. Reader reader = new Pdf. Reader(input. Pdf. Stream). Pdf. Stamper stamper = new Pdf. Stamper(reader, output. Pdf. Stream). Pdf. Content. Byte pdf. Content. Byte = stamper. Get. Over. Content(1). Text. Sharp. text. Image. Get. Instance(input. Image. Stream). image. Set. Absolute. Position(1, 1). Content. Byte. Add. Image(image). stamper. Close(). //Add Company Letter- Head/Stationary to an existing pdf. Add. Company. Stationary(). Pdf. Reader reader = new Pdf. Reader(Server. Map. Path(P_Input. Stream. Pdf. Reader s_reader = new Pdf. Reader(Server. Map. Path(P_Input. Stream. Pdf. Stamper stamper = new Pdf. Stamper(reader, new File. Stream(Server. Map. Path(P_Output. Stream), File. Mode. Create))). Pdf. Imported. Page page = stamper. Get. Imported. Page(s_reader, 1). Number. Of. Pages. Pdf. Content. Byte background. Get. Under. Content(i). Add. Template(page, 0, 0). Close(). //Create a new PDF document by copying only 2nd page from an existing PDF document. Also add a date on the top- left corner. Add. Text(). //Method 1. Pdf. Reader reader = new Pdf. Reader(Server. Map. Path(P_Input. Stream. Document document = new Document()). Pdf. Writer writer = Pdf. Writer. Get. Instance(document, new File. Stream(Server. Map. Path(P_Output. Stream), File. Mode. Create))). document. Open(). Pdf. Content. Byte cb = writer. Direct. Content. Pdf. Imported. Page page = writer. Get. Imported. Page(reader, 2). New. Page(). cb. Add. Template(page, 0, 0). Add(new Paragraph(Date. Time. Now. To. Short. Date. String())). Close(). //Method 2. Pdf. Reader reader. Pdf. Reader(Server. Map. Path(P_Input. Stream. 3)). using (Pdf. Stamper stamper = new Pdf. Stamper(reader, new File. Stream(Server. Map. Path(P_Output. Stream), File. Mode. Create))). stamper. Rotate. Contents = false. Pdf. Content. Byte canvas = stamper. Get. Over. Content(2). Column. Text. Show. Text. Aligned(canvas, Element. ALIGN_LEFT, new Phrase(Date. Time. Now. To. Short. Date. String()), 0, 0, 0). Close(). //Data. Sheets: Copy 2 pages from one PDF to another brand new PDF. Also alter some form data. Create. PDFBy. Copy(). Document document = new Document()). Pdf. Smart. Copy copy = new Pdf. Smart. Copy(document, new File. Stream(Server. Map. Path(P_Output. Stream), File. Mode. Create))). document. Open(). for (int i = 0; i < 2; ++i). Pdf. Reader reader = new Pdf. Reader(Add. Data. Sheets("Some Text" + i. To. String())). copy. Add. Page(copy. Get. Imported. Page(reader, 1)).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
September 2016
Categories |