본문 바로가기

카테고리 없음

Itextsharp Rectangle On Existing Pdf



27 Nov 2013CPOL
Create/Read/Write Advance PDF Report using iTextSharp.DLL in Desktop, Mobile, Web Application

Contents


    The following two contents are added in this updated version:

How to replace specific word in pdf using itextsharp C#.net. //Create a PDF from existing and with a template private void CreatePDFTemplateMSnew(string strSource) string oldFile = strSource. How to find and replace specific word in pdf using itextsharp c#.net throghout the pdf.

Introduction

Recently I was looking for an Advance Tool to create complex PDF Report in C#.Net and I found iTextSharp. The main problem is that lacks of Documentation. Yes, there are a few examples in C#, but that is not sufficient to the Beginners and the examples are in older version of iTextSharp, there are lots of change in the latest version. So, it will be difficult for beginners to convert older version to latest version. Besides, I think that if I write an Article on this, then it will help me also as a future reference as I'll describe each functionality of this library with example. Frankly speaking, in this Article, you find all the examples from Chapter 1 of the book iText in Action, Second Edition which is written for Java Developers. I'll explain all the examples of the rest Chapters in my next release of this Article. So if any one is interested in using this library, he/she will get a good kick start.

To know the history or any other details of iTextSharp, please go through the Official Website

Requirements

  • To compile this library, you need a C# 2008 compiler or better, Visual Studio 2008 or Visual C# 2008 Express Edition
  • This library code will run in:
    • .NET 2.0
    • .NET 3.0
    • .NET 3.5
    • .NET 4.0
    • .NET 4.0 Client Profile
    • .NET 4.5
    • .NET 4.5 Client Profile

Installation

  • Simply install the NuGet package

Or you can download the library DLL from the above link or from the SourceForge. Then do the following:

  • Add the following library into your project by adding reference to that library DLL. Just see the image below:
  • The necessary namespace that you have to add into your C# file to do what are in this Article are::
    • iTextSharp.text
    • iTextSharp.text.pdf

Quick Start

Itext Draw Rectangle On Existing Pdf

Creating PDF Document in 6 Steps:

  • Step 1: Create a System.IO.FileStream object:
  • Step 2: Create a iTextSharp.text.Document object:
  • Step 3: Create a iTextSharp.text.pdf.PdfWriter object. It helps to write the Document to the Specified FileStream:
  • Step 4: Openning the Document:
  • Step 5: Adding a Paragraph by creating a iTextSharp.text.Paragraph object:
  • Step 6: Closing the Document:

Working with Page Size of PDF Document:

Creating a Page of specified size, we must have to create a iTextSharp.text.Rectangle object and Passing the size as argument to its constructor. There are a few way to define Page Size:

MobaXterm X server and SSH clientMobaXterm is your ultimate toolbox for remote computing. In a single Windows application, it provides loads of functions that are tailored for programmers, webmasters, IT administrators and pretty much all users who need to handle their remote jobs in a more simple fashion.MobaXterm provides all the important remote network tools (SSH, X11, RDP, VNC, FTP, MOSH.) and Unix commands (bash, ls, cat, sed, grep, awk, rsync.) to Windows desktop, in a single portable exe file which works out of the box.There are many advantages of having an All-In-One network application for your remote tasks, e.g. If you want to use it inside your company, you should consider subscribing to MobaXterm Professional Edition: this will give you access to much more features, professional support and 'Customizer' software.When developing MobaXterm, we focused on a simple aim: proposing an intuitive user interface in order for you to efficiently access remote servers through different networks or systems.MobaXterm is being actively developed and frequently updated. Xterm for windows 10 download. Your remote applications will also display seamlessly on your Windows desktop using the embedded X server.You can download and use MobaXterm Home Edition for free. When you use SSH to connect to a remote server, a graphical SFTP browser will automatically pop up in order to directly edit your remote files.

  • First Way to define Page Size:
    Creating Page Size by Pixels or Inch. NOTE: In iTextSharp library, unit is 'point'. 72 points = 1 inch. Suppose we want to create a PDF File of width = 2 inch & height = 10 inch, then we need 144pt for 2 inch & 72pt for 10 inch. Lets see, how to do this:
  • Second Way to define Page Size:
    Taking Page Size from in-built iTextSharp.text.PageSize class:
    The following are the Supported in-built Page Size. Read the full Documentation of Page Size:
    • _11X17
    • A0
    • A1
    • A10
    • A2
    • A3
    • A4
    • A4_LANDSCAPE
    • A5
    • A6
    • A7
    • A8
    • A9
    • ARCH_A
    • ARCH_B
    • ARCH_C
    • ARCH_D
    • ARCH_E
    • B0
    • B1
    • B10
    • B2
    • B3
    • B4
    • B5
    • B6
    • B7
    • B8
    • B9
    • CROWN_OCTAVO
    • CROWN_QUARTO
    • DEMY_OCTAVO
    • DEMY_QUARTO
    • EXECUTIVE
    • FLSA
    • FLSE
    • HALFLETTER
    • ID_1
    • ID_2
    • ID_3
    • LARGE_CROWN_OCTAVO
    • LARGE_CROWN_QUARTO
    • LEDGER
    • LEGAL
    • LEGAL_LANDSCAPE
    • LETTER
    • LETTER_LANDSCAPE
    • NOTE
    • PENGUIN_LARGE_PAPERBACK
    • PENGUIN_SMALL_PAPERBACK
    • POSTCARD
    • ROYAL_OCTAVO
    • ROYAL_QUARTO
    • SMALL_PAPERBACK
    • TABLOID
  • Third Way to define Page Size:
    Rotating Document i.e. height becomes width & vice-versa:

