1

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

表格 MTR 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”。

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

4

2 回答 2

2

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

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

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);
于 2011-02-21T07:27:04.873 回答
0

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

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);
于 2011-02-21T21:04:13.667 回答