如何更改pdf文档的字体大小 [英] How to change font size of pdf document
本文介绍了如何更改pdf文档的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用C#将div标签数据导出到asp.net中的pdf格式,但默认字体大小永远不会改变..
Plz为我提供了解决方案 asap 。
我的aspx代码是..
I exporting div tag data into pdf format in asp.net using C# but the default font size never changes..
Plz give me solution for this asap.
my aspx code is..
protected void btnPdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Tbldata.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
推荐答案
尝试放置这个
try placing this
BaseFont bfChronicleFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\ChronicleDisp-Black.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font ChronicleFont = new Font(bfChronicleFont, 16f);
BaseFont bfBrandonFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\Brandon_reg.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font BrandonFont = new Font(bfBrandonFont, 14f, Font.NORMAL);
private void HTMLtoPDF()
{
try
{
string sFilePath = "C:/Users/NandaKishore.K/Desktop/";
FileStream fs = File.Create(sFilePath + "SamplePDF.pdf");
Document document = new Document();
BaseFont bfR = iTextSharp.text.pdf.BaseFont.CreateFont(BaseFont.TIMES_ROMAN, iTextSharp.text.pdf.BaseFont.CP1257, iTextSharp.text.pdf.BaseFont.EMBEDDED);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
document.Add(jpg);
//iTextSharp.text.Font HeaderOne = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 16f, iTextSharp.text.Font.BOLD);
//Font Chronicle = FontFactory.GetFont("Chronicle Display Black");
//Chronicle.Size = 16;
//Chronicle.IsBold();
BaseFont bfChronicleFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\ChronicleDisp-Black.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font ChronicleFont = new Font(bfChronicleFont, 16f);
BaseFont bfBrandonFont = BaseFont.CreateFont("C:\\inetpub\\wwwroot\\Practice\\Practice\\Fonts\\Brandon_reg.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font BrandonFont = new Font(bfBrandonFont, 14f, Font.NORMAL);
PdfPTable table1 = new PdfPTable(1);
PdfPCell pdfCell;
pdfCell = new PdfPCell(new Phrase("BELLEVUE SQUARE", ChronicleFont));
pdfCell.Border = 0;
pdfCell.PaddingTop = 40;
pdfCell.PaddingLeft = 100;
table1.AddCell(pdfCell);
pdfCell = new PdfPCell(new Phrase("Use our west entrance on two.", BrandonFont));
pdfCell.Border = 0;
pdfCell.PaddingTop = 10;
pdfCell.PaddingLeft = 100;
table1.AddCell(pdfCell);
pdfCell = new PdfPCell(new Phrase("Thursday, November 29, 7:30 p.m.", BrandonFont));
pdfCell.Border = 0;
pdfCell.PaddingLeft = 100;
table1.AddCell(pdfCell);
pdfCell = new PdfPCell(new Phrase(string.Empty));
pdfCell.Border = 0;
pdfCell.FixedHeight = 60;
table1.AddCell(pdfCell);
document.Add(table1);
PdfPTable table2 = new PdfPTable(5);
PdfPCell pdfCell2;
pdfCell2 = new PdfPCell(new Phrase("Name:", BrandonFont));
pdfCell2.PaddingLeft = 100;
pdfCell2.Border = 0;
pdfCell2.Colspan = 3;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase("Name", BrandonFont));
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase(string.Empty));
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase("Total Number of Guests:", BrandonFont));
pdfCell2.PaddingLeft = 110;
pdfCell2.PaddingTop = 15;
pdfCell2.Colspan = 3;
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase("Name", BrandonFont));
pdfCell2.PaddingTop = 15;
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase(string.Empty));
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase("Confirmation Number:", BrandonFont));
pdfCell2.PaddingTop = 15;
pdfCell2.Colspan = 3;
pdfCell2.PaddingLeft = 100;
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase("Name", BrandonFont));
pdfCell2.PaddingTop = 15;
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase(string.Empty));
pdfCell2.Border = 0;
table2.AddCell(pdfCell2);
pdfCell2 = new PdfPCell(new Phrase(string.Empty));
pdfCell2.Colspan = 5;
pdfCell2.Border = 0;
pdfCell2.FixedHeight = 80;
table2.AddCell(pdfCell2);
document.Add(table2);
document.Close();
fs.Close();
Response.AddHeader("Content-Disposition", "attachment;filename=SamplePDF.pdf");
Response.ContentType = "application/pdf";
Response.TransmitFile("C:\\Users\\NandaKishore.K\\Desktop\\SamplePDF.pdf");
Response.End();
}
catch (Exception ex)
{
}
}
if (!Directory.Exists("C:\\Downloads\\GSOvertimeReport"))
{
Directory.CreateDirectory("C:\\Downloads\\GSOvertimeReport");
}
filename = filename.Replace('/', '-');
DataTable dataTable = SelectDT(str);
var pdfDocument = new Document();
var pdfFile = string.Format("{0}{1}.pdf", "C:\\Downloads\\GSOvertimeReport\\", filename);
if (File.Exists(pdfFile))
{
File.Delete(pdfFile);
}
var pdfWriter =PdfWriter.GetInstance(pdfDocument,new FileStream(pdfFile,FileMode.Create));
pdfDocument.Open();
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font Mtimes = new Font(bfTimes, 14, Font.BOLD, BaseColor.BLACK);
DataTable dtComp = new DataTable();
dtComp = SelectDT("select * from GSIT_CompanyMaster");
Paragraph head = new Paragraph(dtComp.Rows[0][1].ToString()+" - Time Sheet ",Mtimes);
head.Alignment = Element.ALIGN_CENTER;
pdfDocument.Add(head);
pdfDocument.Add(new Paragraph("-------------------------------------------------------------------------------------------------------------------------------- "));
pdfDocument.Add(new Paragraph());
var pdfTable = new PdfPTable(dataTable.Columns.Count);
pdfTable.WidthPercentage = 100;
Font Htimes = new Font(bfTimes, 10, Font.BOLD, BaseColor.WHITE);
for (int k = 0; k < dataTable.Columns.Count; k++)
{
var pdfCell = new PdfPCell(new Phrase(dataTable.Columns[k].ColumnName,Htimes));
pdfCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
pdfCell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
pdfCell.BackgroundColor = new BaseColor(51,102,102);
pdfTable.AddCell(pdfCell);
}
Font times = new Font(bfTimes, 7, Font.NORMAL, BaseColor.BLACK);
for (int row = 0; row < dataTable.Rows.Count; row++)
{
for (int col = 0; col < dataTable.Columns.Count; col++)
{
var cell = new PdfPCell(new Phrase (dataTable.Rows[row][col].ToString(),times));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
pdfTable.AddCell(cell);
}
}
pdfDocument.Add(pdfTable);
pdfDocument.Close();
}
这篇关于如何更改pdf文档的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文