C# tutorial: PDF print button

Add a print button to a PDF document

Having a clickable button in a PDF document is possible in iTextSharp. A clickable button is an interactive form field of a PDF. In this tutorial, I am going to show you how to add a print button to an existing document. When it is clicked, the print dialog will be opened. To accomplish the task, first you need to create an instance of the PushbuttonField class. This class allows you to define the layout of the button. For instance, you can set text, color of text, background color, image, and layout of the button. You can create a button with a layout that only contain text, icon. It is also able to show both text and icon. You also can specify the horizontal and vertical aligments of the text and icon. Once you're done, you use Field property to obtain the corresponding PdfAnnotation object, and you can add a JavaScript code to trigger an action when the button is pushed.

The JavaScript method execMenuItem() executes a menu item in Foxit Reader or Adobe Reader. In this case, clicking the button will have the same effect as if the user selected File > Print.

The example below adds a print button that contains both text and image to the botton-left corner of very page of the PDF document read from a source file called oop-software-development.pdf.

//path to source file
String source = "d:/oop-software-development.pdf";
//create PdfReader object to read the source file
PdfReader reader = new PdfReader(source);
//create PdfStamper object to modify the content of the PDF
PdfStamper stamp = new PdfStamper(reader, new FileStream("d:/outfile.pdf", FileMode.Create));
//create a Rectangle object to deine the size and location of the button
Rectangle rect = new Rectangle(10, 30, 50, 50);
//create PushbuttonField object
PushbuttonField printbt = new PushbuttonField(stamp.Writer, rect, "Print");
//set button properties
printbt.BorderColor = BaseColor.BLACK;
printbt.Text = "Print";
printbt.TextColor = BaseColor.WHITE;
printbt.BackgroundColor = BaseColor.BLUE;
printbt.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
printbt.Image = Image.GetInstance("d:/saveicon.png");
//get PdfAnnotation object
PdfAnnotation printButton = printbt.Field;
//set button's action using JavaScript code
printButton.Action = PdfAction.JavaScript("app.execMenuItem('Print')", stamp.Writer);
//loop through the document and add the annotation to every page
for (int page = 1; page <= reader.NumberOfPages; page++)
stamp.AddAnnotation(printButton, page);

print button in pdf


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.