使用此代码生成 PDF 文件时如何增加字体大小?
How can I increase the font size while generating a PDF file using this code?
关于使用 iTextSharp 在一个全尺寸页面上打印四个 "quarter page" 部分的问题,我得到了有效的答案 。
问题是打印有点微不足道;这是页面上半部分的示例 - 您可以看到打印的内容有多小以及可用的额外内容 space:
这是使用的代码,基于迄今为止 linked SO post:
中提供的答案
public static void PrintSlips(List<AssignmentStudentMashup> asmList)
{
PdfReader reader = new PdfReader(GetMasterDocument(asmList));
Rectangle pageSize = reader.GetPageSize(1);
string printedDate = DateTime.Now.ToShortDateString();
printedDate = printedDate.Replace("/", "_");
string printedSlipsFile = $"C:\AYttFMApp\AYttFMSlips_{printedDate}.pdf";
// Delete the file if it exists.
if (File.Exists(printedSlipsFile))
{
File.Delete(printedSlipsFile);
}
using (FileStream stream = new FileStream(
printedSlipsFile,
FileMode.Create,
FileAccess.Write))
{
using (Document document = new Document(pageSize, 0, 0, 0, 0)) // pageSize could be replaced with "A4"
{
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
PdfPTable table = new PdfPTable(2)
{
TotalWidth = pageSize.Width,
LockedWidth = true
};
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.DefaultCell.FixedHeight = pageSize.Height / 2;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
table.AddCell(Image.GetInstance(page));
}
document.Add(table);
}
}
}
internal static byte[] GetMasterDocument(List<AssignmentStudentMashup> asmList)
{
int count = asmList.Count;
using (var stream = new MemoryStream())
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, stream);
document.Open();
for (int i = 1; i < count; ++i)
{
document.Add(new Paragraph(
$@"WeekOfAssignment: {asmList[i].WeekOfAssignment}
TalkTypeName: {asmList
[i].TalkTypeName}
StudentFullname: {asmList[i]
.StudentFullname}
AssistantFullname: {asmList[i]
.AssistantFullname}
CounselPointNum: {asmList[i]
.CounselPointNum}
CounselPointStr: {asmList[i
].CounselPointStr}"));
if (i < count) document.NewPage();
}
}
return stream.ToArray();
}
}
我假设增加字体的地方在 GetMasterDocument() 中的某处,但我不知道 where/what 可以做什么。另外,我想将每行的第一部分(“:”之前的部分)加粗。
更新
我从 Agus Rdz 提供的 link 中尝试了他的建议:
BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
Font courier = new Font(bfCourier, 12, Font.BOLD, Color.Black);
. . .
CounselPointStr: {asmList[i
//].CounselPointStr}"));
].CounselPointStr}", courier));
...但这不会编译;我得到:
错误 CS1503 参数 1:无法从 'iTextSharp.text.pdf.BaseFont' 转换为 'iTextSharp.text.Font.FontFamily'
...和:
错误 CS1503 参数 4:无法从 'System.Drawing.Color' 转换为 'iTextSharp.text.BaseColor' AYttFMScheduler
然后我根据添加到该博客的评论尝试了这个 post:
Font fMSGothic = FontFactory.GetFont("MS-PGothic", BaseFont.IDENTITY_H, 16);
. . .
CounselPointStr: {asmList[i
//].CounselPointStr}"));
].CounselPointStr}", courier));
...编译,但我得到一个 I/O 异常,更具体地说“文档没有页面”
这是错误的:
BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
Font courier = new Font(bfCourier, 12, Font.BOLD, Color.Black);
在第一行中,您创建了一个 BaseFont
对象。您可以使用该对象创建一个 Font
,如下所示:
Font courier = new Font(bfCourier, 12);
如果你想让字体加粗,你可以这样创建一个BaseFont
:
BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, false);
您遇到的错误是由于 Font
也可以用不同的方式创建:
Font courier = new Font(Font.FontFamily.COURIER, 12, Font.BOLD, BaseColor.BLACK);
你混淆了BaseFont
和FontFamily
的概念。字体系列(例如 Courier)是由不同字体(Courier regular、Courier bold、Courier italic、Courier bold-italic)组成的字体系列。基本字体是单一字体(例如 Courier 粗体)。
请注意,您还混淆了 类 Color
和 BaseColor
。实际上,您收到的错误消息是不言自明的。
"Document has no pages" 错误也解释了问题所在:当您 Close()
文档时,没有添加任何内容。也许您认为您已经添加了内容,但实际上您没有。如果您认为自己做到了,最可能的原因是抛出了另一个异常。例如:您使用了一种不存在的字体。 IOException
出现,您忽略它。因此,没有添加任何文本,您会收到 "Document has no pages" 错误。或者,您尝试添加 Image
但该图像的路径是错误的。因此,没有添加任何内容,您会收到 "Document has no pages" 错误。
在您的情况下,您可能会收到该错误,因为 asmList
为空。在这种情况下,您正在创建一个没有任何内容的文档,因为 asmList
.
中没有学生
关于使用 iTextSharp 在一个全尺寸页面上打印四个 "quarter page" 部分的问题,我得到了有效的答案
问题是打印有点微不足道;这是页面上半部分的示例 - 您可以看到打印的内容有多小以及可用的额外内容 space:
这是使用的代码,基于迄今为止 linked SO post:
中提供的答案public static void PrintSlips(List<AssignmentStudentMashup> asmList)
{
PdfReader reader = new PdfReader(GetMasterDocument(asmList));
Rectangle pageSize = reader.GetPageSize(1);
string printedDate = DateTime.Now.ToShortDateString();
printedDate = printedDate.Replace("/", "_");
string printedSlipsFile = $"C:\AYttFMApp\AYttFMSlips_{printedDate}.pdf";
// Delete the file if it exists.
if (File.Exists(printedSlipsFile))
{
File.Delete(printedSlipsFile);
}
using (FileStream stream = new FileStream(
printedSlipsFile,
FileMode.Create,
FileAccess.Write))
{
using (Document document = new Document(pageSize, 0, 0, 0, 0)) // pageSize could be replaced with "A4"
{
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
PdfPTable table = new PdfPTable(2)
{
TotalWidth = pageSize.Width,
LockedWidth = true
};
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.DefaultCell.FixedHeight = pageSize.Height / 2;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
table.AddCell(Image.GetInstance(page));
}
document.Add(table);
}
}
}
internal static byte[] GetMasterDocument(List<AssignmentStudentMashup> asmList)
{
int count = asmList.Count;
using (var stream = new MemoryStream())
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, stream);
document.Open();
for (int i = 1; i < count; ++i)
{
document.Add(new Paragraph(
$@"WeekOfAssignment: {asmList[i].WeekOfAssignment}
TalkTypeName: {asmList
[i].TalkTypeName}
StudentFullname: {asmList[i]
.StudentFullname}
AssistantFullname: {asmList[i]
.AssistantFullname}
CounselPointNum: {asmList[i]
.CounselPointNum}
CounselPointStr: {asmList[i
].CounselPointStr}"));
if (i < count) document.NewPage();
}
}
return stream.ToArray();
}
}
我假设增加字体的地方在 GetMasterDocument() 中的某处,但我不知道 where/what 可以做什么。另外,我想将每行的第一部分(“:”之前的部分)加粗。
更新
我从 Agus Rdz 提供的 link 中尝试了他的建议:
BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
Font courier = new Font(bfCourier, 12, Font.BOLD, Color.Black);
. . .
CounselPointStr: {asmList[i
//].CounselPointStr}"));
].CounselPointStr}", courier));
...但这不会编译;我得到:
错误 CS1503 参数 1:无法从 'iTextSharp.text.pdf.BaseFont' 转换为 'iTextSharp.text.Font.FontFamily'
...和:
错误 CS1503 参数 4:无法从 'System.Drawing.Color' 转换为 'iTextSharp.text.BaseColor' AYttFMScheduler
然后我根据添加到该博客的评论尝试了这个 post:
Font fMSGothic = FontFactory.GetFont("MS-PGothic", BaseFont.IDENTITY_H, 16);
. . .
CounselPointStr: {asmList[i
//].CounselPointStr}"));
].CounselPointStr}", courier));
...编译,但我得到一个 I/O 异常,更具体地说“文档没有页面”
这是错误的:
BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
Font courier = new Font(bfCourier, 12, Font.BOLD, Color.Black);
在第一行中,您创建了一个 BaseFont
对象。您可以使用该对象创建一个 Font
,如下所示:
Font courier = new Font(bfCourier, 12);
如果你想让字体加粗,你可以这样创建一个BaseFont
:
BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, false);
您遇到的错误是由于 Font
也可以用不同的方式创建:
Font courier = new Font(Font.FontFamily.COURIER, 12, Font.BOLD, BaseColor.BLACK);
你混淆了BaseFont
和FontFamily
的概念。字体系列(例如 Courier)是由不同字体(Courier regular、Courier bold、Courier italic、Courier bold-italic)组成的字体系列。基本字体是单一字体(例如 Courier 粗体)。
请注意,您还混淆了 类 Color
和 BaseColor
。实际上,您收到的错误消息是不言自明的。
"Document has no pages" 错误也解释了问题所在:当您 Close()
文档时,没有添加任何内容。也许您认为您已经添加了内容,但实际上您没有。如果您认为自己做到了,最可能的原因是抛出了另一个异常。例如:您使用了一种不存在的字体。 IOException
出现,您忽略它。因此,没有添加任何文本,您会收到 "Document has no pages" 错误。或者,您尝试添加 Image
但该图像的路径是错误的。因此,没有添加任何内容,您会收到 "Document has no pages" 错误。
在您的情况下,您可能会收到该错误,因为 asmList
为空。在这种情况下,您正在创建一个没有任何内容的文档,因为 asmList
.