Operate PDF-iTextSharp-Use font in Asp. Net

Source: Internet
Author: User

 

Next to my blog on iTextSharp, iTextSharp is a free third-party component that allows Asp. Net to operate on PDF files. This article describes how to use various fonts in your PDF document. If you have not read my first article, I strongly recommend that you read iTextSharp now.

 

ITextSharp supports 14 fonts by default, including Courier, Courier Bold, Courier Italic, Courier Bold and Italic, Helvetica, Helvetica Bold, Helvetica Italic, Helvetica Bold and Italic, Times Roman, times Roman Bold, Times Roman Italic, Times Roman Bold and Italic, Symbol, ZapfDingBats®Because Times Roman already has a replacement for Times New Roman, the default iTextSharp font is Helvetica, 12pt, and black, which is the so-called Normal font.

 

ITextSharp provides three main ways to set fonts: one is to use the BaseFont. CreateFont () method, and the other is to use the FontFactory. GetFont () method. The third method is to directly generate a new Font object. BaseFont. CreateFont () has many limitations, as it only produces a new Font definition. New Font () allows -------------, FontFactory. GetFont () to return a Font object that you can directly operate on. 14 different reloads are provided to provide you with more options, so you may usually use this method, but before you start to use this method, let's take a look at BaseFont. createFont () method:

 

BaseFont bfTimes = BaseFont. CreateFont (BaseFont. TIMES_ROMAN, BaseFont. CP1252, false );

 

Font times = new Font (bfTimes, 12, Font. ITALIC, Color. RED );

 

The above Code creates a BaseFont object and uses the built-in constant value to set the font type and encoding type. Select False in whether to embed the font in PDF to reduce the PDF size. however, if your font is not used in most users' computers, or you plan to fl your pdf on a professional printing device, you must select True. use BaseFont to create a new Font object. The next line of code further sets the Font from the Font size, Font style, and color. Of course, we still use the built-in constant type value. below, add the above style font to the paragraph:

 

 

String path = Server. MapPath ("PDFs ");

 

Document doc = new Document ();

 

Using writer. GetInstance (doc, new FileStream (path + "/fontmode", FileMode. Create ));

 

Doc. Open ();

 

Doc. Add (new Paragraph ("This is a Red Font Test using Times Roman", times ));

 

Doc. Close ();

 

The result is as follows:

 

 

 

Let's start with FontFactory. getFont () method, which provides 14 different reloads for you to set any aspect of the font, including: font, color, style, whether embedded, encoding and cache. every time you call FontFactory. getFont () returns a new object. this method can take effect for any font registered in iTextSharp. The Fonts registered in iTextSharp include the default windows Font directory. In win xp, this directory is generally "C:/WINDOWS/Fonts ", if you want to know which fonts have been registered in iTextSharp, FontFactory. registeredFonts will tell you the answer. Viewing this list is especially important for us to get the exact Font Name:

 

 

Int totalfonts = FontFactory. RegisterDirectory ("C :\\ WINDOWS \ Fonts ");

 

StringBuilder sb = new StringBuilder ();

 

Foreach (string fontname in FontFactory. RegisteredFonts)

 

{

 

Sb. Append (fontname + "\ n ");

 

}

 

Doc. Add (new Paragraph ("All Fonts: \ n" + sb. ToString ()));

 

 

 

 

 

 

 

Font arial = FontFactory. GetFont ("Arial", 28, Color. GRAY );

 

Font verdana = FontFactory. GetFont ("Verdana", 16, Font. BOLDITALIC, new Color (125, 88, 15 ));

 

Font palatino = FontFactory. GetFont (

 

"Palatino linotype italique ",

 

BaseFont. CP1252,

 

BaseFont. EMBEDDED,

 

10,

 

Font. ITALIC,

 

Color. GREEN

 

);

 

Font smallfont = FontFactory. GetFont ("Arial", 7 );

 

Font x = FontFactory. GetFont ("nina fett ");

 

X. Size = 10;

 

X. SetStyle ("Italic ");

 

X. SetColor (100, 50,200 );

 

 

As you can see, the above methods use the constant value of the iTextSharp Color object to set the font Color, and also use the SetColor () method to pass in the RGB value or a New Color object. In general, we can pass in the int value as the font style parameter, or use the SetStyle () method to pass in a string. Of course, there are many parameter input methods for generating Font. You can use intelliisense or Object Browser to view more precise parameter usage methods.

 

 

 

Font Registration

Sometimes you may encounter a problem where you do not have the permission to install fonts on the WEB server. In this case, you must register the fonts in iTextSharp:

 

 

String fontpath = Server. MapPath (".");

 

BaseFont customfont = BaseFont. CreateFont (fontpath + "myspecial. ttf", BaseFont. CP1252, BaseFont. EMBEDDED );

 

Font font = new Font (customfont, 12 );

 

String s = "My expensive M font .";

 

Doc. Add (new Paragraph (s, font ));

 

In the above Code, you may notice that the font file is EMBEDDED in PDF (BaseFont. EMBEDDED), because in many cases, the font in the PDF you create does not exist on your computer.

 

Original article: Create PDFs in ASP. NET-getting started with iTextSharp translated by careyson <script> </script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.