C# 将段落添加到pdf usimg itextsharp中的问题
我有以下代码,其在pdf文件中的输出为: 表格M.T.R.17 宪报刊登官员的工资单C# 将段落添加到pdf usimg itextsharp中的问题,c#,asp.net,itextsharp,paragraphs,C#,Asp.net,Itextsharp,Paragraphs,我有以下代码,其在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; usin
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”
我不希望它显得粗体,也希望它在第二行的段落。我怎么做 使用iTextSharp,您可以添加带有
区块
、短语
或段落
的文本。请参阅有关这些如何工作的参考资料。基本上,你会想把你的段落放在一起作为几个短语或块,以允许不同的字体风格
为了实现您想要的定位,具有适当对齐的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);
段落将换一行,尝试使用以下短语:
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);
我希望输出与中心对齐。我可以对齐块和短语吗?如果你看一看关于
段落
的示例,它们显示了如何将块
添加到短语
,然后再将添加到段落
。然后,您可以调用段落上的setAlignment()
函数将其居中。是的,但我希望“DDO代码:703”右对齐,并且不对其应用格式。对此,您有两个选项。一种方法是使用区块
作为“DDO代码:703”,并根据需要对其进行定位。另一种方法是使用一个PdfPTable
和一对具有不同水平对齐的PdfPCell
s。请参见此处使用这些类的示例:任何人都可以帮助我吗