如何使用iTextSharp为PdfPCell添加不同的块?

时间:2015-04-13 17:57:34

标签: pdf-generation itextsharp chunks

如何连接不同的块并将它们添加到段落中,将段落添加到单元格,然后将单元格添加到使用iTextSharp的表格(生成PDF文件)?

我能够到达某个地方"在我的PDF文件生成中,所以它看起来像这样(页面的右侧是空白的,应该是这样):

enter image description here

这是我用过的代码:

using (var ms = new MemoryStream())
{
    using (var doc = new Document(PageSize.A4, 50, 50, 25, 25))
    {
        //Create a writer that's bound to our PDF abstraction and our stream
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {

            //Open the document for writing
            doc.Open();

            var courierBold11Font = FontFactory.GetFont(FontFactory.COURIER_BOLD, 11, BaseColor.BLACK);
            var docTitle = new Paragraph("Mark Twain", courierBold11Font);
            doc.Add(docTitle);

            var timesRoman9Font = FontFactory.GetFont("Times Roman", 9, BaseColor.BLACK);
            var subTitle = new Paragraph("Roughing It", timesRoman9Font);
            doc.Add(subTitle);

            var courier9RedFont = FontFactory.GetFont("Courier", 9, BaseColor.RED);
            var importantNotice = new Paragraph("'All down but nine; set 'em up on the other alley, pard' - Scotty Briggs", courier9RedFont);
            importantNotice.Leading = 0;
            importantNotice.MultipliedLeading = 0.9F; // reduce the width between lines in the paragraph with these two settings

            PdfPTable table = new PdfPTable(1);
            PdfPCell cellImportantNote = new PdfPCell(importantNotice);
            cellImportantNote.BorderWidth = PdfPCell.NO_BORDER;
            table.WidthPercentage = 50;
            table.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(cellImportantNote);
            doc.Add(table);

            doc.Close();
        }
        var bytes = ms.ToArray();
        String PDFTestOutputFileName = String.Format("iTextSharp_{0}.pdf", DateTime.Now.ToShortTimeString());
        PDFTestOutputFileName = PDFTestOutputFileName.Replace(":", "_");
        var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), PDFTestOutputFileName);
        File.WriteAllBytes(testFile, bytes);
        MessageBox.Show(String.Format("{0} written", PDFTestOutputFileName));
    }
}

但是,我需要分解红色文本,以便它的一部分是粗体,部分是锚标签/ hrefs等。

我以为我可以这样做:

var courier9RedBoldFont = FontFactory.GetFont(FontFactory.COURIER_BOLD, 9, BaseColor.RED);
// Build up chunkified version of "important notice"
Chunk boldpart = new Chunk("All down but nine - set 'em up on the other alley, pard", courier9RedBoldFont);
Chunk attribution = new Chunk("Scotty Briggs", courier9RedFont);

PdfPTable tbl = new PdfPTable(1);
tbl.WidthPercentage = 50;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
par.Chunks.Add(boldpart);
par.Chunks.Add(attribution );
PdfPCell chunky = new PdfPCell(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);                          
doc.Add(tbl);

...但是这并没有向PDF文件添加任何内容,但为什么不呢?单元格不是一个段落,段落不能由Chunks组成吗?

1 个答案:

答案 0 :(得分:2)

而不是para.Chunks.Add()只使用par.Add();Chunks返回的Paragraph实际上来自基类Phrase。如果该属性为you look at the code,您将看到返回的集合实际上是一个临时创建的集合,因此它实际上是只读的。