将文本添加到PDF

问题描述 投票:0回答:2

我正在使用iTextSharp生成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);
}
itext
2个回答
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,你会发现其他元素,如AnchorList(和ListItem),等等。

但是,您似乎很擅长使用iText。在这种情况下,为什么不从最新的iText版本开始?我们刚刚从头开始重写iText,我们正在推出一个全新的API。

您可以在iText 7: Building Blocks教程中找到更详细的新类概述。


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!")); 
      }          
    }
// ===========================================================================
  }
}

(Qazxswpoi)


因此,简而言之,

现在我想在PDF中添加一些普通文本。我怎么做?

您只需创建一个HelloWorld.cs并将其添加到您的Paragraph实例中。


有人说,因为你是iText的新手,为什么不开始使用当前的iText 7 for .Net?

© www.soinside.com 2019 - 2024. All rights reserved.