iText生產PDF

來源:互聯網
上載者:User

 //Document:(文檔)產生pdf必備的一個對象,產生一個Document樣本
                Document document = new Document(PageSize.A4, 30, 30, 5, 5);
                //為該Document建立一個Writer執行個體:
                PdfWriter.GetInstance(document, new FileStream(@"E:\Chap0103.pdf", FileMode.Create));
                //開啟當前Document
                document.Open();
                string fontPath = Environment.GetEnvironmentVariable("WINDIR") + "\\FONTS\\SIMHEI.TTF";//強制中文字型,否則無法顯示中文
                BaseFont baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                //包含頁碼沒有任何邊框的頁尾。
                HeaderFooter footer = new HeaderFooter(new Paragraph("This is page: "), true);
                footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
                document.Footer = footer;

                //Chapter對象和Section對象自動構建一個樹:
                iTextSharp.text.Font fh = new iTextSharp.text.Font(baseFont, 12);
                iTextSharp.text.Font f1 = new iTextSharp.text.Font(baseFont,10);
                f1.SetStyle(iTextSharp.text.Font.BOLD);
                HeaderFooter hf = new HeaderFooter(new Paragraph("\n\n3月份第二批提貨明細\n\n" + "SKZS-03-2012\n\n" + "作業NO" + "     " + "作業小番\n\n", fh),true);
              
                document.Header = hf;

                Paragraph cTitle = new Paragraph("3月份第二批提貨明細\n\n" + "SKZS-03-2012\n\n" + "作業NO" + "     " + "作業小番\n\n", fh);
                Chapter chapter = new Chapter(cTitle, 1);

                document.Add(chapter);

            table.AddCell(cell);

//f1是字型設定
              cell = new Cell(new Paragraph("鋼種", f1));
              table.AddCell(cell);
              cell = new Cell(new Paragraph("爐號", f1));
              table.AddCell(cell);
              cell = new Cell(new Paragraph("厚度", f1));
              table.AddCell(cell);
              cell = new Cell(new Paragraph("定尺", f1));
              table.AddCell(cell);
              cell = new Cell(new Paragraph("捆號", f1));
              table.AddCell(cell);
              cell = new Cell(new Paragraph("捆數", f1));
              table.AddCell(cell);
              cell = new Cell(new Paragraph("支數", f1));
              table.AddCell(cell);
              cell = new Cell(new Paragraph("出庫量", f1));
              table.AddCell(cell);

//下面這句話未來以後每頁的table都有頭
              table.EndHeaders();

   //構建了一個不簡單的表:
                               Table table = new Table(3);
                table.BorderWidth = 1;
                table.BorderColor = new iTextSharp.text.Color(0, 0, 255);
                table.Cellpadding = 5;
                table.Cellspacing = 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 iTextSharp.text.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 iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
                table.AddCell(cell);
                table.AddCell("cell test2");
                // 改變了儲存格“big cell”的對齊:
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                document.Add(table);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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