C# tutorial: add content to an existing PDF document


Add content to an existing PDF document

In some situations, you might want to add content to an existing PDF document. In this tutorial, I am going to show how to modify an existing PDF document by adding more content to its pages. iTextSharp libray assists you to accomplish this task through the use of the PdfStamper class. You can get PdfContentByte object (used to add content to the PDF pages) from the PdfStamper class by using the GetContentOver(int page), and GetContentUnder(int page) mehtod. The GetContentOver(int page) allows you to add content over the original content of a page while the GetContentUnder helps you to add content under the original content of the page. The example code below reads two pages from the iTextAction.pdf file. You will use the SelectPage method of the PdfReader class to select some pages from the original PDF document. The text "Hello World" is added to the first page in left alignment at the coordinate x=100, y=400 without rotation. This text is over the original content of the page. An image object read from a file is also added to the page under the original content at the coordinate x=0, y=0. It is The result PDF file is stored in the pdfcontentadded.pdf file.



//variables
String pathin = "d:/iTextAction.pdf";
String pathout = "d:/pdfcontentadded.pdf";
//create a document object
//var doc = new Document(PageSize.A4);
//create PdfReader object to read from the existing document
PdfReader reader = new PdfReader(pathin);
//select two pages from the original document
reader.SelectPages("1-2");
//create PdfStamper object to write to get the pages from reader
PdfStamper stamper=new PdfStamper(reader, new FileStream(pathout, FileMode.Create));
// PdfContentByte from stamper to add content to the pages over the original content
PdfContentByte pbover = stamper.GetOverContent(1);
//add content to the page using ColumnText
ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase("Hello World"), 100, 400, 0);
// PdfContentByte from stamper to add content to the pages under the original content
PdfContentByte pbunder = stamper.GetUnderContent(1);
//add image from a file
iTextSharp.text.Image img = new Jpeg(imageToByteArray(System.Drawing.Image.FromFile("d://baby.jpg")));
//add the image under the original content
pbunder.AddImage(img, img.Width / 2, 0, 0, img.Height / 2, 0,0);
//close the stamper
stamper.Close();

add text and image to exising pdf page

You can download the complete C# code of this tutorial from here.




Comments

zoedtdt comment

 zoedtdt

Here is the link for add text field to pdf. Hope this gives you a start for you ppt to adding text boxes to pdf form on pdfcoding.com page http://www.pdfcoding.com/online/pdf/convert-pdf-to-text-file/.


2016-08-02
lorretadt comment

 lorretadt

Try the vb.net add editable text box to pdf directly and easily with high quality on rasteredge page http://www.rasteredge.com/how-to/vb-net-imaging/pdf-annotate-text/


2016-05-26
lorretadt comment

 lorretadt

vb.net add editable text box to pdf can be download on rasteredge and create the best product for your product, try this vb.net add editable text box to pdf reader pages in pdf

for free on page rasteredge http://www.rasteredge.com/how-to/vb-net-imaging/pdf-text-edit-insert/


2016-05-23
Sinan Basalan comment

 Sinan Basalan

This tutorial worked well for me. Thanks a lot


2016-05-21
lorretadt comment

 lorretadt

you may test c# add editable text box to pdf on rasteredge and download this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text/



2016-05-03
lorretadt comment

 lorretadt

you may test c# add editable text box to pdf on rasteredge and download this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text/



2016-05-03
lorretadt comment

 lorretadt

c# add text pdf file
can be done on this page http://www.rasteredge.com/how-to/csharp-imaging/pdf-text-edit-insert/


2016-04-19
atifyah@gmail.com comment

 atifyah@gmail.com

hi,I have written this code for "Add content to an existing PDF document"..but it is not working.can u help me pls'

string sourcePdfPath = Server.MapPath("~/Documents/jQuery DataTables and ASP.pdf");
string outputPdfPath = Server.MapPath("~/OutPut/Sessions and Shopping Carts.pdf");
reader = new PdfReader(sourcePdfPath);
reader.SelectPages("1-2");
PdfStamper stamper = new PdfStamper(reader, new FileStream(outputPdfPath, FileMode.Create));
PdfContentByte pbover = stamper.GetOverContent(1);
ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase("Hello World"), 100, 400, 0);
PdfContentByte pbunder = stamper.GetUnderContent(1);
iTextSharp.text.Image img = new Jpeg(imageToByteArray(System.Drawing.Image.FromFile(Server.MapPath("~/Images/lee.jpg"))));
pbunder.AddImage(img, img.Width / 2, 0, 0, img.Height / 2, 0, 0);
stamper.Close();


2015-10-14
atifyah@gmail.com comment

 atifyah@gmail.com

hi,I have written this code for "Add content to an existing PDF document"..but it is not working.can u help me pls'

string sourcePdfPath = Server.MapPath("~/Documents/jQuery DataTables and ASP.pdf");
string outputPdfPath = Server.MapPath("~/OutPut/Sessions and Shopping Carts.pdf");
reader = new PdfReader(sourcePdfPath);
reader.SelectPages("1-2");
PdfStamper stamper = new PdfStamper(reader, new FileStream(outputPdfPath, FileMode.Create));
PdfContentByte pbover = stamper.GetOverContent(1);
ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase("Hello World"), 100, 400, 0);
PdfContentByte pbunder = stamper.GetUnderContent(1);
iTextSharp.text.Image img = new Jpeg(imageToByteArray(System.Drawing.Image.FromFile(Server.MapPath("~/Images/lee.jpg"))));
pbunder.AddImage(img, img.Width / 2, 0, 0, img.Height / 2, 0, 0);
stamper.Close();


2015-10-14
Aditya comment

 Aditya

It's really world's best learning Center. Concepts are taught in a simple way. Thanks a lot.


2015-05-20



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.