Now, just pass this iTextSharp.text.Rectangle object (any one) i.e. either 'rec', or 'rec2' or 'rec3' to the iTextSharp.text.Document's constructor during object creation like below:

Setting Background Color of PDF Document:

There are a few ways to set background color:

  • First Way to Set Background Color:
    It takes the object of iTextSharp.text.BaseColor. BaseColor constructor takes in-built System.Drawing.Color object Or you can pass RGB values to the constructor in different forms:
  • Second Way to Set Background Color:
    It takes the object of iTextSharp.text.pdf.CMYKColor. CMYKColor constructor takes only CMYK values in different forms:

Setting Page Margins of PDF Document:

Pdf

Margins can be set during Document object creation like Page Size
Suppose we set the margins as below:

  • Left Margin: 0.5 inch
  • Right Margin: 1 inch
  • Top Margin: 1.5 inch
  • Bottom Margin: 2.5 inch

So, we need to do set the following points for the Left, Right, Top, Bottom Margins respectively as we already know that iTextSharp library only understand points where 72 points = 1 inch.

  • Left Margin: 36pt => 0.5 inch
  • Right Margin: 72pt => 1 inch
  • Top Margin: 108pt => 1.5 inch
  • Bottom Margini: 180pt => 2.5 inch

Lets implement:

Setting Text Alignment in PDF Document:

Alignment is one of the property of iTextSharp.text.Paragraph's object. iTextSharp Library provides various types of Alignments. These Alignments can be access through iTextSharp.text.Element class. The following are Alignment types provides iTextSharp:

And its use by you is covered under the terms of a license agreement. Small caps font download. LicenseNOTIFICATION OF LICENSE AGREEMENTThis typeface is the property of Monotype Imaging Inc. You may not copy or distribute this font software. If you have any questions regarding your license terms, please review the license agreement you received with the software.General license terms and usage rights can be viewed at www.linotype.com/license.Generelle Lizenzbedingungen und Nutzungsrechte finden Sie unter www.linotype.com/license.Pour plus d'informations concernant le contrat d'utilisation du logiciel de polices, veuillez consulter notre site web www.linotype.com/license.Linotype GmbH can be contacted at:Tel.: +49(0)6172 484-418.

  • [^]
  • [^]
  • [^]
  • [^]
  • [^]
  • [^]
  • [^]
  • [^]
  • [^]
  • [^]

As we already see the iTextSharp.text.Document's constructor takes iTextSharp.text.Paragraph's object during Document creation. So, after creating the Paragraph object and setting Alignment property, we can pass this object to iTextSharp.text.Document's constructor during Document ceration. Lets implement:

Setting Meta Information or Properties of a PDF Document:

The following Meta information of PDF Document, you can set through iTextSharp.text.Document's methods by creating its object i.e. doc here:

  • Author Name[^]
  • Creation Date[^]
  • Creator Name[^]
  • Header Name & Content[^]
  • Keywords[^]
  • Langugae[^]
  • Producer[^]
  • Subject[^]
  • Title[^]

Lets implement a few of them:

