【问题标题】:Issue in adding paragraphs to pdf usimg itextsharp使用 itextsharp 将段落添加到 pdf 的问题
【发布时间】:2011-07-01 02:23:03
【问题描述】:

我有以下代码,其在 pdf 文件中的输出是:

表格 M.T.R. 17

宪报官员工资单

                                DDO Code: 703

代码是:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;
public partial class new_salary : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";

        // Create PDF document
        Document pdfDocument = new Document(PageSize.A4, 70, 45, 40, 25);

        PdfWriter wri = PdfWriter.GetInstance(pdfDocument, new FileStream("d://JudgeSalary.pdf", FileMode.Create));

        PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);

        pdfDocument.Open();

        Chunk boo = new Chunk("Form M.T.R. 17");


        Paragraph main1 = new Paragraph("Form M.T.R. 17 \nPAY-BILL OF GAZETTED OFFICER");
        main1.Alignment = Element.ALIGN_CENTER;
        main1.Font.SetStyle(Font.BOLD);

        Paragraph main1a = new Paragraph("          DDO Code: 703");
        main1a.Alignment = Element.ALIGN_RIGHT;





        pdfDocument.Add(main1);
        pdfDocument.Add(main1a);

        pdfDocument.Close();
        HttpContext.Current.Response.End();
    }
}

我希望输出是:

               **Form M.T.R. 17**

               **PAY-BILL OF GAZETTED OFFICER**                           DDO Code: 703

如何在段落的第 2 行使用“DDO 代码:703”和没有文本格式来获得上述输出。如果我在“para1”中包含“DDO 代码:703”,则文本显示为粗体。 我希望此输出居中对齐接受我希望右对齐的“DDO 代码:703”。

我不希望它显示为粗体,也希望它出现在段落的第二行。我该怎么做?

【问题讨论】:

    标签: c# asp.net itextsharp paragraphs


    【解决方案1】:

    使用 iTextSharp,您可以添加带有 ChunkPhraseParagraph 的文本。请参阅有关这些工作原理的参考here。从本质上讲,您将希望将您的段落作为几个短语或块放在一起,以允许不同的字体样式。

    要实现您想要的定位,带有适当对齐的PdfPCell 元素的PdfPTable 可能效果最好。类似于以下内容:

    PdfPTable table = new PdfPTable(2);
    PdfPCell cell = new PdfPCell(new Phrase("Form M.T.R. 17", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, iTextSharp.text.Font.BOLD)));
    cell.Colspan = 2;
    cell.Border = 0;
    cell.HorizontalAlignment = Element.ALIGN_LEFT; 
    table.AddCell(cell);
    table.AddCell(new Phrase("PAY-BILL OF GAZETTED OFFICER"));
    
    PdfPCell rCell = new PdfPCell(new Phrase("DDO Code: 703", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12)));
    rCell.Border = 0;
    rCell.HorizontalAlignment = Element.ALIGN_RIGHT; 
    table.AddCell(rCell);
    pdfDocument.Add(table);
    

    【讨论】:

    • 我希望输出居中对齐。我可以对齐块、短语吗?
    • 如果您查看有关Paragraph 的示例,它们会显示如何将Chunk 添加到Phrase,而Phrase 又可以添加到Paragraph。然后,您可以在段落上调用setAlignment() 函数使其居中。
    • 是的,但我希望“DDO 代码:703”右对齐并且不对其应用任何格式。
    • 您有几个选择。一种方法是使用Chunk 作为“DDO 代码:703”并根据需要定位它。另一种方法是使用PdfPTable 和一对具有不同水平对齐的PdfPCells。在此处查看使用这些类的示例:mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables
    【解决方案2】:

    段落将在一个新行中,尝试使用这样的短语:

    Phrase main1 = new Phrase("Form M.T.R. 17 \nPAY-BILL OF GAZETTED OFFICER");
    main1.Font.SetStyle(Font.BOLD);
    
    Phrase main1a = new Paragraph(" DDO Code: 703");
    
    pdfDocument.Add(main1);
    pdfDocument.Add(main1a);
    

    【讨论】:

    猜你喜欢
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多