示例代码目录
示例代码0101. 5
示例代码0102. 7
示例代码0103. 9
示例代码0104. 11
示例代码0105. 13
示例代码0106. 15
示例代码0107. 17
示例代码0108. 20
示例代码0109. 23
示例代码0110. 24
示例代码0111. 25
示例代码0201. 29
示例代码0202. 31
示例代码0203. 34
示例代码0204. 36
示例代码0205. 38
示例代码0206. 40
示例代码0207. 43
示例代码0208. 45
示例代码0301. 47
示例代码0302. 49
示例代码0303. 52
示例代码0304. 55
示例代码0401. 57
示例代码0402. 59
示例代码0403. 62
示例代码0404. 64
示例代码0405. 66
示例代码0501. 68
示例代码0502. 70
示例代码0503. 72
示例代码0504. 74
示例代码0505. 76
示例代码0506. 78
示例代码0507. 80
示例代码0508. 81
示例代码0509. 84
示例代码0510. 87
示例代码0511. 90
示例代码0512. 92
示例代码0513. 95
示例代码0514. 98
示例代码0515. 102
示例代码0516. 104
示例代码0517. 106
示例代码0518. 108
示例代码0601. 111
示例代码0602. 113
示例代码0603. 115
示例代码0604. 117
示例代码0605. 119
示例代码0606. 121
示例代码0607. 123
示例代码0608. 125
示例代码0609. 127
示例代码0610. 129
示例代码0611. 131
示例代码0613. 133
示例代码0614. 135
示例代码0615. 137
示例代码0616. 139
示例代码0702. 141
示例代码0801. 142
示例代码0802. 144
示例代码0803. 146
示例代码0804. 148
示例代码0901. 150
示例代码1001. 152
示例代码1002. 154
示例代码1003. 156
示例代码1004. 158
示例代码1005. 160
示例代码1006. 165
示例代码1007. 170
示例代码1008. 173
示例代码1009. 177
示例代码1010. 178
示例代码1011. 180
示例代码1012. 182
示例代码1013. 187
示例代码1013. 189
示例代码1014. 193
示例代码1015. 195
示例代码1101. 200
示例代码1102. 202
示例代码0101
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0101
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 1: Hello World");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
document.Add(new Paragraph("Hello World"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0102
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0102
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 2: PageSize");
// step 1: creation of a document-object
Rectangle pageSize = new Rectangle(144, 720);
pageSize.BackgroundColor = new Color(0xFF, 0xFF, 0xDE);
Document document = new Document(pageSize);
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0102.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add some paragraphs to the document
for (int i = 0; i < 5; i++)
{
document.Add(new Paragraph("Hello World"));
}
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0103
using System;
using System.IO;
using System.Diagnostics;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0103
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 3: PageSize");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4.rotate());
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0103.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add some phrases to the document
for (int i = 0; i < 20; i++)
{
document.Add(new Phrase("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. "));
}
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0104
using System;
using System.IO;
using System.Diagnostics;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0104
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 4: Margins");
// step 1: creation of a document-object
Document document = new Document(PageSize.A5, 36, 72, 108, 180);
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0104.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
Paragraph paragraph = new Paragraph();
paragraph.Alignment = Element.ALIGN_JUSTIFIED;
for (int i = 0; i < 20; i++)
{
paragraph.Add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. ");
}
document.Add(paragraph);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0105
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
/// <summary>
/// Summary description for Chap0501.
/// </summary>
public class Chap0501 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// step 1
// need to write to memory first due to IE wanting
// to know the length of the pdf beforehand
MemoryStream m = new MemoryStream();
Document document = new Document();
try
{
// step 2: we set the ContentType and create an instance of the Writer
Response.ContentType = "application/pdf";
PdfWriter.getInstance(document, m);
// step 3
document.Open();
// step 4
document.Add(new Paragraph(DateTime.Now.ToString()));
}
catch (DocumentException ex)
{
Console.Error.WriteLine(ex.StackTrace);
Console.Error.WriteLine(ex.Message);
}
// step 5: Close document
document.Close();
// step 6: Write pdf bytes to outputstream
Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
示例代码0106
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0106
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 6: Meta Information");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0106.pdf", FileMode.Create));
// step 3: we add some metadata and open the document
document.addTitle("Hello World example");
document.addSubject("This example explains step 6 in Chapter 1");
document.addKeywords("Metadata, iText, step 6, tutorial");
document.addCreator("My program using iText#");
document.addAuthor("Bruno Lowagie");
document.addHeader("Expires", "0");
document.Open();
// step 4: we add a paragraph to the document
document.Add(new Paragraph("Hello World"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0107
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0107
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 7: newPage()");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0107.pdf", FileMode.Create));
// step 3:
// we Add a Watermark that will show up on PAGE 1
try
{
Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 200, 420);
document.Add(watermark);
}
catch
{
Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
}
// we Add a Header that will show up on PAGE 1
HeaderFooter header = new HeaderFooter(new Phrase("This is a header"), false);
document.Header = header;
// we open the document
document.Open();
// we rotate the page, starting from PAGE 2
document.setPageSize(PageSize.A4.rotate());
// we need to change the position of the Watermark
try
{
Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 320, 200);
document.Add(watermark);
}
catch
{
Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
}
// we Add a Footer that will show up on PAGE 2
HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
document.Footer = footer;
// step 4: we Add content to the document
// PAGE 1
document.Add(new Paragraph("Hello World"));
// we trigger a page break
document.newPage();
// PAGE 2
// we Add some more content
document.Add(new Paragraph("Hello Earth"));
// we remove the header starting from PAGE 3
document.resetHeader();
// we trigger a page break
document.newPage();
// PAGE 3
// we Add some more content
document.Add(new Paragraph("Hello Sun"));
document.Add(new Paragraph("Remark: the header has vanished!"));
// we reset the page numbering
document.resetPageCount();
// we trigger a page break
document.newPage();
// PAGE 4
// we Add some more content
document.Add(new Paragraph("Hello Moon"));
document.Add(new Paragraph("Remark: the pagenumber has been reset!"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0108
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0108
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 8: Viewerpreferences");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writerA = PdfWriter.getInstance(document, new FileStream("Chap0108a.pdf", FileMode.Create));
writerA.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
PdfWriter writerB = PdfWriter.getInstance(document, new FileStream("Chap0108b.pdf", FileMode.Create));
writerB.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
PdfWriter writerC = PdfWriter.getInstance(document, new FileStream("Chap0108c.pdf", FileMode.Create));
writerC.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft | PdfWriter.PageModeFullScreen | PdfWriter.NonFullScreenPageModeUseThumbs);
// step 3:
// we Add a Watermark that will show up on PAGE 1
try
{
Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 200, 420);
document.Add(watermark);
}
catch
{
Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
}
// we Add a Header that will show up on PAGE 1
HeaderFooter header = new HeaderFooter(new Phrase("This is a header"), false);
document.Header = header;
// we open the document
document.Open();
// we rotate the page, starting from PAGE 2
document.setPageSize(PageSize.A4.rotate());
// we need to change the position of the Watermark
try
{
Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 320, 200);
document.Add(watermark);
}
catch
{
Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
}
// we Add a Footer that will show up on PAGE 2
HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
document.Footer = footer;
// step 4: we Add content to the document
// PAGE 1
document.Add(new Paragraph("Hello World"));
// we trigger a page break
document.newPage();
// PAGE 2
// we Add some more content
document.Add(new Paragraph("Hello Earth"));
// we remove the header starting from PAGE 3
document.resetHeader();
// we trigger a page break
document.newPage();
// PAGE 3
// we Add some more content
document.Add(new Paragraph("Hello Sun"));
document.Add(new Paragraph("Remark: the header has vanished!"));
// we reset the page numbering
document.resetPageCount();
// we trigger a page break
document.newPage();
// PAGE 4
// we Add some more content
document.Add(new Paragraph("Hello Moon"));
document.Add(new Paragraph("Remark: the pagenumber has been reset!"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0109
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0109
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 9: encryption 40 bits");
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0109.pdf", FileMode.Create));
writer.setEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowCopy);
document.Open();
document.Add(new Paragraph("This document is Top Secret!"));
document.Close();
}
catch (Exception de)
{
Console.WriteLine(de.StackTrace);
}
}
}
示例代码0110
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0110
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 10: encryption 128 bits");
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0110.pdf", FileMode.Create));
writer.setEncryption(PdfWriter.STRENGTH128BITS, "userpass", "ownerpass", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
document.Open();
document.Add(new Paragraph("This document is Top Secret!"));
document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.StackTrace);
}
}
}
示例代码0111
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0111
{
public static void Main()
{
Console.WriteLine("Chapter 1 example 11: pause() and resume()");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writerA = PdfWriter.getInstance(document, new FileStream("Chap0111a.pdf", FileMode.Create));
PdfWriter writerB = PdfWriter.getInstance(document, new FileStream("Chap0111b.pdf", FileMode.Create));
// step 3:
// we Add a Watermark that will show up on PAGE 1
writerB.Pause();
try
{
Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 200, 420);
document.Add(watermark);
}
catch
{
Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
}
writerB.resume();
// we Add a Header that will show up on PAGE 1
HeaderFooter header = new HeaderFooter(new Phrase("This is a header"), false);
document.Header = header;
// we open the document
document.Open();
// we rotate the page, starting from PAGE 2
document.setPageSize(PageSize.A4.rotate());
// we need to change the position of the Watermark
try
{
Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 320, 200);
document.Add(watermark);
}
catch
{
Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
}
// we Add a Footer that will show up on PAGE 2
HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
document.Footer = footer;
// step 4: we Add content to the document
// PAGE 1
document.Add(new Paragraph("Hello World"));
// we trigger a page break
document.newPage();
// PAGE 2
// we Add some more content
document.Add(new Paragraph("Hello Earth"));
// we remove the header starting from PAGE 3
writerA.Pause();
document.resetHeader();
writerA.resume();
// we trigger a page break
document.newPage();
// PAGE 3
// we Add some more content
document.Add(new Paragraph("Hello Sun"));
writerA.Pause();
document.Add(new Paragraph("Remark: the header has vanished!"));
writerA.resume();
// we reset the page numbering
writerB.Pause();
document.resetPageCount();
writerB.resume();
// we trigger a page break
document.newPage();
// PAGE 4
// we Add some more content
document.Add(new Paragraph("Hello Moon"));
writerB.Pause();
document.Add(new Paragraph("Remark: the pagenumber has been reset!"));
writerB.resume();
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0201
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0201
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 1: Chunks and fonts");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0201.pdf",FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add content to the document
Font[] fonts = new Font[14];
fonts[0] = FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL);
fonts[1] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD);
fonts[2] = FontFactory.getFont(FontFactory.COURIER, 12, Font.ITALIC);
fonts[3] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC);
fonts[4] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
fonts[5] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
fonts[6] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC);
fonts[7] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC);
fonts[8] = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12, Font.NORMAL);
fonts[9] = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12, Font.BOLD);
fonts[10] = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12, Font.ITALIC);
fonts[11] = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12, Font.BOLD | Font.ITALIC);
fonts[12] = FontFactory.getFont(FontFactory.SYMBOL, 12, Font.NORMAL);
fonts[13] = FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL);
for (int i = 0; i < 14; i++)
{
Chunk chunk = new Chunk("This is some", fonts[i]);
document.Add(new Phrase(chunk));
document.Add(new Phrase(new Chunk(" font. ",
fonts[i]).setTextRise((i % 2 == 0) ? -6 : 6)));
}
document.Add(new Phrase(new Chunk("This text is underlined",
FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE))));
document.Add(new Phrase(new Chunk("This font is of type ITALIC | STRIKETHRU",
FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC | Font.STRIKETHRU))));
Chunk ck = new Chunk("This text has a yellow background color", FontFactory.getFont(FontFactory.HELVETICA, 12));
ck.setBackground(new Color(0xFF, 0xFF, 0x00));
document.Add(new Phrase(ck));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0202
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0202
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 2: Phrases");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0202.pdf",FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
Phrase phrase0 = new Phrase();
Phrase phrase1 = new Phrase("(1) this is a phrase ");
// In this example the leading is passed as a parameter
Phrase phrase2 = new Phrase(24, "(2) this is a phrase with leading 24. You can only see the difference if the line is long enough. Do you see it? There is more space between this line and the previous one. ");
// When a Font is passed (explicitely or embedded in a chunk),
// the default leading = 1.5 * size of the font
Phrase phrase3 = new Phrase("(3) this is a phrase with a red, normal font Courier, size 20. As you can see the leading is automatically changed. ", FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL, new Color(255, 0, 0)));
Phrase phrase4 = new Phrase(new Chunk("(4) this is a phrase "));
Phrase phrase5 = new Phrase(18, new Chunk("(5) this is a phrase in Helvetica, bold, red and size 16 with a given leading of 18 points. ", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0))));
// A Phrase can contains several chunks with different fonts
Phrase phrase6 = new Phrase("(6)");
Chunk chunk = new Chunk(" This is a font: ");
phrase6.Add(chunk);
phrase6.Add(new Chunk("Helvetica", FontFactory.getFont(FontFactory.HELVETICA, 12)));
phrase6.Add(chunk);
phrase6.Add(new Chunk("Times New Roman", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12)));
phrase6.Add(chunk);
phrase6.Add(new Chunk("Courier", FontFactory.getFont(FontFactory.COURIER, 12)));
phrase6.Add(chunk);
phrase6.Add(new Chunk("Symbol", FontFactory.getFont(FontFactory.SYMBOL, 12)));
phrase6.Add(chunk);
phrase6.Add(new Chunk("ZapfDingBats", FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12)));
Phrase phrase7 = new Phrase("(7) if you don't Add a newline yourself, all phrases are glued to eachother!");
document.Add(phrase1);
document.Add(phrase2);
document.Add(phrase3);
document.Add(phrase4);
document.Add(phrase5);
document.Add(phrase6);
document.Add(phrase7);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0203
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0203
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 3: Greek Characters");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0203.pdf",FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
document.Add(new Phrase("What is the " + (char) 945 + "-coefficient of the "
+ (char) 946 + "-factor in the " + (char) 947 + "-equation? "));
for (int i = 913; i < 970; i++)
{
document.Add(new Phrase(" " + i.ToString() + ": " + (char) i));
}
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0204
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0204
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 4: Negative leading");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0204.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
document.Add(new Phrase(16, " "));
document.Add(new Phrase(-16, "Hello, this is a very long phrase to show you the somewhat odd effect of a negative leading. You can write from bottom to top. This is not fully supported. It's something between a feature and a bug."));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0205
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0205
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 5: Paragraphs");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0205.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph. ",
FontFactory.getFont(FontFactory.HELVETICA, 10)));
p1.Add("The leading of this paragraph is calculated automagically. ");
p1.Add("The default leading is 1.5 times the fontsize. ");
p1.Add(new Chunk("You can Add chunks "));
p1.Add(new Phrase("or you can Add phrases. "));
p1.Add(new Phrase("Unless you change the leading with the method setLeading, the leading doesn't change if you Add text with another leading. This can lead to some problems.", FontFactory.getFont(FontFactory.HELVETICA, 18)));
document.Add(p1);
Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph. ",
FontFactory.getFont(FontFactory.HELVETICA, 12)));
p2.Add("As you can see, it started on a new line.");
document.Add(p2);
Paragraph p3 = new Paragraph("This is my third paragraph.",
FontFactory.getFont(FontFactory.HELVETICA, 12));
document.Add(p3);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0206
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0206
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 6: keeping a paragraph together");
// step 1: creation of a document-object
Document document = new Document(PageSize.A6);
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0206.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4:
Paragraph p;
p = new Paragraph("GALLIA est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes lingua, institutis, legibus inter se differunt. Gallos ab Aquitanis Garumna flumen, a Belgis Matrona et Sequana dividit. Horum omnium fortissimi sunt Belgae, propterea quod a cultu atque humanitate provinciae longissime absunt, minimeque ad eos mercatores saepe commeant atque ea quae ad effeminandos animos pertinent important, proximique sunt Germanis, qui trans Rhenum incolunt, quibuscum continenter bellum gerunt. Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis proeliis cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt.", FontFactory.getFont(FontFactory.HELVETICA, 12));
p.KeepTogether = true;
document.Add(p);
p = new Paragraph("[Eorum una, pars, quam Gallos obtinere dictum est, initium capit a flumine Rhodano, continetur Garumna flumine, Oceano, finibus Belgarum, attingit etiam ab Sequanis et Helvetiis flumen Rhenum, vergit ad septentriones. Belgae ab extremis Galliae finibus oriuntur, pertinent ad inferiorem partem fluminis Rheni, spectant in septentrionem et orientem solem. Aquitania a Garumna flumine ad Pyrenaeos montes et eam partem Oceani quae est ad Hispaniam pertinet; spectat inter occasum solis et septentriones.]", FontFactory.getFont(FontFactory.HELVETICA, 12));
p.KeepTogether = true;
document.Add(p);
p = new Paragraph("Apud Helvetios longe nobilissimus fuit et ditissimus Orgetorix. Is M. Messala, [et P.] M. Pisone consulibus regni cupiditate inductus coniurationem nobilitatis fecit et civitati persuasit ut de finibus suis cum omnibus copiis exirent: perfacile esse, cum virtute omnibus praestarent, totius Galliae imperio potiri. Id hoc facilius iis persuasit, quod undique loci natura Helvetii continentur: una ex parte flumine Rheno latissimo atque altissimo, qui agrum Helvetium a Germanis dividit; altera ex parte monte Iura altissimo, qui est inter Sequanos et Helvetios; tertia lacu Lemanno et flumine Rhodano, qui provinciam nostram ab Helvetiis dividit. His rebus fiebat ut et minus late vagarentur et minus facile finitimis bellum inferre possent; qua ex parte homines bellandi cupidi magno dolore adficiebantur. Pro multitudine autem hominum et pro gloria belli atque fortitudinis angustos se fines habere arbitrabantur, qui in longitudinem milia passuum CCXL, in latitudinem CLXXX patebant.", FontFactory.getFont(FontFactory.HELVETICA, 12));
p.KeepTogether = true;
document.Add(p);
p = new Paragraph("His rebus Adducti et auctoritate Orgetorigis permoti constituerunt ea quae ad proficiscendum pertinerent comparare, iumentorum et carrorum quam maximum numerum coemere, sementes quam maximas facere, ut in itinere copia frumenti suppeteret, cum proximis civitatibus pacem et amicitiam confirmare. Ad eas res conficiendas biennium sibi satis esse duxerunt; in tertium annum profectionem lege confirmant. Ad eas res conficiendas Orgetorix deligitur. Is sibi legationem ad civitates suscipit. In eo itinere persuadet Castico, Catamantaloedis filio, Sequano, cuius pater regnum in Sequanis multos annos obtinuerat et a senatu populi Romani amicus appellatus erat, ut regnum in civitate sua occuparet, quod pater ante habuerit; itemque Dumnorigi Haeduo, fratri Diviciaci, qui eo tempore principatum in civitate obtinebat ac maxime plebi acceptus erat, ut idem conaretur persuadet eique filiam suam in matrimonium dat. Perfacile factu esse illis probat conata perficere, propterea quod ipse suae civitatis imperium obtenturus esset: non esse dubium quin totius Galliae plurimum Helvetii possent; se suis copiis suoque exercitu illis regna conciliaturum confirmat. Hac oratione Adducti inter se fidem et ius iurandum dant et regno occupato per tres potentissimos ac firmissimos populos totius Galliae sese potiri posse sperant.", FontFactory.getFont(FontFactory.HELVETICA, 12));
p.KeepTogether = true;
document.Add(p);
p = new Paragraph("Ea res est Helvetiis per indicium enuntiata. Moribus suis Orgetoricem ex vinculis causam dicere coegerunt; damnatum poenam sequi oportebat, ut igni cremaretur. Die constituta causae dictionis Orgetorix ad iudicium omnem suam familiam, ad hominum milia decem, undique coegit, et omnes clientes obaeratosque suos, quorum magnum numerum habebat, eodem conduxit; per eos ne causam diceret se eripuit. Cum civitas ob eam rem incitata armis ius suum exequi conaretur multitudinemque hominum ex agris magistratus cogerent, Orgetorix mortuus est; neque abest suspicio, ut Helvetii arbitrantur, quin ipse sibi mortem consciverit.", FontFactory.getFont(FontFactory.HELVETICA, 12));
p.KeepTogether = true;
document.Add(p);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0207
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0207
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 7: font propagation");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0207.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4:
// we Add some content
Phrase myPhrase = new Phrase("Hello 1! ", new Font(Font.TIMES_NEW_ROMAN, 8, Font.BOLD));
myPhrase.Add(new Phrase("some other font ", new Font(Font.HELVETICA, 8)));
myPhrase.Add(new Phrase("This is the end of the sentence. ", new Font(Font.TIMES_NEW_ROMAN, 8, Font.ITALIC)));
document.Add(myPhrase);
myPhrase = new Phrase("Hello 1bis! ", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 8, Font.BOLD));
myPhrase.Add(new Phrase("some other font ", FontFactory.getFont(FontFactory.HELVETICA, 8)));
myPhrase.Add(new Phrase("This is the end of the sentence. ", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 8, Font.ITALIC)));
document.Add(myPhrase);
Paragraph myParagraph = new Paragraph("Hello 2! ", new Font(Font.TIMES_NEW_ROMAN, 8, Font.BOLD));
myParagraph.Add(new Paragraph("This is the end of the sentence.", new Font(Font.TIMES_NEW_ROMAN, 8, Font.ITALIC)));
document.Add(myParagraph);
myParagraph = new Paragraph(12);
myParagraph.Add(new Paragraph("Hello 3! ", new Font(Font.TIMES_NEW_ROMAN, 8, Font.BOLD)));
myParagraph.Add(new Paragraph("This is the end of the sentence.", new Font(Font.TIMES_NEW_ROMAN, 8, Font.ITALIC)));
document.Add(myParagraph);
myPhrase = new Phrase(12);
myPhrase.Add(new Phrase("Hello 4! ", new Font(Font.TIMES_NEW_ROMAN, 8, Font.BOLD)));
myPhrase.Add(new Phrase("This is the end of the sentence. ", new Font(Font.TIMES_NEW_ROMAN, 8, Font.ITALIC)));
document.Add(myPhrase);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0208
using System;
using System.IO;
using System.Collections;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0208 : ISplitCharacter
{
public static void Main()
{
Console.WriteLine("Chapter 2 example 8: split character");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0208.pdf", FileMode.Create));
// step 3: we Open the document
document.Open();
// step 4:
// we Add some content
String text = "Some.text.to.show.the.splitting.action.of.the.interface.";
Chap0208 split = new Chap0208();
Chunk ck = new Chunk(text, FontFactory.getFont(FontFactory.HELVETICA, 24));
Paragraph p = new Paragraph(24, ck);
document.Add(new Paragraph("Normal split."));
document.Add(p);
ck = new Chunk(text, FontFactory.getFont(FontFactory.HELVETICA, 24));
ck.setSplitCharacter(split);
p = new Paragraph(24, ck);
document.Add(new Paragraph("The dot '.' is the split character."));
document.Add(p);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
/**
* Returns <CODE>true</CODE> if the character can split a line.
* @param c the character
* @return <CODE>true</CODE> if the character can split a line
*/
public bool isSplitCharacter(char c)
{
return (c == '.');
}
}
示例代码0301
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0301
{
public static void Main()
{
Console.WriteLine("Chapter 3 example 1: Anchors");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0301.pdf", FileMode.Create));
// step 3: we Open the document
document.Open();
// step 4:
Paragraph paragraph = new Paragraph("Please visit my ");
Anchor anchor1 = new Anchor("website (external reference)", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255)));
anchor1.Reference = "http://itextsharp.sourceforge.net";
anchor1.Name = "top";
paragraph.Add(anchor1);
paragraph.Add(new Chunk(". "));
document.Add(paragraph);
Anchor anchor2 = new Anchor("please jump to a local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255)));
anchor2.Reference = "#top";
document.Add(anchor2);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0302
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0302
{
public static void Main()
{
Console.WriteLine("Chapter 3 example 2: Lists");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0302.pdf", FileMode.Create));
// step 3: we Open the document
document.Open();
// step 4:
List list = new List(true, 20);
list.Add(new ListItem("First line"));
list.Add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
list.Add(new ListItem("Third line"));
document.Add(list);
document.Add(new Paragraph("some books I really like:"));
ListItem listItem;
list = new List(true, 15);
listItem = new ListItem("When Harlie was one", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12));
listItem.Add(new Chunk(" by David Gerrold", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 11, Font.ITALIC)));
list.Add(listItem);
listItem = new ListItem("The World according to Garp", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12));
listItem.Add(new Chunk(" by John Irving", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 11, Font.ITALIC)));
list.Add(listItem);
listItem = new ListItem("Decamerone", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12));
listItem.Add(new Chunk(" by Giovanni Boccaccio", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 11, Font.ITALIC)));
list.Add(listItem);
document.Add(list);
Paragraph paragraph = new Paragraph("some movies I really like:");
list = new List(false, 10);
list.Add("Wild At Heart");
list.Add("Casablanca");
list.Add("When Harry met Sally");
list.Add("True Romance");
list.Add("Le mari de la coiffeuse");
paragraph.Add(list);
document.Add(paragraph);
document.Add(new Paragraph("Some authors I really like:"));
list = new List(false, 20);
list.ListSymbol = new Chunk("u2022", FontFactory.getFont(FontFactory.HELVETICA, 20, Font.BOLD));
listItem = new ListItem("Isaac Asimov");
list.Add(listItem);
List sublist;
sublist = new List(true, 10);
sublist.ListSymbol = new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 8));
sublist.Add("The Foundation Trilogy");
sublist.Add("The Complete Robot");
sublist.Add("Caves of Steel");
sublist.Add("The Naked Sun");
list.Add(sublist);
listItem = new ListItem("John Irving");
list.Add(listItem);
sublist = new List(true, 10);
sublist.ListSymbol = new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 8));
sublist.Add("The World according to Garp");
sublist.Add("Hotel New Hampshire");
sublist.Add("A prayer for Owen Meany");
sublist.Add("Widow for a year");
list.Add(sublist);
listItem = new ListItem("Kurt Vonnegut");
list.Add(listItem);
sublist = new List(true, 10);
sublist.ListSymbol = new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 8));
sublist.Add("Slaughterhouse 5");
sublist.Add("Welcome to the Monkey House");
sublist.Add("The great pianola");
sublist.Add("Galapagos");
list.Add(sublist);
document.Add(list);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0303
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0303
{
public static void Main()
{
Console.WriteLine("Chapter 3 example 3: Annotations");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0303.pdf", FileMode.Create));
// step 3: we Open the document
document.Open();
// step 4:
List list = new List(true, 20);
list.Add(new ListItem("First line"));
list.Add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
list.Add(new ListItem("Third line"));
document.Add(list);
document.Add(new Paragraph("some books I really like:"));
document.Add(new Annotation("books", "This is really a very short list, I like a lot of books."));
ListItem listItem;
list = new List(true, 15);
listItem = new ListItem("When Harlie was one", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12));
listItem.Add(new Chunk(" by David Gerrold", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 11, Font.ITALIC)));
list.Add(listItem);
listItem = new ListItem("The World according to Garp", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12));
listItem.Add(new Chunk(" by John Irving", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 11, Font.ITALIC)));
list.Add(listItem);
listItem = new ListItem("Decamerone", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 12));
listItem.Add(new Chunk(" by Giovanni Boccaccio", FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 11, Font.ITALIC)));
list.Add(listItem);
document.Add(list);
document.Add(new Phrase("Some authors I really like:"));
document.Add(new Annotation("authors", "Maybe it's because I wanted to be an writer myself that I wrote iText."));
list = new List(false, 20);
list.ListSymbol = new Chunk("*", FontFactory.getFont(FontFactory.HELVETICA, 20, Font.BOLD));
listItem = new ListItem("Isaac Asimov");
list.Add(listItem);
List sublist;
sublist = new List(true, 10);
sublist.ListSymbol = new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 8));
sublist.Add("The Foundation Trilogy");
sublist.Add("The Complete Robot");
sublist.Add("Caves of Steel");
sublist.Add("The Naked Sun");
list.Add(sublist);
listItem = new ListItem("John Irving");
list.Add(listItem);
sublist = new List(true, 10);
sublist.ListSymbol = new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 8));
sublist.Add("The World according to Garp");
sublist.Add("Hotel New Hampshire");
sublist.Add("A prayer for Owen Meany");
sublist.Add("Widow for a year");
list.Add(sublist);
listItem = new ListItem("Kurt Vonnegut");
list.Add(listItem);
sublist = new List(true, 10);
sublist.ListSymbol = new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 8));
sublist.Add("Slaughterhouse 5");
sublist.Add("Welcome to the Monkey House");
sublist.Add("The great pianola");
sublist.Add("Galapagos");
list.Add(sublist);
document.Add(list);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0304
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0304
{
public static void Main()
{
Console.WriteLine("Chapter 3 example 4: annotations at absolute positions");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
// step 2:
// we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0304.pdf", FileMode.Create));
// step 3: we Open the document
document.Open();
// step 4: we Add some content
PdfContentByte cb = writer.DirectContent;
// draw a rectangle
cb.setRGBColorStroke(0x00, 0x00, 0xFF);
cb.rectangle(100, 700, 100, 100);
cb.stroke();
Annotation annot = new Annotation(100f, 700f, 200f, 800f, "http://itextsharp.sourceforge.net");
document.Add(annot);
cb.setRGBColorStroke(0xFF, 0x00, 0x00);
cb.rectangle(200, 700, 100, 100);
cb.stroke();
try
{
document.Add(new Annotation(200f, 700f, 300f, 800f, new Uri("http://itextsharp.sourceforge.net")));
}
catch
{
}
cb.setRGBColorStroke(0x00, 0xFF, 0x00);
cb.rectangle(300, 700, 100, 100);
cb.stroke();
document.Add(new Annotation(300f, 700f, 400f, 800f, "c:/winnt/notepad.exe", null, null, null));
cb.setRGBColorStroke(0x00, 0x00, 0xFF);
cb.rectangle(100, 500, 100, 100);
cb.stroke();
document.Add(new Annotation("annotation", "This annotation is placed on an absolute position", 100f, 500f, 200f, 600f));
cb.setRGBColorStroke(0xFF, 0x00, 0x00);
cb.rectangle(200, 500, 100, 100);
cb.stroke();
document.Add(new Annotation(200f, 500f, 300f, 600f, "Chap1102a.pdf", "test"));
cb.setRGBColorStroke(0x00, 0xFF, 0x00);
cb.rectangle(300, 500, 100, 100);
cb.stroke();
document.Add(new Annotation(300f, 500f, 400f, 600f, "Chap1102b.pdf", 3));
}
catch (Exception de)
{
Console.WriteLine(de.StackTrace);
}
// step 5: we close the document
document.Close();
}
}
示例代码0401
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0401
{
public static void Main()
{
Console.WriteLine("Chapter 4 example 1: Headers en Footers");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2: we create a writer that listens to the document
PdfWriter.getInstance(document, new FileStream("Chap0401.pdf", FileMode.Create));
// we Add a Footer that will show up on PAGE 1
HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
footer.Border = Rectangle.NO_BORDER;
document.Footer = footer;
// step 3: we open the document
document.Open();
// we Add a Header that will show up on PAGE 2
HeaderFooter header = new HeaderFooter(new Phrase("This is a header"), false);
document.Header = header;
// step 4: we Add content to the document
// PAGE 1
document.Add(new Paragraph("Hello World"));
// we trigger a page break
document.newPage();
// PAGE 2
// we Add some more content
document.Add(new Paragraph("Hello Earth"));
// we remove the header starting from PAGE 3
document.resetHeader();
// we trigger a page break
document.newPage();
// PAGE 3
// we Add some more content
document.Add(new Paragraph("Hello Sun"));
document.Add(new Paragraph("Remark: the header has vanished!"));
// we reset the page numbering
document.resetPageCount();
// we trigger a page break
document.newPage();
// PAGE 4
// we Add some more content
document.Add(new Paragraph("Hello Moon"));
document.Add(new Paragraph("Remark: the pagenumber has been reset!"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0402
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0402
{
public static void Main()
{
Console.WriteLine("Chapter 4 example 2: Chapters and Sections");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0402.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add content to the document
// we define some fonts
Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new Color(255, 0, 0));
Font sectionFont = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, new Color(0, 0, 255));
Font subsectionFont = FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD, new Color(0, 64, 64));
// we create some paragraphs
Paragraph blahblah = new Paragraph("blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
Paragraph blahblahblah = new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
// this loop will create 7 chapters
for (int i = 1; i < 8; i++)
{
Paragraph cTitle = new Paragraph("This is chapter " + i, chapterFont);
Chapter chapter = new Chapter(cTitle, i);
if (i == 4)
{
blahblahblah.Alignment = Element.ALIGN_JUSTIFIED;
blahblah.Alignment = Element.ALIGN_JUSTIFIED;
chapter.Add(blahblah);
}
if (i == 5)
{
blahblahblah.Alignment = Element.ALIGN_CENTER;
blahblah.Alignment = Element.ALIGN_RIGHT;
chapter.Add(blahblah);
}
// Add a table in the 6th chapter
if (i == 6)
{
blahblah.Alignment = Element.ALIGN_JUSTIFIED;
}
// in every chapter 3 sections will be Added
for (int j = 1; j < 4; j++)
{
Paragraph sTitle = new Paragraph("This is section " + j + " in chapter " + i, sectionFont);
Section section = chapter.addSection(sTitle, 1);
// in all chapters except the 1st one, some extra text is Added to section 3
if (j == 3 && i > 1)
{
section.Add(blahblah);
}
// in every section 3 subsections are Added
for (int k = 1; k < 4; k++)
{
Paragraph subTitle = new Paragraph("This is subsection " + k + " of section " + j, subsectionFont);
Section subsection = section.addSection(subTitle, 3);
if (k == 1 && j == 3)
{
subsection.Add(blahblahblah);
}
subsection.Add(blahblah);
}
if (j == 2 && i > 2)
{
section.Add(blahblahblah);
}
}
document.Add(chapter);
}
}
catch(Exception de)
{
Console.Error.WriteLine(de.StackTrace);
}
// step 5: we close the document
document.Close();
}
}
示例代码0403
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0403
{
public static void Main()
{
Console.WriteLine("Chapter 4 example 3: Chapters and Sections");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
// step 2: we create a writer that listens to the document
PdfWriter writer=PdfWriter.getInstance(document, new FileStream("Chap0403.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add content to the document
Paragraph title1 = new Paragraph("This is Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
Chapter chapter1 = new Chapter(title1, 2);
chapter1.NumberDepth = 0;
Paragraph someText = new Paragraph("This is some text");
chapter1.Add(someText);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This is some silly paragraph in a chapter and/or section. It contains some text to test the functionality of Chapters and Section.");
section1.Add(someSectionText);
document.Add(chapter1);
Paragraph title2 = new Paragraph("This is Chapter 2", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
Chapter chapter2 = new Chapter(title2, 2);
chapter2.NumberDepth = 0;
chapter2.Add(someText);
Paragraph title21 = new Paragraph("This is Section 1 in Chapter 2", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section2 = chapter2.addSection(title21);
section2.Add(someSectionText);
chapter2.BookmarkOpen = false;
document.Add(chapter2);
}
catch(Exception de)
{
Console.Error.WriteLine(de.StackTrace);
}
// step 5: we close the document
document.Close();
}
}
示例代码0404
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0404
{
public static void Main()
{
Console.WriteLine("Chapter 4 example 4: Simple Graphic");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0404.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add a Graphic to the document
Graphic grx = new Graphic();
// add a rectangle
grx.rectangle(100, 700, 100, 100);
// add the diagonal
grx.moveTo(100, 700);
grx.lineTo(200, 800);
// stroke the lines
grx.stroke();
document.Add(grx);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0405
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0405
{
public static void Main()
{
Console.WriteLine("Chapter 4 example 5: page borders and horizontal lines");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2: we create a writer that listens to the document
PdfWriter.getInstance(document, new FileStream("Chap0405.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
Graphic g = new Graphic();
g.setBorder(3f, 5f);
document.Add(g);
document.Add(new Paragraph("Hello World"));
document.Add(new Paragraph("Hello World "));
g = new Graphic();
g.setHorizontalLine(5f, 100f);
document.Add(g);
document.Add(new Paragraph("Hello World"));
document.Add(new Paragraph("Hello World "));
g = new Graphic();
g.setHorizontalLine(2f, 80f, new Color(0xFF, 0x00, 0x00));
document.Add(g);
document.Add(new Paragraph("Hello World"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0501
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0501
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 1: my first table");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0501.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table aTable = new Table(2,2); // 2 rows, 2 columns
aTable.addCell("0.0");
aTable.addCell("0.1");
aTable.addCell("1.0");
aTable.addCell("1.1");
document.Add(aTable);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0502
using System;
using System.IO;
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0502
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 2: adding cells at a specific position");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0502.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table aTable;
aTable = new Table(4,4); // 4 rows, 4 columns
aTable.AutoFillEmptyCells = true;
aTable.addCell("2.2", new Point(2,2));
aTable.addCell("3.3", new Point(3,3));
aTable.addCell("2.1", new Point(2,1));
aTable.addCell("1.3", new Point(1,3));
document.Add(aTable);
document.newPage();
aTable = new Table(4,4); // 4 rows, 4 columns
aTable.addCell("2.2", new Point(2,2));
aTable.addCell("3.3", new Point(3,3));
aTable.addCell("2.1", new Point(2,1));
aTable.addCell("1.3", new Point(1,3));
document.Add(aTable);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0503
using System;
using System.IO;
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0503
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 3: rows added automatically");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0503.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table aTable = new Table(4,4); // 4 rows, 4 columns
aTable.AutoFillEmptyCells = true;
aTable.addCell("2.2", new Point(2,2));
aTable.addCell("3.3", new Point(3,3));
aTable.addCell("2.1", new Point(2,1));
aTable.addCell("1.3", new Point(1,3));
aTable.addCell("5.2", new Point(5,2));
aTable.addCell("6.1", new Point(6,1));
aTable.addCell("5.0", new Point(5,0));
document.Add(aTable);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0504
using System;
using System.IO;
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0504
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 4: adding columns");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0504.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table aTable = new Table(2,2); // 2 rows, 2 columns
aTable.AutoFillEmptyCells = true;
aTable.addCell("0.0");
aTable.addCell("0.1");
aTable.addCell("1.0");
aTable.addCell("1.1");
aTable.addColumns(2);
float[] f = {1f, 1f, 1f, 1f};
aTable.Widths = f;
aTable.addCell("2.2", new Point(2,2));
aTable.addCell("3.3", new Point(3,3));
aTable.addCell("2.1", new Point(2,1));
aTable.addCell("1.3", new Point(1,3));
aTable.addCell("5.2", new Point(5,2));
aTable.addCell("6.1", new Point(6,1));
aTable.addCell("5.0", new Point(5,0));
document.Add(aTable);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0505
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0505
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 5: colspan, rowspan, padding, spacing, colors");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0505.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.addCell(cell);
table.addCell("cell test2");
document.Add(table);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0506
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0506
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 6: spacing, padding, alignment");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0506.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 3;
table.Spacing = 1;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.addCell(cell);
table.addCell("cell test2");
document.Add(table);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0507
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0507
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 7: borders");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0507.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Border = Rectangle.TOP | Rectangle.BOTTOM;
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.BorderWidth = 3;
cell.Border = Rectangle.TOP | Rectangle.BOTTOM;
cell.Colspan = 3;
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
cell.Border = Rectangle.LEFT | Rectangle.BOTTOM;
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.Border = Rectangle.NO_BORDER;
cell.GrayFill = 0.9f;
table.addCell(cell);
table.addCell("cell test2");
document.Add(table);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0508
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0508
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 8: table splitting");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0508.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.addCell(cell);
table.addCell("cell test2");
document.Add(new Paragraph("repeating the same table 10 times:"));
for (int i = 0; i < 10; i++)
{
document.Add(table);
}
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0509
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0509
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 9: large tables");
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
try
{
// creation of the different writers
PdfWriter.getInstance(document, new FileStream("Chap0509.pdf", FileMode.Create));
// we add some meta information to the document
document.addAuthor("Gerald Henson");
document.addSubject("This is the result of a Test.");
document.Open();
Table datatable = new Table(10);
datatable.Padding = 4;
datatable.Spacing = 0;
//datatable.setBorder(Rectangle.NO_BORDER);
float[] headerwidths = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7};
datatable.Widths = headerwidths;
datatable.WidthPercentage = 100;
// the first cell spans 10 columns
Cell cell = new Cell(new Phrase("Administration -System Users Report", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 30;
cell.Colspan = 10;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
datatable.addCell(cell);
// These cells span 2 rows
datatable.DefaultCellBorderWidth = 2;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 2;
datatable.addCell("User Id");
datatable.addCell(new Phrase("Name", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD)));
datatable.addCell("Company");
datatable.addCell("Department");
// This cell spans the remaining 6 columns in 1 row
datatable.DefaultRowspan = 1;
datatable.DefaultColspan = 6;
datatable.addCell("Permissions");
// These cells span 1 row and 1 column
datatable.DefaultColspan = 1;
datatable.addCell("Admin");
datatable.addCell("Data");
datatable.addCell("Expl");
datatable.addCell("Prod");
datatable.addCell("Proj");
datatable.addCell("Online");
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;
for (int i = 1; i < 30; i++)
{
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
datatable.addCell("myUserId");
datatable.addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");
datatable.addCell("No Name Company");
datatable.addCell("D" + i);
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
}
document.Add(datatable);
}
catch(Exception e)
{
Console.Error.WriteLine(e.StackTrace);
}
// we close the document
document.Close();
}
}
示例代码0510
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0510
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 10: large tables with repeating headers");
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
try
{
// creation of the different writers
PdfWriter.getInstance(document, new FileStream("Chap0510.pdf", FileMode.Create));
// we add some meta information to the document
document.addAuthor("Gerald Henson");
document.addSubject("This is the result of a Test.");
document.Open();
Table datatable = new Table(10);
datatable.Padding = 4;
datatable.Spacing = 0;
//datatable.setBorder(Rectangle.NO_BORDER);
float[] headerwidths = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7};
datatable.Widths = headerwidths;
datatable.WidthPercentage = 100;
// the first cell spans 10 columns
Cell cell = new Cell(new Phrase("Administration -System Users Report", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 30;
cell.Colspan = 10;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
datatable.addCell(cell);
// These cells span 2 rows
datatable.DefaultCellBorderWidth = 2;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 2;
datatable.addCell("User Id");
datatable.addCell(new Phrase("Name", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD)));
datatable.addCell("Company");
datatable.addCell("Department");
// This cell spans the remaining 6 columns in 1 row
datatable.DefaultRowspan = 1;
datatable.DefaultColspan = 6;
datatable.addCell("Permissions");
// These cells span 1 row and 1 column
datatable.DefaultColspan = 1;
datatable.addCell("Admin");
datatable.addCell("Data");
datatable.addCell("Expl");
datatable.addCell("Prod");
datatable.addCell("Proj");
datatable.addCell("Online");
// this is the end of the table header
datatable.endHeaders();
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;
for (int i = 1; i < 30; i++)
{
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
datatable.addCell("myUserId");
datatable.addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");
datatable.addCell("No Name Company");
datatable.addCell("D" + i);
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
}
document.Add(datatable);
}
catch(Exception e)
{
Console.Error.WriteLine(e.StackTrace);
}
// we close the document
document.Close();
}
}
示例代码0511
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0511
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 11: avoid table splitting");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0511.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table table = new Table(3);
table.TableFitsPage = true;
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.addCell(cell);
table.addCell("cell test2");
document.Add(new Paragraph("repeating the same table 10 times:"));
for (int i = 0; i < 10; i++)
{
document.Add(table);
}
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0512
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0512
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 12: avoid cell splitting");
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
try
{
// creation of the different writers
PdfWriter.getInstance(document, new FileStream("Chap0512.pdf", FileMode.Create));
// we add some meta information to the document
document.addAuthor("Gerald Henson");
document.addSubject("This is the result of a Test.");
document.Open();
Table datatable = new Table(10);
datatable.CellsFitPage = true;
datatable.Padding = 4;
datatable.Spacing = 0;
//datatable.setBorder(Rectangle.NO_BORDER);
float[] headerwidths = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7};
datatable.Widths = headerwidths;
datatable.WidthPercentage = 100;
// the first cell spans 10 columns
Cell cell = new Cell(new Phrase("Administration -System Users Report", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 30;
cell.Colspan = 10;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
datatable.addCell(cell);
// These cells span 2 rows
datatable.DefaultCellBorderWidth = 2;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 2;
datatable.addCell("User Id");
datatable.addCell(new Phrase("Name", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD)));
datatable.addCell("Company");
datatable.addCell("Department");
// This cell spans the remaining 6 columns in 1 row
datatable.DefaultRowspan = 1;
datatable.DefaultColspan = 6;
datatable.addCell("Permissions");
// These cells span 1 row and 1 column
datatable.DefaultColspan = 1;
datatable.addCell("Admin");
datatable.addCell("Data");
datatable.addCell("Expl");
datatable.addCell("Prod");
datatable.addCell("Proj");
datatable.addCell("Online");
// this is the end of the table header
datatable.endHeaders();
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;
for (int i = 1; i < 30; i++)
{
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
datatable.addCell("myUserId");
datatable.addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");
datatable.addCell("No Name Company");
datatable.addCell("D" + i);
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
}
document.Add(datatable);
}
catch(Exception e)
{
Console.Error.WriteLine(e.StackTrace);
}
// we close the document
document.Close();
}
}
示例代码0513
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0513
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 13: large tables with fitspage");
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
try
{
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0513.pdf", FileMode.Create));
// we add some meta information to the document
document.addAuthor("Alan Soukup");
document.addSubject("This is the result of a Test.");
document.Open();
Table datatable = getTable();
for (int i = 1; i < 30; i++)
{
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
datatable.addCell("myUserId");
datatable.addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");
datatable.addCell("No Name Company");
datatable.addCell("D" + i);
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
datatable.addCell("No");
datatable.addCell("Yes");
if (!writer.fitsPage(datatable))
{
datatable.deleteLastRow();
i--;
document.Add(datatable);
document.newPage();
datatable = getTable();
}
}
document.Add(datatable);
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
// we close the document
document.Close();
}
private static Table getTable()
{
Table datatable = new Table(10);
datatable.Padding = 4;
datatable.Spacing = 0;
//datatable.setBorder(Rectangle.NO_BORDER);
float[] headerwidths = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7};
datatable.Widths = headerwidths;
datatable.WidthPercentage = 100;
// the first cell spans 10 columns
Cell cell = new Cell(new Phrase("Administration -System Users Report", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 30;
cell.Colspan = 10;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
datatable.addCell(cell);
// These cells span 2 rows
datatable.DefaultCellBorderWidth = 2;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 2;
datatable.addCell("User Id");
datatable.addCell(new Phrase("Name", FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD)));
datatable.addCell("Company");
datatable.addCell("Department");
// This cell spans the remaining 6 columns in 1 row
datatable.DefaultRowspan = 1;
datatable.DefaultColspan = 6;
datatable.addCell("Permissions");
// These cells span 1 row and 1 column
datatable.DefaultColspan = 1;
datatable.addCell("Admin");
datatable.addCell("Data");
datatable.addCell("Expl");
datatable.addCell("Prod");
datatable.addCell("Proj");
datatable.addCell("Online");
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;
return datatable;
}
}
示例代码0514
using System;
using System.Drawing;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0514
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 14: nested tables");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0514.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
// simple example
Table secondTable = new Table(2);
secondTable.addCell("2nd table 0.0");
secondTable.addCell("2nd table 0.1");
secondTable.addCell("2nd table 1.0");
secondTable.addCell("2nd table 1.1");
Table aTable = new Table(4,4); // 4 rows, 4 columns
aTable.AutoFillEmptyCells = true;
aTable.addCell("2.2", new Point(2,2));
aTable.addCell("3.3", new Point(3,3));
aTable.addCell("2.1", new Point(2,1));
aTable.insertTable(secondTable, new Point(1,3));
document.Add(aTable);
// example with 2 nested tables
Table thirdTable = new Table(2);
thirdTable.addCell("3rd table 0.0");
thirdTable.addCell("3rd table 0.1");
thirdTable.addCell("3rd table 1.0");
thirdTable.addCell("3rd table 1.1");
aTable = new Table(5,5);
aTable.AutoFillEmptyCells = true;
aTable.addCell("2.2", new Point(2,2));
aTable.addCell("3.3", new Point(3,3));
aTable.addCell("2.1", new Point(2,1));
aTable.insertTable(secondTable, new Point(1,3));
aTable.insertTable(thirdTable, new Point(6,2));
document.Add(aTable);
// relative column widths are preserved
Table a = new Table( 2 );
a.Widths = new float[] { 85, 15 };
a.addCell("a-1");
a.addCell("a-2");
Table b = new Table(5);
b.Widths = new float[] { 15, 7, 7, 7, 7 };
b.addCell("b-1");
b.addCell("b-2");
b.addCell("b-3");
b.addCell("b-4");
b.addCell("b-5");
// now, insert these 2 tables into a third for layout purposes
Table c = new Table( 3, 1 );
c.WidthPercentage = 100.0f;
c.Widths = new float[] { 20, 2, 78 };
c.insertTable(a, new Point(0,0) );
c.insertTable(b, new Point(0,2) );
document.Add(c);
// adding extra cells after adding a table
Table t1 = new Table(3);
t1.addCell("1.1");
t1.addCell("1.2");
t1.addCell("1.3");
// nested
Table t2 = new Table(2);
t2.addCell("2.1");
t2.addCell("2.2");
// now insert the nested
t1.insertTable(t2);
t1.addCell("new cell"); // correct row/column ?
document.Add(t1);
// deep nesting
t1=new Table(2,2);
for (int i = 0; i < 4; i++)
{
t1.addCell("t1");
}
t2=new Table(3,3);
for (int i = 0; i < 9; i++)
{
if (i == 4) t2.insertTable(t1);
else t2.addCell("t2");
}
Table t3=new Table(4,4);
for (int i = 0; i < 16; i++)
{
if (i == 10) t3.insertTable(t2);
else t3.addCell("t3");
}
document.Add(t3);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0515
using System;
using System.Drawing;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0515
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 15: nested tables");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0515.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table secondTable = new Table(2);
secondTable.addCell("2.0.0");
secondTable.addCell("2.0.1");
secondTable.addCell("2.1.0");
secondTable.addCell("2.1.1");
Cell tableCell = new Cell(secondTable);
Table aTable = new Table(3,3); // 3 rows, 3 columns
aTable.addCell("0.0", new Point(0,0));
aTable.addCell("0.1", new Point(0,1));
aTable.addCell("0.2", new Point(0,2));
aTable.addCell("0.0", new Point(1,0));
aTable.addCell(tableCell, new Point(1,1));
aTable.addCell("2.2", new Point(1,2));
aTable.addCell("2.0", new Point(2,0));
aTable.addCell("2.1", new Point(2,1));
aTable.addCell("2.2", new Point(2,2));
document.Add(aTable);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0516
using System;
using System.Drawing;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0516
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 16: nested tables");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4.rotate());
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0516.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table secondTable = new Table(2);
secondTable.addCell("2nd table 0.0");
secondTable.addCell("2nd table 0.1");
secondTable.addCell("2nd table 1.0");
secondTable.addCell("2nd table 1.1");
Cell tableCell = new Cell("This is a nested table");
tableCell.Add(secondTable);
Table aTable = new Table(3,3); // 3 rows, 3 columns
aTable.addCell("0.0", new Point(0,0));
aTable.addCell("0.1", new Point(0,1));
aTable.addCell("0.2", new Point(0,2));
aTable.addCell("0.0", new Point(1,0));
aTable.addCell(tableCell, new Point(1,1));
aTable.addCell("2.2", new Point(1,2));
aTable.addCell("2.0", new Point(2,0));
aTable.addCell("2.1", new Point(2,1));
aTable.addCell("2.2", new Point(2,2));
document.Add(aTable);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0517
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0517
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 17: table offset");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0517.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.addCell(cell);
table.addCell("cell test2");
document.Add(new Paragraph("repeating the same table 10 times, but with different offsets:"));
document.Add(table);
document.Add(new Paragraph("blah blah."));
document.Add(table);
document.Add(new Paragraph("we increase the offset."));
table.Offset = 32;
document.Add(table);
document.Add(new Paragraph("blah blah."));
document.Add(table);
document.Add(new Paragraph("blah blah."));
document.Add(table);
document.Add(new Paragraph("we use an offset 0."));
table.Offset = 0;
document.Add(table);
document.Add(new Paragraph("blah blah."));
document.Add(table);
document.Add(new Paragraph("blah blah."));
document.Add(table);
document.Add(new Paragraph("A negative offset."));
table.Offset = -16;
document.Add(table);
document.Add(new Paragraph("blah blah."));
document.Add(table);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0518
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0518
{
public static void Main()
{
Console.WriteLine("Chapter 5 example 18: PdfPTable");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
try
{
// step 2: we create a writer that listens to the document
PdfWriter.getInstance(document, new FileStream("Chap0518.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content to the document (this happens in a seperate method)
loadDocument(document);
}
catch (Exception e2)
{
Console.WriteLine(e2);
}
// step 5: we close the document
document.Close();
}
public static void loadDocument(Document document)
{
String[] bogusData = { "M0065920",
"SL",
"FR86000P",
"PCGOLD",
"119000",
"96 06",
"2001-08-13",
"4350",
"6011648299",
"FLFLMTGP",
"153",
"119000.00"
};
int NumColumns = 12;
try
{
// we add some meta information to the document
PdfPTable datatable = new PdfPTable(NumColumns);
datatable.DefaultCell.Padding = 3;
float[] headerwidths = {9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10}; // percentage
datatable.setWidths(headerwidths);
datatable.WidthPercentage = 100; // percentage
datatable.DefaultCell.BorderWidth = 2;
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
datatable.addCell("Clock #");
datatable.addCell("Trans Type");
datatable.addCell("Cusip");
datatable.addCell("Long Name");
datatable.addCell("Quantity");
datatable.addCell("Fraction Price");
datatable.addCell("Settle Date");
datatable.addCell("Portfolio");
datatable.addCell("ADP Number");
datatable.addCell("Account ID");
datatable.addCell("Reg Rep ID");
datatable.addCell("Amt To Go ");
datatable.HeaderRows = 1; // this is the end of the table header
datatable.DefaultCell.BorderWidth = 1;
int max = 666;
for (int i = 1; i < max; i++)
{
if (i % 2 == 1)
{
datatable.DefaultCell.GrayFill = 0.9f;
}
for (int x = 0; x < NumColumns; x++)
{
datatable.addCell(bogusData[x]);
}
if (i % 2 == 1)
{
datatable.DefaultCell.GrayFill = 0.0f;
}
}
document.Add(datatable);
}
catch(Exception e)
{
Console.Error.WriteLine(e.StackTrace);
}
}
}
示例代码0601
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0601
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 1: Adding a Wmf, Gif, Jpeg and Png-file using urls");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0601.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
Image wmf = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/harbour.wmf"));
Image gif = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/vonnegut.gif"));
Image jpeg = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/myKids.jpg"));
Image png = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/hitchcock.png"));
document.Add(wmf);
document.Add(gif);
document.Add(jpeg);
document.Add(png);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0602
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0602
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 2: Adding a Wmf, Gif, Jpeg and Png-file using filenames");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0602.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
Image gif = Image.getInstance("vonnegut.gif");
Image jpeg = Image.getInstance("myKids.jpg");
Image png = Image.getInstance("hitchcock.png");
document.Add(gif);
document.Add(jpeg);
document.Add(png);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0603
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0603
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 3: using a relative path for HTML");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0603.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content
Image jpg = Image.getInstance("raf.jpg");
jpg.scalePercent(50);
document.Add(jpg);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0604
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0604
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 4: Alignment of images");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0604.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
Image gif = Image.getInstance("vonnegut.gif");
gif.Alignment = Image.RIGHT;
Image jpeg = Image.getInstance("myKids.jpg");
jpeg.Alignment = Image.MIDDLE;
Image png = Image.getInstance("hitchcock.png");
png.Alignment = Image.LEFT;
document.Add(gif);
document.Add(jpeg);
document.Add(png);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0605
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0605
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 5: Alignment of images");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0605.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
Image gif = Image.getInstance("vonnegut.gif");
gif.Alignment = Image.RIGHT | Image.TEXTWRAP;
Image jpeg = Image.getInstance("myKids.jpg");
jpeg.Alignment = Image.MIDDLE;
Image png = Image.getInstance("hitchcock.png");
png.Alignment = Image.LEFT | Image.UNDERLYING;
for (int i = 0; i < 100; i++)
{
document.Add(new Phrase("Who is this? "));
}
document.Add(gif);
for (int i = 0; i < 100; i++)
{
document.Add(new Phrase("Who is this? "));
}
document.Add(jpeg);
for (int i = 0; i < 100; i++)
{
document.Add(new Phrase("Who is this? "));
}
document.Add(png);
for (int i = 0; i < 100; i++)
{
document.Add(new Phrase("Who is this? "));
}
document.Add(gif);
for (int i = 0; i < 100; i++)
{
document.Add(new Phrase("Who is this? "));
}
document.Add(jpeg);
for (int i = 0; i < 100; i++)
{
document.Add(new Phrase("Who is this? "));
}
document.Add(png);
for (int i = 0; i < 100; i++)
{
document.Add(new Phrase("Who is this? "));
}
}
catch(DocumentException de)
{
Console.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0606
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0606
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 6: Absolute Positioning of an Image");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0606.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content
Image png = Image.getInstance("hitchcock.png");
png.setAbsolutePosition(171, 250);
document.Add(png);
png.setAbsolutePosition(342, 500);
document.Add(png);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0607
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0607
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 7: Scaling an Image");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0607.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content
Image jpg1 = Image.getInstance("myKids.jpg");
jpg1.scaleAbsolute(97, 101);
document.Add(new Paragraph("scaleAbsolute(97, 101)"));
document.Add(jpg1);
Image jpg2 = Image.getInstance("myKids.jpg");
jpg2.scalePercent(50);
document.Add(new Paragraph("scalePercent(50)"));
document.Add(jpg2);
Image jpg3 = Image.getInstance("myKids.jpg");
jpg3.scaleAbsolute(194, 101);
document.Add(new Paragraph("scaleAbsolute(194, 101)"));
document.Add(jpg3);
Image jpg4 = Image.getInstance("myKids.jpg");
jpg4.scalePercent(100, 50);
document.Add(new Paragraph("scalePercent(100, 50)"));
document.Add(jpg4);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0608
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0608
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 8: Rotating an Image");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0608.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content
Image jpg = Image.getInstance("myKids.jpg");
jpg.Alignment = Image.MIDDLE;
jpg.setRotation((float)Math.PI / 6);
document.Add(new Paragraph("rotate 30 degrees"));
document.Add(jpg);
document.newPage();
jpg.setRotation((float)Math.PI / 4);
document.Add(new Paragraph("rotate 45 degrees"));
document.Add(jpg);
document.newPage();
jpg.setRotation((float)Math.PI / 2);
document.Add(new Paragraph("rotate pi/2 radians"));
document.Add(jpg);
document.newPage();
jpg.setRotation((float)(Math.PI * 0.75));
document.Add(new Paragraph("rotate 135 degrees"));
document.Add(jpg);
document.newPage();
jpg.setRotation((float)Math.PI);
document.Add(new Paragraph("rotate pi radians"));
document.Add(jpg);
document.newPage();
jpg.setRotation((float)(2.0 * Math.PI));
document.Add(new Paragraph("rotate 2 x pi radians"));
document.Add(jpg);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0609
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0609
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 9: bytes() / Raw Image");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0609.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content (example by Paulo Soares)
// creation a jpeg passed as an array of bytes to the Image
FileStream rf = new FileStream("mykids.jpg", FileMode.Open, FileAccess.Read);
int size = (int)rf.Length;
byte[] imext = new byte[size];
rf.Read(imext, 0, size);
rf.Close();
Image img1 = Image.getInstance(imext);
img1.setAbsolutePosition(50, 500);
document.Add(img1);
// creation of an image of 100 x 100 pixels (x 3 bytes for the Red, Green and Blue value)
byte[] data = new byte[100*100*3];
for (int k = 0; k < 100; ++k)
{
for (int j = 0; j < 300; j += 3)
{
data[k * 300 + j] = (byte)(255 * Math.Sin(j * .5 * Math.PI / 300));
data[k * 300 + j + 1] = (byte)(256 - j * 256 / 300);
data[k * 300 + j + 2] = (byte)(255 * Math.Cos(k * .5 * Math.PI / 100));
}
}
Image img2 = Image.getInstance(100, 100, 3, 8, data);
img2.setAbsolutePosition(200, 200);
document.Add(img2);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0610
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0610
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 10: Images using System.Drawing.Bitmap!");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0610.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content to the document
for (int i = 0; i < 300; i++)
{
document.Add(new Phrase("Who is this? "));
}
PdfContentByte cb = writer.DirectContent;
Image image = Image.getInstance(new System.Drawing.Bitmap("h.gif"), null);
image.setAbsolutePosition(100, 200);
cb.addImage(image);
}
catch(DocumentException de)
{
Console.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0611
using System;
using System.IO;
using System.Drawing.Imaging;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0611
{
public static void Main()
{
// creation of the document with a certain size and certain margins
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//Document.compress = false;
try
{
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0611.pdf", FileMode.Create));
System.Drawing.Bitmap bm = new System.Drawing.Bitmap("338814-00.tif");
int total = bm.GetFrameCount(FrameDimension.Page);
Console.WriteLine("Number of images in this TIFF: " + total);
// Which of the multiple images in the TIFF file do we want to load
// 0 refers to the first, 1 to the second and so on.
document.Open();
PdfContentByte cb = writer.DirectContent;
for (int k = 0; k < total; ++k)
{
bm.SelectActiveFrame(FrameDimension.Page, k);
Image img = Image.getInstance(bm, null, true);
img.scalePercent(72f / 200f * 100);
img.setAbsolutePosition(0, 0);
Console.WriteLine("Image: " + k);
cb.addImage(img);
document.newPage();
}
document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
}
}
示例代码0613
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0613
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 13: masked images");
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0613.pdf", FileMode.Create));
document.Open();
Paragraph p = new Paragraph("Some text behind a masked image.");
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
document.Add(p);
PdfContentByte cb = writer.DirectContent;
byte[] maskr = {(byte)0x3c, (byte)0x7e, (byte)0xe7, (byte)0xc3, (byte)0xc3, (byte)0xe7, (byte)0x7e, (byte)0x3c};
Image mask = Image.getInstance(8, 8, 1, 1, maskr);
mask.makeMask();
mask.InvertMask = true;
Image image = Image.getInstance("vonnegut.gif");
image.ImageMask = mask;
image.setAbsolutePosition(60, 620);
// explicit masking
cb.addImage(image);
// stencil masking
cb.setRGBColorFill(255, 0, 0);
cb.addImage(mask, mask.ScaledWidth * 8, 0, 0, mask.ScaledHeight * 8, 100, 400);
cb.setRGBColorFill(0, 255, 0);
cb.addImage(mask, mask.ScaledWidth * 8, 0, 0, mask.ScaledHeight * 8, 200, 400);
cb.setRGBColorFill(0, 0, 255);
cb.addImage(mask, mask.ScaledWidth * 8, 0, 0, mask.ScaledHeight * 8, 300, 400);
document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.StackTrace);
}
}
}
示例代码0614
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0614
{
public static void Main()
{
Document.compress = false;
Console.WriteLine("Chapter 6 example 14: images wrapped in a Chunk");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0614.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Image img = Image.getInstance("pngnow.png");
img.scalePercent(70);
Chunk ck = new Chunk(img, 0, -5);
Table table = new Table(3);
table.WidthPercentage = 100;
table.Padding = 2;
table.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
Cell cell = new Cell(new Chunk(img, 0, -13));
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.addCell("I see an image on my right");
table.addCell(cell);
table.addCell("I see an image on my left");
table.addCell(cell);
table.addCell("I see images everywhere");
table.addCell(cell);
table.addCell("I see an image on my right");
table.addCell(cell);
table.addCell("I see an image on my left");
Phrase p1 = new Phrase("This is an image ");
p1.Add(ck);
p1.Add(" just here.");
document.Add(p1);
document.Add(p1);
document.Add(p1);
document.Add(p1);
document.Add(p1);
document.Add(p1);
document.Add(p1);
document.Add(table);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0615
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0615
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 15: images in tables");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0615.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Image img0 = Image.getInstance("myKids.jpg");
img0.Alignment = Image.MIDDLE;
Image img1 = Image.getInstance("pngnow.png");
img1.Alignment = Image.LEFT | Image.UNDERLYING;
Image img2 = Image.getInstance("pngnow.png");
img2.Alignment = Image.RIGHT | Image.TEXTWRAP;
Image img3 = Image.getInstance("pngnow.png");
img3.Alignment = Image.LEFT;
Image img4 = Image.getInstance("pngnow.png");
img4.Alignment = Image.MIDDLE;
Image img5 = Image.getInstance("pngnow.png");
img5.Alignment = Image.RIGHT;
Table table = new Table(3);
table.Padding = 2;
table.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
// row 1
table.addCell("I see an image on my right");
Cell cell = new Cell("This is the image (aligned in the middle):");
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
cell.Add(img0);
cell.Add(new Phrase("This was the image"));
table.addCell(cell);
table.addCell("I see an image on my left");
// row 2
cell = new Cell("This is the image (left aligned):");
cell.Add(img1);
cell.Add(new Phrase("This was the image"));
table.addCell(cell);
table.addCell("I see images everywhere");
cell = new Cell("This is the image (right aligned):");
cell.Add(img2);
cell.Add(new Phrase("This was the image"));
table.addCell(cell);
// row 3
table.addCell("I see an image on my right");
cell = new Cell(img3);
cell.Add(img4);
cell.Add(img5);
table.addCell(cell);
table.addCell("I see an image on my left");
document.Add(table);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0616
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap0616
{
public static void Main()
{
Console.WriteLine("Chapter 6 example 16: images and annotations");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
// step 2:
// we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0616.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add some content
Image wmf = Image.getInstance(new Uri("http://itext.sourceforge.net/examples/harbour.wmf"));
wmf.Annotation = new Annotation(0, 0, 0, 0, "http://www.lowagie.com");
wmf.setAbsolutePosition(100f, 600f);
document.Add(wmf);
Image gif = Image.getInstance(new Uri("http://itext.sourceforge.net/examples/vonnegut.gif"));
gif.Annotation = new Annotation(0, 0, 0, 0, "Chap0610.pdf", 3);
gif.setAbsolutePosition(100f, 400f);
document.Add(gif);
Image jpeg = Image.getInstance(new Uri("http://itext.sourceforge.net/examples/myKids.jpg"));
jpeg.Annotation = new Annotation("picture", "These are my children", 0, 0, 0, 0);
jpeg.setAbsolutePosition(100f, 150f);
document.Add(jpeg);
Image png = Image.getInstance(new Uri("http://itext.sourceforge.net/examples/hitchcock.png"));
png.Annotation = new Annotation(0, 0, 0, 0, "Chap0610.pdf", "test");
png.setAbsolutePosition(350f, 250f);
document.Add(png);
}
catch (Exception de)
{
Console.Error.WriteLine(de.StackTrace);
}
// step 5: we close the document
document.Close();
}
}
示例代码0702
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.xml;
using iTextSharp.text.pdf;
public class Chap0702
{
public static void Main()
{
Console.WriteLine("Chapter 7 example 2: parsing the result of example 1");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter.getInstance(document, new FileStream("Chap0702.pdf", FileMode.Create));
// step 3: we create a parser
iTextHandler h = new iTextHandler(document);
// step 4: we parse the document
h.Parse("Chap0701.xml");
}
catch (Exception e)
{
Console.Error.WriteLine(e.StackTrace);
Console.Error.WriteLine(e.Message);
}
}
}
示例代码0801
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.rtf;
public class Chap0801
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 8 example 1: Hello World");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a RTF-stream to a file
RtfWriter.getInstance(document, new FileStream("Chap0801.rtf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add a paragraph to the document
document.Add(new Paragraph("Hello World"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0802
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.rtf;
/**
* This example creates a RTF document with two chapters and different headers
* for both chapters. The same works for footers. Just replace setHeader with
* setFooter.
*
* @author <a href="mailto:mhall@myrealbox.com">Mark.Hall@myrealbox.com</a>
*/
public class Chap0802
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 8 example 2: Headers in RTF");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
RtfWriter.getInstance(document, new FileStream("Chap0802.rtf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create two chapters and add the same content to both.
Paragraph par = new Paragraph("This is some sample content.");
Chapter chap1 = new Chapter("Chapter 1", 1);
chap1.Add(par);
Chapter chap2 = new Chapter("Chapter 2", 2);
chap2.Add(par);
// step 5: we create the header for the first chapter, set the header and
// then add the first chapter.
HeaderFooter hf1 = new HeaderFooter(new Phrase("This is chapter 1"), false);
document.Header = hf1;
document.Add(chap1);
// step 6: we create a second header, set this one and then add the second
// chapter.
HeaderFooter hf2 = new HeaderFooter(new Phrase("This is chapter 2"), false);
document.Header = hf2;
document.Add(chap2);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 7: we close the document
document.Close();
}
}
示例代码0803
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.rtf;
/** * This example creates a RTF document with more complex headers and footers
* * using the RtfHeaderFooters class.
* * @author Mark.Hall@myrealbox.com
* */
public class Chap0803
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 8 example 3: RTF with the RtfHeaderFooters class");
/* Create a new document */
Document document = new Document(PageSize.A4);
try
{
/* Create a RtfWriter and a PdfWriter for the document */
RtfWriter rtf = RtfWriter.getInstance(document, new FileStream("Chap0803.rtf", FileMode.Create));
/* We specify that the RTF file has a Title Page */
rtf.HasTitlePage = true;
/* We create headers and footers for the RTF file */
RtfHeaderFooters header = new RtfHeaderFooters();
RtfHeaderFooters footer = new RtfHeaderFooters();
/* We add a header that will only appear on the first page */
header.Set(RtfHeaderFooters.FIRST_PAGE, new HeaderFooter(new Phrase("This header is only on the first page"), false));
/* We add a header that will only appear on left-side pages */
header.Set(RtfHeaderFooters.LEFT_PAGES, new HeaderFooter(new Phrase("This header is only on left pages"), false));
/* We add a header that will only appear on right-side pages */
header.Set(RtfHeaderFooters.RIGHT_PAGES, new HeaderFooter(new Phrase("This header is only on right pages. "), false));
/* We add a footer that will appear on all pages except the first (because of the title page)
* Because the header has different left and right page footers, we have to add the footer
* to both the left and right pages. */
footer.Set(RtfHeaderFooters.LEFT_PAGES, new HeaderFooter(new Phrase("This footer is on all pages except the first. Page: "), true));
footer.Set(RtfHeaderFooters.RIGHT_PAGES, new HeaderFooter(new Phrase("This footer is on all pages except the first. Page: "), true));
/* Open the document */
document.Open();
/* We add the header and footer */
document.Header = header;
document.Footer = footer;
/* We add some content */
Chapter chapter = new Chapter(new Paragraph("Advanced RTF headers and footers", new Font(Font.HELVETICA, 16, Font.BOLD)), 1);
chapter.Add(new Paragraph("This document demonstrates the use of advanced RTF headers and footers."));
for(int i = 0;
i < 300;
i++)
{
chapter.Add(new Paragraph("Line " + i));
} document.Add(chapter);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
/* Close the document */
document.Close();
}
}
示例代码0804
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.rtf;
public class Chap0804
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 8 example 4: Tables and RTF");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
RtfWriter.getInstance(document, new FileStream("Chap0804.rtf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we create a table and add it to the document
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.addCell(cell);
table.addCell("cell test2");
document.Add(table);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码0901
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class FontTest
{
public static void Main()
{
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("FontTest.pdf",FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add content to the document
BaseFont bfHei = BaseFont.createFont(@"c:winntfontsSIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfHei, 32);
String text = "这是黑体字测试!";
document.Add(new Paragraph(text, font));
BaseFont bfSun=BaseFont.createFont(@"c:winntfontsSIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
font = new Font(bfSun, 16);
text = "这是字体集合中的新宋体测试!";
document.Add(new Paragraph(text, font));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码1001
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class FontTest
{
public static void Main()
{
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileStream("FontTest.pdf",FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add content to the document
BaseFont bfHei = BaseFont.createFont(@"c:winntfontsSIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfHei, 32);
String text = "这是黑体字测试!";
document.Add(new Paragraph(text, font));
BaseFont bfSun=BaseFont.createFont(@"c:winntfontsSIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
font = new Font(bfSun, 16);
text = "这是字体集合中的新宋体测试!";
document.Add(new Paragraph(text, font));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码1002
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1002
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 2: Text at absolute positions");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1002.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
// first we draw some lines to be able to visualize the text alignment functions
cb.LineWidth = 0f;
cb.moveTo(250, 500);
cb.lineTo(250, 800);
cb.moveTo(50, 700);
cb.lineTo(400, 700);
cb.moveTo(50, 650);
cb.lineTo(400, 650);
cb.moveTo(50, 600);
cb.lineTo(400, 600);
cb.stroke();
// we tell the ContentByte we're ready to draw text
cb.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.setFontAndSize(bf, 12);
String text = "Sample text for alignment";
// we show some text starting on some absolute position with a given alignment
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, 700, 0);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, 650, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, 600, 0);
// we draw some text on a certain position
cb.setTextMatrix(100, 400);
cb.showText("Text at position 100,400.");
// we draw some rotated text on a certain position
cb.setTextMatrix(0, 1, -1, 0, 100, 300);
cb.showText("Text at position 100,300, rotated 90 degrees.");
// we draw some mirrored, rotated text on a certain position
cb.setTextMatrix(0, 1, 1, 0, 200, 200);
cb.showText("Text at position 200,200, mirrored and rotated 90 degrees.");
// we tell the contentByte, we've finished drawing text
cb.endText();
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码1003
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1003
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 3: Templates");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1003.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(500, 200);
// we add some graphics
template.moveTo(0, 200);
template.lineTo(500, 0);
template.stroke();
template.setRGBColorStrokeF(255f, 0f, 0f);
template.circle(250f, 100f, 80f);
template.stroke();
// we add some text
template.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
template.setFontAndSize(bf, 12);
template.setTextMatrix(100, 100);
template.showText("Text at the position 100,100 (relative to the template!)");
template.endText();
// we add the template on different positions
cb.addTemplate(template, 0, 0);
cb.addTemplate(template, 0, 1, -1, 0, 500, 200);
cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);
// we go to a new page
document.newPage();
cb.addTemplate(template, 0, 400);
cb.addTemplate(template, 2, 0, 0, 2, -200, 400);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码1004
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1004
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 4: Templates");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1004.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(50, 50);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
// we add a number of pages
int i;
for (i = 1; i < 5; i++)
{
String text = "Page " + writer.PageNumber + " of ";
float len = bf.getWidthPoint(text, 12);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.setTextMatrix(280, 40);
cb.showText(text);
cb.endText();
cb.addTemplate(template, 280 + len, 40);
document.newPage();
}
template.beginText();
template.setFontAndSize(bf, 12);
template.showText((writer.PageNumber - 1).ToString());
template.endText();
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码1005
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1005
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 5: Simple Columns");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1005.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4:
// we create some content
BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 11, Font.NORMAL);
Phrase unicodes = new Phrase(15, "UNI ", font);
Phrase characters = new Phrase(15, " ", font);
Phrase names = new Phrase(15, "NAME ", font);
for (int i = 0; i < 27; i++)
{
unicodes.Add(uni[i] + " ");
characters.Add(code[i] + " ");
names.Add(name[i] + " ");
}
// we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(unicodes, 60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
ct.go();
cb.rectangle(103, 295, 52, 8 + 28 * 15);
cb.stroke();
ct.setSimpleColumn(characters, 105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
ct.go();
ct.setSimpleColumn(names, 160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
ct.go();
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
public static String[] uni = new String[27];
public static String[] code = new String[27];
public static String[] name = new String[27];
static Chap1005()
{
uni[0]="\u0152";
code[0]="u0152";
name[0]="LATIN CAPITAL LIGATURE OE";
uni[1]="\u0153";
code[1]="u0153";
name[1]="LATIN SMALL LIGATURE OE";
uni[2]="\u0160";
code[2]="u0160";
name[2]="LATIN CAPITAL LETTER S WITH CARON";
uni[3]="\u0161";
code[3]="u0161";
name[3]="LATIN SMALL LETTER S WITH CARON";
uni[4]="\u0178";
code[4]="u0178";
name[4]="LATIN CAPITAL LETTER Y WITH DIAERESIS";
uni[5]="\u017D";
code[5]="u017D";
name[5]="LATIN CAPITAL LETTER Z WITH CARON";
uni[6]="\u017E";
code[6]="u017E";
name[6]="LATIN SMALL LETTER Z WITH CARON";
uni[7]="\u0192";
code[7]="u0192";
name[7]="LATIN SMALL LETTER F WITH HOOK";
uni[8]="\u02C6";
code[8]="u02C6";
name[8]="MODIFIER LETTER CIRCUMFLEX ACCENT";
uni[9]="\u02DC";
code[9]="u02DC";
name[9]="SMALL TILDE";
uni[10]="\u2013";
code[10]="u2013";
name[10]="EN DASH";
uni[11]="\u2014";
code[11]="u2014";
name[11]="EM DASH";
uni[12]="\u2018";
code[12]="u2018";
name[12]="LEFT SINGLE QUOTATION MARK";
uni[13]="\u2019";
code[13]="u2019";
name[13]="RIGHT SINGLE QUOTATION MARK";
uni[14]="\u201A";
code[14]="u201A";
name[14]="SINGLE LOW-9 QUOTATION MARK";
uni[15]="\u201C";
code[15]="u201C";
name[15]="LEFT DOUBLE QUOTATION MARK";
uni[16]="\u201D";
code[16]="u201D";
name[16]="RIGHT DOUBLE QUOTATION MARK";
uni[17]="\u201E";
code[17]="u201E";
name[17]="DOUBLE LOW-9 QUOTATION MARK";
uni[18]="\u2020";
code[18]="u2020";
name[18]="DAGGER";
uni[19]="\u2021";
code[19]="u2021";
name[19]="DOUBLE DAGGER";
uni[20]="\u2022";
code[20]="u2022";
name[20]="BULLET";
uni[21]="\u2026";
code[21]="u2026";
name[21]="HORIZONTAL ELLIPSIS";
uni[22]="\u2030";
code[22]="u2030";
name[22]="PER MILLE SIGN";
uni[23]="\u2039";
code[23]="u2039";
name[23]="SINGLE LEFT-POINTING ANGLE QUOTATION MARK";
uni[24]="\u203A";
code[24]="u203A";
name[24]="SINGLE RIGHT-POINTING ANGLE QUOTATION MARK";
uni[25]="\u20AC";
code[25]="u20AC";
name[25]="EURO SIGN";
uni[26]="\u2122";
code[26]="u2122";
name[26]="TRADE MARK SIGN";
}
}
示例代码1006
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1006
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 6: Simple Columns");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1006.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4:
// we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 11, Font.NORMAL);
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
ct.addText(new Phrase(15, "UNI ", font));
for (int i = 0; i < 27; i++)
{
ct.addText(new Phrase(15, uni[i] + " ", font));
}
ct.go();
cb.rectangle(103, 295, 52, 8 + 28 * 15);
cb.stroke();
ct.setSimpleColumn(105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
ct.addText(new Phrase(15, "char ", font));
for (int i = 0; i < 27; i++)
{
ct.addText(new Phrase(15, code[i] + " ", font));
}
ct.go();
ct.setSimpleColumn(160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
ct.addText(new Phrase(15, "NAME ", font));
for (int i = 0; i < 27; i++)
{
ct.addText(new Phrase(15, name[i] + " ", font));
}
ct.go();
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
public static String[] uni = new String[27];
public static String[] code = new String[27];
public static String[] name = new String[27];
static Chap1006()
{
uni[0]="\u0152";
code[0]="u0152";
name[0]="LATIN CAPITAL LIGATURE OE";
uni[1]="\u0153";
code[1]="u0153";
name[1]="LATIN SMALL LIGATURE OE";
uni[2]="\u0160";
code[2]="u0160";
name[2]="LATIN CAPITAL LETTER S WITH CARON";
uni[3]="\u0161";
code[3]="u0161";
name[3]="LATIN SMALL LETTER S WITH CARON";
uni[4]="\u0178";
code[4]="u0178";
name[4]="LATIN CAPITAL LETTER Y WITH DIAERESIS";
uni[5]="\u017D";
code[5]="u017D";
name[5]="LATIN CAPITAL LETTER Z WITH CARON";
uni[6]="\u017E";
code[6]="u017E";
name[6]="LATIN SMALL LETTER Z WITH CARON";
uni[7]="\u0192";
code[7]="u0192";
name[7]="LATIN SMALL LETTER F WITH HOOK";
uni[8]="\u02C6";
code[8]="u02C6";
name[8]="MODIFIER LETTER CIRCUMFLEX ACCENT";
uni[9]="\u02DC";
code[9]="u02DC";
name[9]="SMALL TILDE";
uni[10]="\u2013";
code[10]="u2013";
name[10]="EN DASH";
uni[11]="\u2014";
code[11]="u2014";
name[11]="EM DASH";
uni[12]="\u2018";
code[12]="u2018";
name[12]="LEFT SINGLE QUOTATION MARK";
uni[13]="\u2019";
code[13]="u2019";
name[13]="RIGHT SINGLE QUOTATION MARK";
uni[14]="\u201A";
code[14]="u201A";
name[14]="SINGLE LOW-9 QUOTATION MARK";
uni[15]="\u201C";
code[15]="u201C";
name[15]="LEFT DOUBLE QUOTATION MARK";
uni[16]="\u201D";
code[16]="u201D";
name[16]="RIGHT DOUBLE QUOTATION MARK";
uni[17]="\u201E";
code[17]="u201E";
name[17]="DOUBLE LOW-9 QUOTATION MARK";
uni[18]="\u2020";
code[18]="u2020";
name[18]="DAGGER";
uni[19]="\u2021";
code[19]="u2021";
name[19]="DOUBLE DAGGER";
uni[20]="\u2022";
code[20]="u2022";
name[20]="BULLET";
uni[21]="\u2026";
code[21]="u2026";
name[21]="HORIZONTAL ELLIPSIS";
uni[22]="\u2030";
code[22]="u2030";
name[22]="PER MILLE SIGN";
uni[23]="\u2039";
code[23]="u2039";
name[23]="SINGLE LEFT-POINTING ANGLE QUOTATION MARK";
uni[24]="\u203A";
code[24]="u203A";
name[24]="SINGLE RIGHT-POINTING ANGLE QUOTATION MARK";
uni[25]="\u20AC";
code[25]="u20AC";
name[25]="EURO SIGN";
uni[26]="\u2122";
code[26]="u2122";
name[26]="TRADE MARK SIGN";
}
}
示例代码1007
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1007
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 7: Columns");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1007.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4:
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 11, Font.NORMAL);
// we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
ColumnText ct = new ColumnText(cb);
float[] right = {70, 320};
float[] left = {300, 550};
ct.addText(new Phrase("GALLIA est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes lingua, institutis, legibus inter se differunt. Gallos ab Aquitanis Garumna flumen, a Belgis Matrona et Sequana dividit. Horum omnium fortissimi sunt Belgae, propterea quod a cultu atque humanitate provinciae longissime absunt, minimeque ad eos mercatores saepe commeant atque ea quae ad effeminandos animos pertinent important, proximique sunt Germanis, qui trans Rhenum incolunt, quibuscum continenter bellum gerunt. Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis proeliis cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("[Eorum una, pars, quam Gallos obtinere dictum est, initium capit a flumine Rhodano, continetur Garumna flumine, Oceano, finibus Belgarum, attingit etiam ab Sequanis et Helvetiis flumen Rhenum, vergit ad septentriones. Belgae ab extremis Galliae finibus oriuntur, pertinent ad inferiorem partem fluminis Rheni, spectant in septentrionem et orientem solem. Aquitania a Garumna flumine ad Pyrenaeos montes et eam partem Oceani quae est ad Hispaniam pertinet; spectat inter occasum solis et septentriones.] ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("Apud Helvetios longe nobilissimus fuit et ditissimus Orgetorix. Is M. Messala, [et P.] M. Pisone consulibus regni cupiditate inductus coniurationem nobilitatis fecit et civitati persuasit ut de finibus suis cum omnibus copiis exirent: perfacile esse, cum virtute omnibus praestarent, totius Galliae imperio potiri. Id hoc facilius iis persuasit, quod undique loci natura Helvetii continentur: una ex parte flumine Rheno latissimo atque altissimo, qui agrum Helvetium a Germanis dividit; altera ex parte monte Iura altissimo, qui est inter Sequanos et Helvetios; tertia lacu Lemanno et flumine Rhodano, qui provinciam nostram ab Helvetiis dividit. His rebus fiebat ut et minus late vagarentur et minus facile finitimis bellum inferre possent; qua ex parte homines bellandi cupidi magno dolore adficiebantur. Pro multitudine autem hominum et pro gloria belli atque fortitudinis angustos se fines habere arbitrabantur, qui in longitudinem milia passuum CCXL, in latitudinem CLXXX patebant. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("His rebus adducti et auctoritate Orgetorigis permoti constituerunt ea quae ad proficiscendum pertinerent comparare, iumentorum et carrorum quam maximum numerum coemere, sementes quam maximas facere, ut in itinere copia frumenti suppeteret, cum proximis civitatibus pacem et amicitiam confirmare. Ad eas res conficiendas biennium sibi satis esse duxerunt; in tertium annum profectionem lege confirmant. Ad eas res conficiendas Orgetorix deligitur. Is sibi legationem ad civitates suscipit. In eo itinere persuadet Castico, Catamantaloedis filio, Sequano, cuius pater regnum in Sequanis multos annos obtinuerat et a senatu populi Romani amicus appellatus erat, ut regnum in civitate sua occuparet, quod pater ante habuerit; itemque Dumnorigi Haeduo, fratri Diviciaci, qui eo tempore principatum in civitate obtinebat ac maxime plebi acceptus erat, ut idem conaretur persuadet eique filiam suam in matrimonium dat. Perfacile factu esse illis probat conata perficere, propterea quod ipse suae civitatis imperium obtenturus esset: non esse dubium quin totius Galliae plurimum Helvetii possent; se suis copiis suoque exercitu illis regna conciliaturum confirmat. Hac oratione adducti inter se fidem et ius iurandum dant et regno occupato per tres potentissimos ac firmissimos populos totius Galliae sese potiri posse sperant. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("Ea res est Helvetiis per indicium enuntiata. Moribus suis Orgetoricem ex vinculis causam dicere coegerunt; damnatum poenam sequi oportebat, ut igni cremaretur. Die constituta causae dictionis Orgetorix ad iudicium omnem suam familiam, ad hominum milia decem, undique coegit, et omnes clientes obaeratosque suos, quorum magnum numerum habebat, eodem conduxit; per eos ne causam diceret se eripuit. Cum civitas ob eam rem incitata armis ius suum exequi conaretur multitudinemque hominum ex agris magistratus cogerent, Orgetorix mortuus est; neque abest suspicio, ut Helvetii arbitrantur, quin ipse sibi mortem consciverit.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.Indent = 20;
int status = 0;
int column = 0;
while((status & ColumnText.NO_MORE_TEXT) == 0)
{
Console.WriteLine("page " + writer.PageNumber + " column " + column);
ct.setSimpleColumn(right[column], 60, left[column], 790, 16, Element.ALIGN_JUSTIFIED);
status = ct.go();
if ((status & ColumnText.NO_MORE_COLUMN) != 0)
{
column++;
if (column > 1)
{
document.newPage();
column = 0;
}
}
}
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码1008
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1008
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 8: Irregular columns");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1008.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4:
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 11, Font.NORMAL);
// we grab the contentbyte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
PdfTemplate t = cb.createTemplate(600, 800);
Image caesar = Image.getInstance("caesar_coin.jpg");
cb.addImage(caesar, 100, 0, 0, 100, 260, 595);
t.GrayFill = 0.75f;
t.moveTo(310, 112);
t.lineTo(280, 60);
t.lineTo(340, 60);
t.closePath();
t.moveTo(310, 790);
t.lineTo(310, 710);
t.moveTo(310, 580);
t.lineTo(310, 122);
t.stroke();
cb.addTemplate(t, 0, 0);
ColumnText ct = new ColumnText(cb);
ct.addText(new Phrase("GALLIA est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes lingua, institutis, legibus inter se differunt. Gallos ab Aquitanis Garumna flumen, a Belgis Matrona et Sequana dividit. Horum omnium fortissimi sunt Belgae, propterea quod a cultu atque humanitate provinciae longissime absunt, minimeque ad eos mercatores saepe commeant atque ea quae ad effeminandos animos pertinent important, proximique sunt Germanis, qui trans Rhenum incolunt, quibuscum continenter bellum gerunt. Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis proeliis cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("[Eorum una, pars, quam Gallos obtinere dictum est, initium capit a flumine Rhodano, continetur Garumna flumine, Oceano, finibus Belgarum, attingit etiam ab Sequanis et Helvetiis flumen Rhenum, vergit ad septentriones. Belgae ab extremis Galliae finibus oriuntur, pertinent ad inferiorem partem fluminis Rheni, spectant in septentrionem et orientem solem. Aquitania a Garumna flumine ad Pyrenaeos montes et eam partem Oceani quae est ad Hispaniam pertinet; spectat inter occasum solis et septentriones.] ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("Apud Helvetios longe nobilissimus fuit et ditissimus Orgetorix. Is M. Messala, [et P.] M. Pisone consulibus regni cupiditate inductus coniurationem nobilitatis fecit et civitati persuasit ut de finibus suis cum omnibus copiis exirent: perfacile esse, cum virtute omnibus praestarent, totius Galliae imperio potiri. Id hoc facilius iis persuasit, quod undique loci natura Helvetii continentur: una ex parte flumine Rheno latissimo atque altissimo, qui agrum Helvetium a Germanis dividit; altera ex parte monte Iura altissimo, qui est inter Sequanos et Helvetios; tertia lacu Lemanno et flumine Rhodano, qui provinciam nostram ab Helvetiis dividit. His rebus fiebat ut et minus late vagarentur et minus facile finitimis bellum inferre possent; qua ex parte homines bellandi cupidi magno dolore adficiebantur. Pro multitudine autem hominum et pro gloria belli atque fortitudinis angustos se fines habere arbitrabantur, qui in longitudinem milia passuum CCXL, in latitudinem CLXXX patebant. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("His rebus adducti et auctoritate Orgetorigis permoti constituerunt ea quae ad proficiscendum pertinerent comparare, iumentorum et carrorum quam maximum numerum coemere, sementes quam maximas facere, ut in itinere copia frumenti suppeteret, cum proximis civitatibus pacem et amicitiam confirmare. Ad eas res conficiendas biennium sibi satis esse duxerunt; in tertium annum profectionem lege confirmant. Ad eas res conficiendas Orgetorix deligitur. Is sibi legationem ad civitates suscipit. In eo itinere persuadet Castico, Catamantaloedis filio, Sequano, cuius pater regnum in Sequanis multos annos obtinuerat et a senatu populi Romani amicus appellatus erat, ut regnum in civitate sua occuparet, quod pater ante habuerit; itemque Dumnorigi Haeduo, fratri Diviciaci, qui eo tempore principatum in civitate obtinebat ac maxime plebi acceptus erat, ut idem conaretur persuadet eique filiam suam in matrimonium dat. Perfacile factu esse illis probat conata perficere, propterea quod ipse suae civitatis imperium obtenturus esset: non esse dubium quin totius Galliae plurimum Helvetii possent; se suis copiis suoque exercitu illis regna conciliaturum confirmat. Hac oratione adducti inter se fidem et ius iurandum dant et regno occupato per tres potentissimos ac firmissimos populos totius Galliae sese potiri posse sperant. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
ct.addText(new Phrase("Ea res est Helvetiis per indicium enuntiata. Moribus suis Orgetoricem ex vinculis causam dicere coegerunt; damnatum poenam sequi oportebat, ut igni cremaretur. Die constituta causae dictionis Orgetorix ad iudicium omnem suam familiam, ad hominum milia decem, undique coegit, et omnes clientes obaeratosque suos, quorum magnum numerum habebat, eodem conduxit; per eos ne causam diceret se eripuit. Cum civitas ob eam rem incitata armis ius suum exequi conaretur multitudinemque hominum ex agris magistratus cogerent, Orgetorix mortuus est; neque abest suspicio, ut Helvetii arbitrantur, quin ipse sibi mortem consciverit.", FontFactory.getFont(FontFactory.HELVETICA, 12)));
float[] left1 = {70,790, 70,60};
float[] right1 = {300,790, 300,700, 240,700, 240,590, 300,590, 300,106, 270,60};
float[] left2 = {320,790, 320,700, 380,700, 380,590, 320,590, 320,106, 350,60};
float[] right2 = {550,790, 550,60};
int status = 0;
int column = 0;
while ((status & ColumnText.NO_MORE_TEXT) == 0)
{
if (column == 0)
{
ct.setColumns(left1, right1);
column = 1;
}
else
{
ct.setColumns(left2, right2);
column = 0;
}
status = ct.go();
ct.YLine = 790;
ct.Alignment = Element.ALIGN_JUSTIFIED;
status = ct.go();
if ((column == 0) && ((status & ColumnText.NO_MORE_COLUMN) != 0))
{
document.newPage();
cb.addTemplate(t, 0, 0);
cb.addImage(caesar, 100, 0, 0, 100, 260, 595);
}
}
// step 5: we close the document
document.Close();
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
}
}
示例代码1009
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1009
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 9: a PdfPTable at an absolute position");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1009.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add some content
PdfPTable table = new PdfPTable(4);
table.DefaultCell.Border = Rectangle.LEFT | Rectangle.RIGHT;
for (int k = 0; k < 24; ++k)
{
table.addCell("cell " + k);
}
table.TotalWidth = 300;
table.writeSelectedRows(0, -1, 100, 600, writer.DirectContent);
// step 5: we close the document
document.Close();
}
catch (Exception de)
{
Console.WriteLine(de.Message);
Console.WriteLine(de.StackTrace);
}
}
}
示例代码1010
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1010
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 10: nested PdfPTables");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try
{
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1010.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add some content
PdfPTable table = new PdfPTable(4);
PdfPTable nested1 = new PdfPTable(2);
nested1.addCell("1.1");
nested1.addCell("1.2");
PdfPTable nested2 = new PdfPTable(1);
nested2.addCell("2.1");
nested2.addCell("2.2");
for (int k = 0; k < 24; ++k)
{
if (k == 1)
{
table.addCell(nested1);
}
else if (k == 20)
{
table.addCell(nested2);
}
else
{
table.addCell("cell " + k);
}
}
table.TotalWidth = 300;
table.writeSelectedRows(0, -1, 100, 600, writer.DirectContent);
// step 5: we close the document
document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
}
}
示例代码1011
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1011
{
public static void Main()
{
Console.WriteLine("Chapter 10 example 11: a PdfPTable in a template");
// step 1: creation of a document-object
Rectangle rect = new Rectangle(PageSize.A4);
rect.BackgroundColor = new Color(238, 221, 88);
Document document = new Document(rect, 50, 50, 50, 50);
try
{
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1011.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4:
PdfTemplate template = writer.DirectContent.createTemplate(20, 20);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
String text = "Vertical";
float size = 16;
float width = bf.getWidthPoint(text, size);
template.beginText();
template.setRGBColorFillF(1, 1, 1);
template.setFontAndSize(bf, size);
template.setTextMatrix(0, 2);
template.showText(text);
template.endText();
template.Width = width;
template.Height = size + 2;
Image img = Image.getInstance(template);
img.setRotationDegrees(90);
Chunk ck = new Chunk(img, 0, 0);
PdfPTable table = new PdfPTable(3);
table.WidthPercentage = 100;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
PdfPCell cell = new PdfPCell(img);
cell.Padding = 4;
cell.BackgroundColor = new Color(0, 0, 255);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
table.addCell(cell);
table.addCell("I see a template everywhere");
table.addCell(cell);
table.addCell("I see a template on my right");
table.addCell(cell);
table.addCell("I see a template on my left");
Paragraph p1 = new Paragraph("This is a template ");
p1.Add(ck);
p1.Add(" just here.");
p1.Leading = img.ScaledHeight * 1.1f;
document.Add(p1);
document.Add(table);
Paragraph p2 = new Paragraph("More templates ");
p2.Leading = img.ScaledHeight * 1.1f;
p2.Alignment = Element.ALIGN_JUSTIFIED;
img.scalePercent(70);
for (int k = 0; k < 20; ++k)
p2.Add(ck);
document.Add(p2);
// step 5: we close the document
document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
}
}
示例代码1012
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1012
{
static public String[] headings = {
"Book/Product Model:",
"Sales Handle:",
"Why We Published this Book/Product Model:",
"Key benefits:",
"About the Author(s):",
"Technology/Topic Overview: ",
"Book/Product Content Summary:",
"Audience:",
"What's on the CD/DVD/Web:"
};
static public String[] texts = {
"Ideally, choose one title (2-3 if absolutely necessary) that this book should perform like. Include full title, ISBN, author, and any sell through numbers if possible.",
"One line description about the sales.",
"Brief description (one-two lines) on the importance of this book to the audience.",
"What benefit does this book provide to the consumer? (expert advice, speed, fun, productivity). Why should the Retailer/Wholesaler select this book over its competition? What are the unique features about this book should be highlighted? What makes this book different, better? From other books and the previous edition?",
"What makes this person so special? Is she/he an expert, creator of the technology, educational leader, etc.? What is their background, and what relevant experiences do they have to make them the BEST choice? Have he/she/they won awards or been recognized in any way. Other books poublished by the author. 1. Book one. 2. Book two.",
"In brief two to five line description of the technology, topic or relevant information. Please keep descriptions succinct.",
"Ideal describe the contents of this book. What will this book do for the reader? Will this book help them optimize their system? Increase productivity? offer tips and stragegies?",
"Who is your intended customer? Experts? Power users? Business professionals? Programmers? What are the demographics?",
"What is included on the Cd or Web site? Why is it necessary and what will it do for the purchaser (source code, examples, case studies)? Is there a value that can be associated with what is on the CD/DVD or Web?"
};
public static void Main()
{
Console.WriteLine("Chapter 10 example 12: PdfPTables and columns");
// step 1: creation of a document-object
Document document = new Document(PageSize.LETTER, 90, 54, 72, 72);
try
{
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1012.pdf", FileMode.Create));
float gutter = 20;
int numColumns = 3;
float fullWidth = document.Right - document.Left;
float columnWidth = (fullWidth - (numColumns - 1) * gutter) / numColumns;
float[] allColumns = new float[numColumns]; // left
for (int k = 0; k < numColumns; ++k)
{
allColumns[k] = document.Left + (columnWidth + gutter) * k;
}
// set the fonts
Font font24B = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 24, Font.BOLD);
Font font10B = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 10, Font.BOLD);
Font font14B = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 14, Font.BOLD, new Color(255, 0, 0));
Font font9 = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 9);
Font font11 = FontFactory.getFont(FontFactory.TIMES_NEW_ROMAN, 11);
// step 3: we open the document
document.Open();
// step 4:
// get the stream content
PdfContentByte cb = writer.DirectContent;
// headers
Phrase fullTitle = new Phrase("Full Title", font24B);
float currentY = document.Top;
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(fullTitle, document.Left, 0, document.Right, document.Top, 24, Element.ALIGN_JUSTIFIED);
ct.go();
currentY = ct.YLine;
currentY -= 4;
cb.LineWidth = 1;
cb.moveTo(document.Left, currentY);
cb.lineTo(document.Right, currentY);
cb.stroke();
currentY -= 4;
ct.YLine = currentY;
ct.addText(new Chunk("Author: Name of the author comes here", font10B));
ct.Leading = 10;
ct.go();
currentY = ct.YLine;
currentY -= 15;
float topColumn = currentY;
for (int k = 1; k < numColumns; ++k)
{
float x = allColumns[k] - gutter / 2;
cb.moveTo(x, topColumn);
cb.lineTo(x, document.Bottom);
}
cb.stroke();
Image img = Image.getInstance("cover.png");
cb.addImage(img, img.ScaledWidth, 0, 0, img.ScaledHeight, document.Left, currentY - img.ScaledHeight);
currentY -= img.ScaledHeight + 10;
ct.YLine = currentY;
ct.addText(new Chunk("Key Data:", font14B));
ct.go();
currentY = ct.YLine;
currentY -= 4;
PdfPTable ptable = new PdfPTable(2);
ptable.DefaultCell.PaddingLeft = 4;
ptable.DefaultCell.PaddingTop = 0;
ptable.DefaultCell.PaddingBottom = 4;
ptable.addCell(new Phrase("Imprint Name:", font9));
ptable.addCell(new Phrase("Prentice Hall", font9));
ptable.addCell(new Phrase("Series Name:", font9));
ptable.addCell(new Phrase("", font9));
ptable.addCell(new Phrase("ISBN:", font9));
ptable.addCell(new Phrase("Hall", font9));
ptable.addCell(new Phrase("UPC Code:", font9));
ptable.addCell(new Phrase("0789718103", font9));
ptable.addCell(new Phrase("EAN #", font9));
ptable.addCell(new Phrase("0786718103", font9));
ptable.addCell(new Phrase("Price:", font9));
ptable.addCell(new Phrase("49.99", font9));
ptable.addCell(new Phrase("Page Count:", font9));
ptable.addCell(new Phrase("500", font9));
ptable.addCell(new Phrase("Discount:", font9));
ptable.addCell(new Phrase("10%", font9));
ptable.addCell(new Phrase("Trim Size:", font9));
ptable.addCell(new Phrase("420x340", font9));
ptable.addCell(new Phrase("Cover:", font9));
ptable.addCell(new Phrase("Hard", font9));
ptable.addCell(new Phrase("Interior Color:", font9));
ptable.addCell(new Phrase("none", font9));
ptable.addCell(new Phrase("Media with book:", font9));
ptable.addCell(new Phrase("CD", font9));
ptable.addCell(new Phrase("Author(s):", font9));
ptable.addCell(new Phrase("Ben Forta", font9));
ptable.addCell(new Phrase("Editor:", font9));
ptable.addCell(new Phrase("Ben Forta", font9));
ptable.addCell(new Phrase("Pub Date:", font9));
ptable.addCell(new Phrase("06/05/1998", font9));
ptable.TotalWidth = columnWidth;
currentY = ptable.writeSelectedRows(0, -1, document.Left, currentY, cb) - 20;
for (int k = 0; k < headings.Length; ++k)
{
ct.addText(new Chunk(headings[k] + " ", font14B));
ct.addText(new Chunk(texts[k] + " ", font11));
}
int currentColumn = 0;
ct.setSimpleColumn(allColumns[currentColumn], document.Bottom,
allColumns[currentColumn] + columnWidth, currentY, 15, Element.ALIGN_JUSTIFIED);
ct.setLeading(2, 1);
for (;;)
{
int rc = ct.go();
if ((rc & ColumnText.NO_MORE_TEXT) != 0)
break;
// we run out of column. Let's go to another one
++currentColumn;
if (currentColumn >= allColumns.Length)
break;
ct.setSimpleColumn(allColumns[currentColumn], document.Bottom,
allColumns[currentColumn] + columnWidth, topColumn, 15, Element.ALIGN_JUSTIFIED);
ct.setLeading(2, 1);
}
// step 5: we close the document
document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
}
}
示例代码1013
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1013
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 10 Example 13: Spot Color");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1013.pdf", FileMode.Create));
BaseFont bf = BaseFont.createFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED);
// step 3: we open the document
document.Open();
// step 4: we add some content
PdfContentByte cb = writer.DirectContent;
// Note: I made up these names unless someone give me a PANTONE swatch as gift (phillip@formstar.com)
PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", 0.9f, new Color(114, 94, 38));
PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", 0.5f, new GrayColor(0.9f));
// Stroke a rectangle with CMYK alternate
cb.setColorStroke(spc_cmyk, .5f);
cb.LineWidth = 10f;
// draw a rectangle
cb.rectangle(100, 700, 100, 100);
// add the diagonal
cb.moveTo(100, 700);
cb.lineTo(200, 800);
// stroke the lines
cb.stroke();
// Fill a rectangle with CMYK alternate
cb.setColorFill(spc_cmyk, spc_cmyk.Tint);
cb.rectangle(250, 700, 100, 100);
cb.fill();
// Stroke a circle with RGB alternate
cb.setColorStroke(spc_rgb, spc_rgb.Tint);
cb.LineWidth = 5f;
cb.circle(150f, 500f, 100f);
cb.stroke();
// Fill the circle with RGB alternate
cb.setColorFill(spc_rgb, spc_rgb.Tint);
cb.circle(150f, 500f, 50f);
cb.fill();
// example with colorfill
cb.setColorFill(spc_g, spc_g.Tint);
cb.moveTo(100f, 200f);
cb.lineTo(200f, 250f);
cb.lineTo(400f, 150f);
cb.fill();
document.newPage();
String text = "Some text to show";
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk))));
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f))));
// example with template
PdfTemplate t = cb.createTemplate(500f, 500f);
// Stroke a rectangle with CMYK alternate
t.ColorStroke = new SpotColor(spc_cmyk, .5f);
t.LineWidth = 10f;
// draw a rectangle
t.rectangle(100, 10, 100, 100);
// add the diagonal
t.moveTo(100, 10);
t.lineTo(200, 100);
// stroke the lines
t.stroke();
// Fill a rectangle with CMYK alternate
t.setColorFill(spc_g, spc_g.Tint);
t.rectangle(100, 125, 100, 100);
t.fill();
t.beginText();
t.setFontAndSize(bf, 20f);
t.setTextMatrix(1f, 0f, 0f, 1f, 10f, 10f);
t.showText("Template text upside down");
t.endText();
t.rectangle(0, 0, 499, 499);
t.stroke();
cb.addTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f);
}
catch(Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
// step 5: we close the document
document.Close();
}
}
示例代码1013
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1013
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 10 Example 13: Spot Color");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1013.pdf", FileMode.Create));
BaseFont bf = BaseFont.createFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED);
// step 3: we open the document
document.Open();
// step 4: we add some content
PdfContentByte cb = writer.DirectContent;
// Note: I made up these names unless someone give me a PANTONE swatch as gift (phillip@formstar.com)
PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", 0.9f, new Color(114, 94, 38));
PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", 0.5f, new GrayColor(0.9f));
// Stroke a rectangle with CMYK alternate
cb.setColorStroke(spc_cmyk, .5f);
cb.LineWidth = 10f;
// draw a rectangle
cb.rectangle(100, 700, 100, 100);
// add the diagonal
cb.moveTo(100, 700);
cb.lineTo(200, 800);
// stroke the lines
cb.stroke();
// Fill a rectangle with CMYK alternate
cb.setColorFill(spc_cmyk, spc_cmyk.Tint);
cb.rectangle(250, 700, 100, 100);
cb.fill();
// Stroke a circle with RGB alternate
cb.setColorStroke(spc_rgb, spc_rgb.Tint);
cb.LineWidth = 5f;
cb.circle(150f, 500f, 100f);
cb.stroke();
// Fill the circle with RGB alternate
cb.setColorFill(spc_rgb, spc_rgb.Tint);
cb.circle(150f, 500f, 50f);
cb.fill();
// example with colorfill
cb.setColorFill(spc_g, spc_g.Tint);
cb.moveTo(100f, 200f);
cb.lineTo(200f, 250f);
cb.lineTo(400f, 150f);
cb.fill();
document.newPage();
String text = "Some text to show";
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk))));
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f))));
// example with template
PdfTemplate t = cb.createTemplate(500f, 500f);
// Stroke a rectangle with CMYK alternate
t.ColorStroke = new SpotColor(spc_cmyk, .5f);
t.LineWidth = 10f;
// draw a rectangle
t.rectangle(100, 10, 100, 100);
// add the diagonal
t.moveTo(100, 10);
t.lineTo(200, 100);
// stroke the lines
t.stroke();
// Fill a rectangle with CMYK alternate
t.setColorFill(spc_g, spc_g.Tint);
t.rectangle(100, 125, 100, 100);
t.fill();
t.beginText();
t.setFontAndSize(bf, 20f);
t.setTextMatrix(1f, 0f, 0f, 1f, 10f, 10f);
t.showText("Template text upside down");
t.endText();
t.rectangle(0, 0, 499, 499);
t.stroke();
cb.addTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f);
}
catch(Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
// step 5: we close the document
document.Close();
}
}
示例代码1014
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1014
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 10 Example 14: colored patterns");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
Document.compress = false;
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1014.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add some content
PdfContentByte cb = writer.DirectContent;
PdfTemplate tp = cb.createTemplate(400, 300);
PdfPatternPainter pat = cb.createPattern(15, 15, null);
pat.rectangle(5, 5, 5, 5);
pat.fill();
PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
SpotColor spot = new SpotColor(spc_cmyk);
tp.setPatternFill(pat, spot, .9f);
tp.rectangle(0, 0, 400, 300);
tp.fill();
cb.addTemplate(tp, 50, 50);
PdfPatternPainter pat2 = cb.createPattern(10, 10, null);
pat2.LineWidth = 2;
pat2.moveTo(-5, 0);
pat2.lineTo(10, 15);
pat2.stroke();
pat2.moveTo(0, -5);
pat2.lineTo(15, 10);
pat2.stroke();
cb.LineWidth = 1;
cb.ColorStroke = new Color(System.Drawing.Color.Black);
cb.setPatternFill(pat2, new Color(System.Drawing.Color.Red));
cb.rectangle(100, 400, 30, 210);
cb.fillStroke();
cb.setPatternFill(pat2, new Color(System.Drawing.Color.LightGreen));
cb.rectangle(150, 400, 30, 100);
cb.fillStroke();
cb.setPatternFill(pat2, new Color(System.Drawing.Color.Blue));
cb.rectangle(200, 400, 30, 130);
cb.fillStroke();
cb.setPatternFill(pat2, new GrayColor(0.5f));
cb.rectangle(250, 400, 30, 80);
cb.fillStroke();
cb.setPatternFill(pat2, new GrayColor(0.7f));
cb.rectangle(300, 400, 30, 170);
cb.fillStroke();
cb.setPatternFill(pat2, new GrayColor(0.9f));
cb.rectangle(350, 400, 30, 40);
cb.fillStroke();
}
catch (Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
// step 5: we close the document
document.Close();
}
}
示例代码1015
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1015
{
public static void Main(String[] args)
{
Console.WriteLine("Chapter 10 Example 15: Tiled Patterns");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1015.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.createFont("Times-Roman", "winansi", false);
// step 5: we create some PdfPatternPainter instances for drawing path, text, or placing image
// Image instance to be placed in PdfPatternPainter canvas. Any nice one?
Image img = Image.getInstance("pngnow.png");
PdfPatternPainter p = cb.createPattern(60f, 60f, 60f, 60f);
PdfPatternPainter p1 = cb.createPattern(60f, 60f, 60f, 60f);
PdfPatternPainter p2 = cb.createPattern(img.ScaledWidth, img.ScaledHeight, img.ScaledWidth, img.ScaledHeight);
// step 6: put your drawing instruction in the painter canvas
// A star pattern taken from Adobe PDF Reference Book p.207
String star = "0.3 g 15.000 27.000 m "
+ "7.947 5.292 l 26.413 18.708 l "
+ "3.587 18.708 l 22.053 5.292 l f "
+ "45.000 57.000 m 37.947 35.292 l "
+ "56.413 48.708 l 33.587 48.708 l "
+ "52.053 35.292 l f "
+ "0.7 g 15.000 57.000 m "
+ "7.947 35.292 l 26.413 48.708 l "
+ "3.587 48.708 l 22.053 35.292 l f "
+ "45.000 27.000 m 37.947 5.292 l "
+ "56.413 18.708 l 33.587 18.708 l "
+ "52.053 5.292 l f";
p.setLiteral(star);
// A Pattern with some text drawing
p1.GrayFill = 0.3f;
p1.setFontAndSize(bf, 12);
p1.beginText();
p1.setTextMatrix(1f, 0f, 0f, 1f, 0f, 0f);
p1.showText("A B C D");
p1.endText();
p1.moveTo(0f, 0f);
p1.lineTo(60f, 60f);
p1.stroke();
// A pattern with an image and position
p2.addImage(img, img.ScaledWidth, 0f, 0f, img.ScaledHeight, 0f, 0f);
p2.setPatternMatrix(1f, 0f, 0f, 1f, 60f, 60f);
// See if we can apply the pattern color to chunk, phrase or paragraph
PatternColor pat = new PatternColor(p);
PatternColor pat1 = new PatternColor(p1);
PatternColor pat2 = new PatternColor(p2);
String text = "Text with pattern";
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 60, Font.BOLD, new GrayColor(0.3f))));
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 60, Font.BOLD, pat)));
// draw a rectangle filled with star pattern
cb.setPatternFill(p);
cb.GrayStroke = 0.0f;
cb.rectangle(20, 20, 284, 120);
cb.fillStroke();
// draw some characters filled with star.
// Note: A gray, rgb, cmyk or spot color should be applied first
// otherwise, you will not be able to see the character glyph
// since the glyph path is filled by pattern
cb.beginText();
cb.setFontAndSize(bf, 1);
cb.setTextMatrix(270f, 0f, 0f, 270f, 20f, 100f);
cb.GrayFill = 0.9f;
cb.showText("ABC");
cb.setPatternFill(p);
cb.moveTextWithLeading(0.0f, 0.0f);
cb.showText("ABC");
cb.endText();
cb.setPatternFill(p);
// draw a circle. Similar to rectangle
cb.GrayStroke = 0.0f;
cb.circle(150f, 400f, 150f);
cb.fillStroke();
// New Page to draw text in the pattern painter's canvas
document.newPage();
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 60, Font.BOLD, new GrayColor(0.3f))));
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 60, Font.BOLD, pat1)));
// draw a rectangle
cb.setPatternFill(p1);
cb.GrayStroke = 0.0f;
cb.rectangle(0, 0, 284, 120);
cb.fillStroke();
// draw some characters
cb.beginText();
cb.setFontAndSize(bf, 1);
cb.setTextMatrix(270f, 0f, 0f, 270f, 20f, 100f);
cb.GrayFill = 0.9f;
cb.showText("ABC");
cb.setPatternFill(p1);
cb.moveTextWithLeading(0.0f, 0.0f);
cb.showText("ABC");
cb.endText();
// draw a circle
cb.setPatternFill(p1);
cb.GrayStroke = 0.0f;
cb.circle(150f, 400f, 150f);
cb.fillStroke();
// New page to place image in the pattern painter's canvas
document.newPage();
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 60, Font.BOLD, new GrayColor(0.3f))));
document.Add(new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA, 60, Font.BOLD, pat2)));
// The original Image for comparison reason.
// Note: The width and height is the same as bbox in pattern
cb.addImage(img, img.ScaledWidth, 0f, 0f, img.ScaledHeight, 350f, 400f);
// draw a rectangle
cb.setPatternFill(p2);
cb.GrayStroke = 0.0f;
cb.rectangle(60, 60, 300, 120);
cb.fillStroke();
// draw some characters.
// Note: if the image fills up the pattern, there's no need to draw text twice
// since colors in image will be clipped to character glyph path
cb.beginText();
cb.setFontAndSize(bf, 1);
cb.setTextMatrix(270f, 0f, 0f, 270f, 60f, 120f);
cb.setPatternFill(p2);
cb.showText("ABC");
cb.endText();
// draw a circle
cb.setPatternFill(p2);
cb.GrayStroke = 0.0f;
cb.circle(150f, 400f, 150f);
cb.fillStroke();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
// finally, we close the document
document.Close();
}
}
示例代码1101
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1101
{
public static void Main()
{
Console.WriteLine("Chapter 11 example 1: local goto");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1101.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add some content
Paragraph p1 = new Paragraph("We will do something special with this paragraph. If you click on ", FontFactory.getFont(FontFactory.HELVETICA, 12));
p1.Add(new Chunk("this word", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255))).setLocalGoto("test"));
p1.Add(" you will automatically jump to another location in this document.");
Paragraph p2 = new Paragraph("blah, blah, blah");
Paragraph p3 = new Paragraph("This paragraph contains a local ");
p3.Add(new Chunk("local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 255, 0))).setLocalDestination("test"));
document.Add(p1);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p3);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
}
}
示例代码1102
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class Chap1102
{
public static void Main()
{
Console.WriteLine("Chapter 11 example 2: anchor and remote goto");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writerA = PdfWriter.getInstance(document, new FileStream("Chap1102a.pdf", FileMode.Create));
PdfWriter writerB = PdfWriter.getInstance(document, new FileStream("Chap1102b.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add some content
Paragraph p1 = new Paragraph("We discussed anchors in chapter 3, but you can add an URL to a chunk to to make it an ", FontFactory.getFont(FontFactory.HELVETICA, 12));
p1.Add(new Chunk("anchor", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255))).setAnchor(new Uri("http://iTextSharp.sourceforge.net")));
p1.Add(" you will automatically jump to another location in this document.");
Paragraph p2 = new Paragraph("blah, blah, blah");
Paragraph p3a = new Paragraph("This paragraph contains a ");
p3a.Add(new Chunk("local destination in document A", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 255, 0))).setLocalDestination("test"));
Paragraph p3b = new Paragraph("This paragraph contains a local ");
p3b.Add(new Chunk("local destination in document B", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 255, 0))).setLocalDestination("test"));
Paragraph p4a = new Paragraph(new Chunk("Click this paragraph to go to a certain destination on document B").setRemoteGoto("Chap1102b.pdf", "test"));
Paragraph p4b = new Paragraph(new Chunk("Click this paragraph to go to a certain destination on document A").setRemoteGoto("Chap1102a.pdf", "test"));
Paragraph p5a = new Paragraph("you can also jump to a ");
p5a.Add(new Chunk("specific page on another document", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC)).setRemoteGoto("Chap1102b.pdf", 3));
document.Add(p1);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
writerA.Pause();
document.Add(p4b);
writerA.resume();
writerB.Pause();
document.Add(p4a);
document.Add(p5a);
writerB.resume();
document.Add(p2);
document.Add(p2);
document.Add(p2);
document.Add(p2);
writerA.Pause();
document.Add(p3b);
document.Add(p2);
document.Add(p2);
document.newPage();
document.Add(p2);
document.Add(p2);
document.newPage();
writerA.resume();
writerB.Pause();
document.Add(p3a);
writerB.resume();
document.Add(p2);
document.Add(p2);
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
}
// step 5: we close the document
document.Close();
}
}