Now after openning this PDF file, right click over it and click Properties, then you'll see the following:

Creating a Multipage Document:

We can create a new page through iTextSharp.text.Document's NewPage() method by creating its object. Lets add five pages in PDF Document:

Creating a New PDF Document from an existing PDF Document:

We can read from a PDF Document using iTextSharp.text.pdf.PdfReader's object and write it to another PDF Document using iTextSharp.text.pdf.PdfStamper's object. Lets implement:

Adding Watermark to PDF Document using Layer:

Watermark can be add after creating the PDF Dodument in iTextSharp Library. Ralink 802.11 n wlan driver windows 7 64 bit. Here I'll add Watermark to existing PDF Document i.e. Original.pdf, through creating a iTextSharp.text.pdf.PdfLayer object. Lets implement:

The created PDF Document is below:

Removing Watermark from the just created Watermarked Document by Removing Layer:

Whenever we add Layer in PDF Document, then the content of the Layer resides under OCG Group. So if I remove this Layer we can remove the content of the Layer also e.g. here it is Watermark Text. To remove all the Layers from PDF Document, you have to remove OCG Group completely from the Document using reader.Catalog.Remove(PdfName.OCPROPERTIES). Now follow the Steps below to remove the Watermark Text from Layer:

  • Read the existing watermarked document using iTextSharp.text.pdf.PdfReader's object
  • Taking each Page in the iTextSharp.text.pdf.PdfDictionary's object using GetPageN(int pageNumber) method of iTextSharp.text.pdf.PdfReader's object.
  • Taking the Content of the Page in the iTextSharp.text.pdf.PdfArray's object using GetAsArray(PdfName.CONTENTS) method of iTextSharp.text.pdf.PdfDictionary's object
  • Loop through this array and Get each element as iTextSharp.text.pdf.PRStream's object using GetAsStream(int arrayIndex) method of iTextSharp.text.pdf.PdfArray's object
  • Convert each stream into Bytes using Static method of iTextSharp.text.pdf.PdfReader class
  • Convert these Bytes into String using method
  • Search for the String '/OC' and also the Watermarked Text. If found then remove it by giving it zero length and zero data using two methods: Put() & SetData() of iTextSharp.text.pdf.PRStream class
  • Write this modified document exists in the reader to a new document using iTextSharp.text.pdf.PdfStamper's object

Lets Implement it:

Adding Watermark to each Page during Creation:

Itextsharp Documentation

Now, we already know that, watermark cannot be add during Page creation, it have to add after document creation. So, I've created a class PDFWriterEvents which implements the interface iTextSharp.text.pdf.IPdfPageEvent and modify the event OnStartPage. This interface contains a set of events from the Openning & to Closing the PDF Document. The events are following:

  • public void OnOpenDocument(PdfWriter writer, Document document)
  • public void OnCloseDocument(PdfWriter writer, Document document)
  • public void OnStartPage(PdfWriter writer, Document document)
  • public void OnEndPage(PdfWriter writer, Document document)
  • public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
  • public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
  • public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition)
  • public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)

You may modify other events accroding to your needs which occured against a particular action. Let see the which I've created:

Lets see how & when you use the object of this class:

See, OnStartPage event called during adding a new paragraph. So I don't need to add watermark later:)

Export/Print/Output the PDF File directly to the Client without saving it to the Disk:

We can create PDF File in memory by creatig System.IO.MemorySystem's object. Lets see:

Setting Viewer Preferences of a PDF Document:

The values of the different ViewerPreferences were originally stored in iTextSharp.text.pdf.PdfWriter class as an integer constant. You can set the ViewerPreferences by following two ways:

  • By setting property ViewerPreferences of iTextSharp.text.pdf.PdfWriter class. To know all the ViewerPreferences and its purpose, please read this first. E.g.-
  • By calling method AddViewerPreference(PdfName key, PdfObject value) of iTextSharp.text.pdf.PdfWriter's object. To know which value is appropiate for which key, read this first. E.g.-

Encrypting a PDF Document:

Itextsharp Pdf Documentation

By SetEncryption() method of iTextSharp.text.pdf.PdfWriter's object, we can encrypt a PDF document. Read full documentation of this method here. To know all the encryption types, click here. E.g.-

Itextsharp Pdf Example

Declaration

References

Itextsharp Rectangle On Existing Pdf File

History

25th Nov, 2013: PART-I Release. PART-II will release soon :)