Anda di halaman 1dari 15

HELLO WORLD

using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0101 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 1: Hello World"); // step 1: creation of a document-object Document document = new Document(); // 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")); // step 5: we close the document document.close(); } }

PAGE SIZE, BACKGROUND COLOR

using using using using using using

System; com.lowagie.text; com.lowagie.text.pdf; System.IO; System.Drawing; itext=com.lowagie.text;

public class Chap0102 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 2: PageSize"); // step 1: creation of a document-object

itext.Rectangle pageSize = new itext.Rectangle(144, 720); pageSize.setBackgroundColor(Color.LightYellow); Document document = new Document(pageSize); // 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")); // step 5: we close the document document.close(); } }

landscaped paper
using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0103 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 3: PageSize"); // step 1: creation of a document-object Document document = new Document(PageSize.A4.rotate()); // 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. ")); // step 5: we close the document document.close(); }

margins
using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0104 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 4: Margins"); // step 1: creation of a document-object Document document = new Document(PageSize.A5, 36, 72, 108, 180); // 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.setAlignment(Element.__Value.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); // step 5: we close the document document.close(); } }

meta information
using using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO; com.lowagie.text.html;

public class Chap0106 {

public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 6: Meta Information"); // step 1: creation of a document-object Document document = new Document(); // 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)); HtmlWriter.getInstance(document, Console.OpenStandardOutput()); // step 3: we add some metadata and open the document document.addTitle("Hello World example"); document.addSubject("This example explains step 3 in Chapter 1"); document.addKeywords("Metadata, iText, step 3, 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")); // step 5: we close the document document.close(); } }

watermark, header
using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0107 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 7: newPage()"); // step 1: creation of a document-object Document document = new Document(); // 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(Exception) { Console.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.setHeader(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(Exception) { Console.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.setFooter(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!")); // step 5: we close the document document.close(); } }

setting of view
using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0108 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 8: Viewerpreferences"); // step 1: creation of a document-object Document document = new Document(); // 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(Exception e) { Console.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.setHeader(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(Exception e) { Console.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.setFooter(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!")); // step 5: we close the document document.close(); } }

40 bits encryption
using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0109 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 9: encryption 40 bits");

Document document = new Document(PageSize.A4, 50, 50, 50, 50); 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(); } }

128 bits encryption


using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0110 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 10: encryption 128 bits"); Document document = new Document(PageSize.A4, 50, 50, 50, 50); 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(); } }

pause and resume of writer


using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0111 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 11: pause() and resume()"); // step 1: creation of a document-object Document document = new Document();

// 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(Exception e) { Console.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.setHeader(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(Exception e) { Console.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.setFooter(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(); // step 5: we close the document document.close(); } }

reading PDF
using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO;

public class Chap0112 { public static void Main(string[] args)

{ Console.WriteLine("Chapter 1 example 12: reading an existing PDF file"); // we create a reader for a certain document PdfReader reader = new PdfReader("Chap0703.pdf"); // we retrieve the total number of pages int n = reader.getNumberOfPages(); // we retrieve the size of the first page Rectangle psize = reader.getPageSize(1); float width = psize.width(); float height = psize.height(); // step 1: creation of a document-object Document document = new Document(psize, 50, 50, 50, 50); // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0112.pdf", FileMode.Create)); // step 3: we open the document try { Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 200, 320); document.add(watermark); } catch(Exception e) { Console.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?"); } document.open(); // step 4: we add content PdfContentByte cb = writer.getDirectContent(); int i = 0; int p = 0; Console.WriteLine("There are " + n + " pages in the document."); while (i < n) { document.newPage(); p++; i++; PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, .5f, 0, 0, .5f, 0, height / 2); Console.WriteLine("processed page " + i); if (i < n) { i++; PdfImportedPage page2 = writer.getImportedPage(reader, i); cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2); Console.WriteLine("processed page " + i); } if (i < n) {

i++; PdfImportedPage page3 = writer.getImportedPage(reader, i); cb.addTemplate(page3, .5f, 0, 0, .5f, 0, 0); Console.WriteLine("processed page " + i); } if (i < n) { i++; PdfImportedPage page4 = writer.getImportedPage(reader, i); cb.addTemplate(page4, .5f, 0, 0, .5f, width / 2, 0); Console.WriteLine("processed page " + i); } cb.setRGBColorStroke(255, 0, 0); cb.moveTo(0, height / 2); cb.lineTo(width, height / 2); cb.stroke(); cb.moveTo(width / 2, height); cb.lineTo(width / 2, 0); cb.stroke(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0? 1 : 0)), width / 2, 40, 0); cb.endText(); } // step 5: we close the document document.close(); } }

Chunk
using using using using using System; com.lowagie.text; com.lowagie.text.pdf; System.IO; GDI=System.Drawing;

public class Chap0201 { public static void Main(String[] args) { Console.WriteLine("Chapter 2 example 1: Chunks and fonts"); // step 1: creation of a document-object Document document = new Document(); // 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]; int i = 0; fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD); fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD | Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.SYMBOL, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL); for (i = 0; i < fonts.Length; 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(GDI.Color.Yellow); document.add(new Phrase(ck)); // step 5: we close the document

document.close(); } }

Anda mungkin juga menyukai