Issue in adding paragraphs to pdf usimg itextsharp
I have the following code whose output in a pdf file is:
Form M.T.R. 17
PAY-BILL OF GAZETTED OFFICER
DDO Code: 703
The code is:
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();
}
}
I want the output to be:
**Form M.开发者_JAVA百科T.R. 17**
**PAY-BILL OF GAZETTED OFFICER** DDO Code: 703
How can i get the above mentioned output with 'DDO Code: 703' in the 2nd line of the paragraph and no text formatting . If i include 'DDO Code: 703' in 'para1' than the text is displayed as bold. I want this output to be center alignment accept 'DDO Code: 703' which i want to be right aligned.
I dont want it to appear bold and also want it in the 2nd line of the paragraph. how can i do it?
Using iTextSharp, you can add text with a Chunk
, Phrase
, or Paragraph
. See a reference of how these work here. Essentially, you will want to put your paragraph together as a few phrases or chunks to allow for the differing font styles.
To achieve the positioning you want, a PdfPTable
with appropriately aligned PdfPCell
elements may work best. Something like the following:
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);
A paragraph will be in a new line, try to use Phrase like this:
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);
精彩评论