2016-09-06 141 views
0

我正在使用iTextSharp生成PDF文檔。我能夠創建並添加一個表格。現在我想給PDF添加一些正常文本。我怎麼做?我在網上看了一下,他們建議使用大塊,但它不工作:將文本添加到PDF

Chunk c1 = new Chunk("A chunk represents an isolated string. "); 

這是我是如何將表:

private byte[] CreatePDFFile(StudentJournalAdd studentjournal, SearchResult<JournalAttendanceStatus> StatusList, string studentName) 
{ 
    var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12); 
    var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12); 
    string Details = string.Empty; 
    int icount = 0; 
    bool bflag = false; 

    PdfPTable table = new PdfPTable(3); 
    table.TotalWidth = 550f; 
    table.LockedWidth = true; 
    float[] widths = new float[] { 10f, 20f, 10f }; 
    table.SetWidths(widths); 
    table.HorizontalAlignment = 1; 

    table.SpacingBefore = 20f; 
    table.SpacingAfter = 30f; 
    table.DefaultCell.PaddingTop = 2f; 
    table.DefaultCell.PaddingBottom = 2f; 

    //PdfPCell cellHeading = new PdfPCell(new Phrase("AdminNo - " + studentjournal.StudentJournaldtls.AdminNo + " (WeekNo) - " + studentjournal.StudentJournaldtls.WeekNo + "\n\n", boldFont)); 
    PdfPCell cellHeading = new PdfPCell(new Phrase("Temasek Polytechnic - Student Internship Programme Journal\n\n " + studentName + " (" + studentjournal.StudentJournaldtls.AdminNo + ")\n\n Week No: " + studentjournal.StudentJournaldtls.WeekNo + "\n\n" + "DAILY/WEEKLY SUMMARY OF TASKS" + "\n\n", boldFont)); 

    cellHeading.Colspan = 3; 
    cellHeading.Rowspan = 2; 
    cellHeading.Border = 0; 
    cellHeading.HorizontalAlignment = 1; 
    cellHeading.VerticalAlignment = Element.ALIGN_MIDDLE; 
    table.AddCell(cellHeading); 

    PdfPCell cell0 = new PdfPCell(new Phrase("Day", boldFont)); 
    cell0.PaddingBottom = 10f; 
    cell0.VerticalAlignment = Element.ALIGN_CENTER; 
    cell0.HorizontalAlignment = Element.ALIGN_CENTER; 
    table.AddCell(cell0); 

    PdfPCell cell1 = new PdfPCell(new Phrase("Task Assigned", boldFont)); 
    cell1.PaddingBottom = 10f; 
    cell1.VerticalAlignment = Element.ALIGN_CENTER; 
    cell1.HorizontalAlignment = Element.ALIGN_CENTER; 
    table.AddCell(cell1); 

    PdfPCell cell2 = new PdfPCell(new Phrase("Attendance", boldFont)); 
    cell2.PaddingBottom = 10f; 
    cell2.VerticalAlignment = Element.ALIGN_CENTER; 
    cell2.HorizontalAlignment = Element.ALIGN_CENTER; 
    table.AddCell(cell2); 
} 

回答

1

你可以找到所有從的iText在行動的例子 - 第二版here on the itextpdf site

首先您會看到示例的Java/iText版本,但在相應頁面的末尾您還可以找到指向C#/ iTextSharp版本的鏈接。

例如,chapter 1 examples的頁面上,你會發現一個鏈接,這個簡單的Hello World程序,增加了一些普通文本的PDF

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Linq; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace kuujinbo.iTextInAction2Ed.Chapter01 { 
    public class HelloWorld : IWriter { 
// =========================================================================== 
    public void Write(Stream stream) { 
     // step 1 
     using (Document document = new Document()) { 
     // step 2 
     PdfWriter.GetInstance(document, stream); 
     // step 3 
     document.Open(); 
     // step 4 
     document.Add(new Paragraph("Hello World!")); 
     }   
    } 
// =========================================================================== 
    } 
} 

HelloWorld.cs


總之,因此,

現在我想添加一些普通的文本到PDF。我怎麼做?

您只需創建一個Paragraph並將其添加到Document實例。


有人說,因爲您是iText的新手,爲什麼不開始使用當前的iText 7 for .Net?

+0

我們幾乎同時回答;-) –

+0

;)是的。清晨的答案... – mkl

2

PdfPTableChunk只是可以添加到iTextSharp中Document的兩個元素。請看看the official web site。例如,如果您瀏覽「iText in Action - Second Edition」一書的示例,您會發現a couple of hundreds of examples。在每個示例頁面的底部,您可以找到指向每個示例的C#版本的鏈接。例如:

// step 1 
using (Document document = new Document()) { 
    // step 2 
    PdfWriter.GetInstance(document, stream); 
    // step 3 
    document.Open(); 
    // step 4 
    document.Add(new Paragraph("Hello World!")); 
} 

在這個簡單的Hello World示例中,我們添加了一個Paragraph。這已經回答了您的問題:「我如何向PDF添加一些正常文本?」

chapter 2中,您會發現其他元素,例如Anchor,List(和ListItem)等。

但是,看起來你是使用iText的新手。那麼,爲什麼不從最新的iText版本開始?我們從頭開始重寫了iText,並且我們正在引入一個全新的API。

您可以在iText 7: Building Blocks教程中找到更詳細的新類概述。