【发布时间】:2016-09-06 16:41:35
【问题描述】:
我正在向现有的 pdf 模板添加一些文本、图像和 html 字符串。我设法添加了文本和图像,但是当我将 html 元素添加到 ColumnText 时,我无法弄清楚如何在该特定 ColumnText 上设置字体和大小。
有什么方法可以设置 ColumnText 的字体和大小?
const string oldFile = "c:\\template.pdf";
const string newFile = "c:\\brandNewFile.pdf";
const string lightFontFile = "c:\\HelveticaNeueLight.ttf";
const string boldFontFile = "c:\\HelveticaNeueBoldItalic.ttf";
const string thinlight = "c:\\HelveticaNeueThinItalic.ttf";
const string logo = "c:\\logo.png";
// open the reader
var reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
var document = new Document(size);
// open the writer
var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
var writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
// Very nice font and size
var veryNiceBaseFont = BaseFont.CreateFont(boldFontFile, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(new BaseColor(0x00, 0x57, 0x94)); // Certain BLUE RGB
cb.SetFontAndSize(veryNiceBaseFont, 11f);
// Add text
cb.BeginText();
text = "Here is an amazing text that I add on top of the template!";
cb.SetCharacterSpacing(0.8f);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 79, 582, 0);
cb.EndText();
// Different font
var paragraphBaseFont = BaseFont.CreateFont(lightFontFile, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(paragraphBaseFont, 12f);
// Add more text
cb.BeginText();
string text = "More text in different font";
cb.SetCharacterSpacing(0.8f);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 79, 600, 0);
cb.EndText();
// Add the logo image
Image image = Image.FromFile(logo);
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, ImageFormat.Png);
if (pic.Height > pic.Width)
{
//Maximum height is 800 pixels.
float percentage = 0.0f;
percentage = 110 / pic.Height;
pic.ScalePercent(percentage * 100);
}
else
{
//Maximum width is 600 pixels.
float percentage = 0.0f;
percentage = 80 / pic.Width;
pic.ScalePercent(percentage * 100);
}
pic.SetAbsolutePosition(440, 575);
cb.AddImage(pic);
// Create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
// create the html part of the document
string html =
"<p>This is a paragraph. Derp derp derp derp !</p>" +
"<p><strong><span style=\"color: #005794;\">With some style and such.</span></strong></p>" +
"<p>DERP</p>" +
"<p><span style=\"color: #ed1c24;\">DERP</span></p>" +
"<p>DERP</p>";
var elements = XMLWorkerHelper.ParseToElementList(html, null);
var paragraph = new Paragraph();
paragraph.Font = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10f); //not working!!!!
//TODO: I am confused, should I try to set font as above, or somehow set columntext?
foreach (var es in elements)
{
paragraph.Add(es);
paragraph.Add(Chunk.NEWLINE);
}
var ct = new ColumnText(cb);
ct.SetSimpleColumn(paragraph, 80, 460, 475, 0, 15, Element.ALIGN_LEFT);
ct.Go();
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
缩写代码(我被要求删除 cmets 中不必要的代码):
// create the html part of the document
string html =
"<p>This is a paragraph. Derp derp derp derp !</p>" +
"<p><strong><span style=\"color: #005794;\">With some style and such.</span></strong></p>" +
"<p>DERP</p>" +
"<p><span style=\"color: #ed1c24;\">DERP</span></p>" +
"<p>DERP</p>";
var elements = XMLWorkerHelper.ParseToElementList(html, null);
var paragraph = new Paragraph();
paragraph.Font = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10f); //not working!!!!
//TODO: I am confused, should I try to set font as above, or somehow set columntext?
foreach (var es in elements)
{
paragraph.Add(es);
paragraph.Add(Chunk.NEWLINE);
}
var ct = new ColumnText(cb);
ct.SetSimpleColumn(paragraph, 80, 460, 475, 0, 15, Element.ALIGN_LEFT);
ct.Go();
【问题讨论】:
-
请从您的示例中删除所有不必要的代码。看到您在有关将 HTML 转换为 PDF 的问题中使用
cb.BeginText();等等,这非常令人困惑。将 HTML 转换为 PDF 时,需要在 HTML 中设置字体和大小。 -
我编辑了@BrunoLowagie 的问题,这是我遇到问题的部分。