2016-09-06 92 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教程中找到更详细的新类概述。