Http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm
Author: Tang Xiaoyun/zookeeper University Computer and Information Network Center Programming organization programming Organization
ASP is required for work content. net C # generate a PDF file, but. net Framework does not have the built-in PDF function, so you can only find the function provided by third-party online. Please help me. The search results have been shown in the upper-right corner. In the search results, there are a lot of PDF functions to be paid, so we will exclude them first, and we will search for them as small as free and support Chinese, finally, let me find a very practical and easy-to-use library-itextsharp.
This function is originally called itext and mainly supports Java program statement. Later, we made a version for Microsoft. net c sharp, which is the itextsharp we want to introduce today. The following functions are supported:
1. Create: automate, convert, sign, encrypt.
2. Read: extract.
3. Update: Stamp, fill out, Split/merge, convert, sign, and encrypt.
Next, let me show you how to use a program to generate a PDF file.
Development Environment
1. My ASP. NET development environment is Microsoft Visual Studio 2010, and the program statement used is C #.
2. The current itextsharp version is 5.0.4, the lower limit location: http://sourceforge.net/projects/itextsharp. Please download the ZIP file first, and there is only one DLL after resolving the issue. Use the Add reference method to change itextsharp. DLL is added to the Bin object of your micro-segmentation case. This is so simple and coding can be started.
Part 1: first create a web application
In this example, after using the web application to generate a PDF file in the server memory product, the user can manually download the file or store the file. The usage of the example writer is different:
To generate a PDF file in the server filesystem, you can use the following program:
To display Chinese characters in the PDF file content, the most important thing is the font setting. If there is no correct setting of the Chinese character type, it will cause the problem that Chinese characters cannot be displayed. First, set the basic font: kaiu. TTF is the title Font provided by the operating system. identity_h refers to encoding (the Unicode encoding with horizontal writing) and whether to embed the font into the PDF. Then, the basic fonts will be changed, such as font size, coarse body oblique, and hidden colors. You can also use other Chinese fonts.
After opening the case, the case will be closed.
In this example, you have already created an example (please refer to example.pdf ). The following figure shows the itinerary result:
Select to open the primary keys, which are shown as follows:
Part 2: Use chunk, phrase, and paragraph to create a text section content
Paragraph is a paragraph in an article. It can be composed of phrases (sentences), and phrase can be further formed by chunks (Text fragments. In this way, you can easily insert different types of fonts into text paragraphs. Note that the chunk does not automatically upload rows. You must insert the line character "\ n" or environment. newline. As for paragraph, there are many types of settings, such as alignment, indentation, leading, and spacing.
The results are as follows:
Part 3: Production table
Use itextsharp
Production tables are very straightforward and easy to use, similar to the CSS embedding method. When creating a table, it is easy to use the sequence phase to set the sequence degree, or to give the sequence degree or a single sequence to the sequence number for an average score, ex: pdfptable
Table = new pdfptable (4 );.
It is worth noting that, from the pdfptable table, each cell is called a cell. Therefore, when data is inserted, you must note that the padding method is left-right, top-down. In addition, pdfpcell has the colspan function of combining two bytes and rowspan function of combining two columns. We can use these two features to make some changes to the plain table. Take the following example as an example:
The results of the rows are as follows:
Part 4: insert parts
The image types supported by itextsharp include JPEG, MPEG-4, GIF, PNG, BMP, WMF, Tiff, and JBIG2. In addition to obtaining the local region of the server, you can also directly specify the URL of the region. The example of our extension table is as follows:
The results of the rows are as follows:
Part 5: zookeeper
Itextsharp is a distributed component of the snapshot structure generated by chapter and section. The top primary Primary Secondary. Section must exist in the chapter object or in another section object.
If you want to automatically expand the hosts column when opening a PDF file, add the following program example:
Using writer. viewerpreferences = Using writer. pagemodeuseoutlines; |
The results of the rows are as follows:
Part 6: Floating watermark
The itextsharp watermark method is to use the watermark Stamper class, and must be processing the existing PDF watermark. In our example, we recommend that you first generate a server-side attack and then process it with the pdfreader object.
The results of the rows are as follows:
Part 7: document encryption and Protection
To enable a PDF file, you must upload a secret to the user. The itextsharp method is to process existing PDF files. In our example, we recommend that you first generate a server-side attack and then process it with the pdfreader object. All security settings use pdfencryptor objects.
Pdfencryptor. Encrypt (pdfreader reader, stream OS,
Bool strength, string userpassword,
String ownerpassword, int permissions );
You can set the user password and owner password in the preceding main function. Once set, you must encrypt the password every time you open a PDF file. User password can be reserved, but cannot be printed or modified. Owner password has full permission.
You can see that the printing and saving of the statement are disable.
If you want to hide the menu bar and Tool Columns, you only need to set the parameter writer. viewerpreferences for the simple setting. This is used when you introduced the practice of auto-opening parameter columns.
Using writer. viewerpreferences = Using writer. hidemenubar | using writer. hidetoolbar; |
If you want to enable certain rights limits for the files to be protected, such as enabling users to print the files, you can use the pdfencryptor
The settings in the object are as follows:
Pdfencryptor. Encrypt (reader, memorystream, null, null, Using writer. allowassembly | using writer. allowfillin | using writer. allowscreenreaders | using writer. allowprinting, false ); |
Part 8: Metadata
We can add some values to the metadata file of the PDF file, but we recommend using the pdfencryptor object to prevent modification.
Doc1.addtitle (" dpdf "); Doc1.addauthor ("Dana Tang "); Doc1.addsubject ("This example shows how to add metadata "); Doc1.addkeywords ("metadata, itextsharp, PDF "); Doc1.addcreator ("using itextsharp "); Doc1.open (); |
Open a PDF example and select "case"> "content" as the tool, and you will see the following details:
Success! Do you think the itextsharp function is very powerful? We can now produce a professional PDF file. In fact, itextsharp also has many progressive functions, such as header, footer, barcode, direct content, and JavaScript, so we will try again later.
Additional information
1. Main tutorial website
Http://itextpdf.com/index.php
Http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp
2. Watermark watermark
Http://footheory.com/blogs/donnfelker/archive/2008/05/11/using-itextsharp-to-watermark-write-text-to-existing-pdf-s.aspx
3. register your bookmark
Http://www.mazsoft.com/blog/post/2008/04/30/Code-sample-for-using-iTextSharp-PDF-library.aspx
4. footer notebook
Http://stackoverflow.com/questions/1032614/itextsharp-creating-a-footer-page-of