我在现有的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();
缩写代码(我被要求删除评论中不必要的代码):
// 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();
答案 0 :(得分:0)
我找到了一种方法来更改IElements的BaseFont,因此设置了我想要的任何字体和大小(但也保留了内联标记样式的字体粗细和颜色)。
以下是上述缩写代码的示例:
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();
foreach (var es in elements)
{
//
var tempBase = BaseFont.CreateFont(lightFontFile, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var tempFont = new Font(tempBase, es.Chunks[0].Font.Size, es.Chunks[0].Font.Style, es.Chunks[0].Font.Color);
foreach (var chunk in es.Chunks)
{
chunk.Font = tempFont;
}
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();