Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
Gnostice PDFOne .NET is a powerful PDF component library. Some of its components can be used in both desktop and Web applications. Specifically, the PDFDocument
and PDFPrinter
components can do duty as ASP.NET PDF components. As this PDF SDK has been written entirely in managed code, you can use it in limited-trust environments.
Last year, I wrote an article titled Generate PDF Forms In ASP.NET Using PDFOne .NET v3. It focussed on gnerating PDF forms and sending them to the browser. In the current article, the example ASP.NET application code creates other page elements such as text, shapes, annotations, and images. There is also a downloadable Web Demo created by Gnostice DevTools member Snehashree.
The example code in the current article does some active font rendering
and font embedding. To support these tasks in your project, you need to add
the following lines to your project's Web.Config
file.
... <configuration> <appSettings> <add key="FontListFile" value="[path_to_folder_containing]\FontList.xml"/> </appSettings> ...
The FontList.xml
file provides the locations of system
font files. When you install PDFOne .NET, the installation program
creates this file in the Bin
folder of your PDFOne
folder. When you host your PDFOne .NET Web application,
you need to create the FontList.xml file by running the
FontListMakerConsole.exe
on your Web server.
Now, here is the main part of the code-behind. You can add this code
for a button control in a Visual Studio web site project. The code
assumes that you have a image named sample_image.jpg
and text file named sample_doc.txt
in the same folder
as the main application.
protected void Button1_Click(object sender, EventArgs e) { /* * Create a new PDF document */ PDFDocument doc = new PDFDocument("Your-license-key"); // This creates a blank first page automatically. // Make form fields behave doc.ApplyFormAppearances = true; // Prevent PDF from getting launched on the server doc.OpenAfterCreate = false; /* * Render text on first page */ doc.WriteText("1. Sans coordinates, text is begun at the beginning. "); // Render text at the next available location doc.WriteText("2. More text follows where it was left off earlier."); // Set document measurement units doc.MeasurementUnit = PDFMeasurementUnit.Inches; // Break out and render text at x-y location = (1, 1) inches doc.WriteText("3. Write text anywhere on the page.", 1f, 1f); // Write text using a non-embedded font PDFFont fontImpact_NoEmbed = new PDFFont(new Font("Impact", 13f), PDFFontEmbedType.None); doc.WriteText("5. Use fonts to create an impact.", fontImpact_NoEmbed, 1f, 1.5f); // Write text using a subset-embedded font PDFFont fontTahoma_SubsetEmbed = new PDFFont(new Font("Tahoma", 20f), PDFFontEmbedType.Subset); doc.WriteText("6. Embed fonts for hi-fidelity.", fontTahoma_SubsetEmbed, 1f, 2f); // Specify how page elements are stroked and filled doc.Pen.Width = 0.5f; doc.Pen.Color = Color.Red; doc.Brush.Color = Color.Yellow; // Write text using a standard type 1 font - Roman, Helvetica, // Courier, Symbol, Zapf-Dingbat PDFFont fontHelvetica = new PDFFont(StdType1Font.Helvetica, PDFFontStyle.Stroke_And_Fill, 24f); doc.WriteText("7. Standard type 1 fonts have universal support.", fontHelvetica, 1f, 2.5f); /* * Draw shapes */ doc.Pen.Color = Color.Black; doc.Brush.Color = Color.Black; // Draw a line from two points doc.DrawLine(new Point(3,5), new Point(4,4)); doc.WriteText("8. A line", 3, 5); doc.Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; doc.Pen.Color = Color.Gray; // Draw two circles - ostensibly to mark // the ends of the previous line doc.DrawCircle(3, 5, 0.1f, false, true); doc.DrawCircle(4, 4, 0.1f, false, true); doc.Pen.Color = Color.Black; doc.Brush.Color = Color.Black; doc.WriteText("9. A circle", 4, 3.8f); doc.Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; doc.Pen.Color = Color.Black; doc.Brush.Color = Color.Gainsboro; // Draw a pie doc.DrawPie(1, 5, // location 3, 3, // width and height 0, // starting angle -45, // sweep angle true, // fill true); // stroke doc.Brush.Color = Color.Black; doc.WriteText("10. A pie.", 1, 5); /* * Draw image */ doc.DrawImage(Server.MapPath(".") + "\\sample_image.jpg", 5, 4); /* * Add annotations */ PDFTextAnnot PDFTextAnnot1 = new PDFTextAnnot(7f, 3.8f, "11. About this image", "This is an annotation popup window. Yippee!", "PDFOne .NET Web Demo", PDFAnnotFlags.Print, Color.Yellow, PDFTextAnnotIcon.Comment, false); doc.AddAnnot(PDFTextAnnot1); // Add a stamp annotation PDFStampAnnot PDFStampAnnot1 = new PDFStampAnnot(new RectangleF(5, 0.5f, 3, 2), PDFAnnotFlags.NoZoom, Color.Red); PDFStampAnnot1.Stamp = PDFAnnotStamps.Confidential; PDFStampAnnot1.Title = "PDFOne .NET Web Demo"; PDFStampAnnot1.Subject = "12. About this stamp annotation"; PDFStampAnnot1.Content = "Insert state secrets here. Thanks."; doc.AddAnnot(PDFStampAnnot1); /* * Create a new page */ PDFPage page2 = new PDFPage(System.Drawing.Printing.PaperKind.Legal, 1, 1, 0.5f, 0.5f, 0.5f, 0.5f); // Add header and footer page2.AddHeaderText("1. Header Text", fontTahoma_SubsetEmbed, PDFHAlignment.Center, PDFVAlignment.Bottom); page2.AddFooterText("2. www.gnostice.com", fontImpact_NoEmbed, PDFHAlignment.Center, PDFVAlignment.Bottom); /* * Add a file to document as an attachment */ PDFFileAttachmentAnnot attachment1 = new PDFFileAttachmentAnnot( 1, 1, Server.MapPath(".")+ "\\sample_doc.txt"); attachment1.FileAttachmentIcon = PDFFileAttachmentIcon.Paperclip; attachment1.Rectangle = new RectangleF(1, 1, 0.5f, 1); attachment1.Color = Color.Yellow; page2.AddAnnot(attachment1); page2.WriteText("3. Click icon on the left for the attachment", new RectangleF(1.8f, 1, 0.5f, 1)); /* * Add new page to document */ doc.AddPage(page2); /* * Add bookmarks */ PDFBookmark bm; // Add bookmark for page #1 doc.AddBookmark( "Page #1", // Text display the bookmark in the bookmark panel null, // Parent bookmark (null for top-level bookmarks) 1, // Number of the linked page 0, // Height to scroll down automatically from page top edge PDFFit.FitH); // Fit width // Add bookmark for page #2 doc.AddBookmark( "Page #2", null, 2, 0, PDFFit.FitH); // Add a dummy bookmark bm = doc.AddBookmark("Contact Gnostice", "", // Dummy JavaScript script (PDFBookmark) null); // Add URI bookmarks under the dummy bookmark doc.AddBookmark("Website: www.gnostice.com", bm, "http://www.gnostice.com"); doc.AddBookmark( "E-mail: support@gnostice.com", bm, @"mailto:support@gnostice.com?subject=About Newsletter Article" + @"&body=Hi,%0a%0a"); // Traverse the bookmark tree and a new bookmark bm = doc.GetFirstBookmark().GetNext().GetNext(); bm.AddNext("Yet another link to Page 1", 1); /* * Set document information properties */ doc.DocInfo.Title = "PDFOne .NET Web Demo"; doc.DocInfo.Subject = "C# ASP.NET sample for PDFOne .NET"; doc.DocInfo.Author = "V. Subhash"; doc.DocInfo.Creator = "Subhash's PDFOne Web Demo"; /* * Set look and feel */ doc.ViewerPreferences.DisplayDocTitle = true; // Make document use single-page continuous layout doc.PageLayout = PDFPageLayout.OneColumn; // Show bookmarks tab doc.PageMode = PDFPageMode.UseOutlines; /* * Encrypt document */ doc.Security.Enabled = true; doc.Security.OwnerPassword = "Dob Segred"; doc.Security.UserPassword = "easy peasy"; doc.Security.Level = PDFEncryptionLevel.Level128Bit; // Specify user permissions doc.Security.UserPermissions = PDFUserPermissions.None | PDFUserPermissions.ContentAccessibility; doc.Security.Enabled = false; /* * Send PDF document to browser as a download */ Response.Clear(); // Tell server to cache output until asked to flush Response.BufferOutput = true; // Set the mime type - tells the browser that a // PDF document is on the way Response.ContentType = "application/pdf"; // Give the document a name Response.AddHeader( "content-disposition", "attachment; filename=pdfone_doc.pdf"); // Write the document to browser ouput stream doc.Save(Response.OutputStream); // Clean up document instance doc.Close(); doc.Dispose(); // Send output to browser Response.Flush(); // Clean up browser output stream Response.Close(); }
Here is the output.
Downloads:
---o0O0o---
Our .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
Our Delphi/C++Builder developer tools | |
---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
Our Platform-Agnostic Cloud and On-Premises APIs | |
---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved. |