0
我正在向現有的pdf模板添加一些文本,圖像和html字符串。我已經設法添加文本和圖像,但是當我將這些html元素添加到ColumnText時,我無法弄清楚如何在該特定的ColumnText上設置字體和大小。在iTextSharp的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();
請從您的例子刪除所有不必要的代碼。在關於將HTML轉換爲PDF的問題中,看到您使用'cb.BeginText();'等是非常令人困惑的。當您將HTML轉換爲PDF時,您可以在HTML中設置字體和大小。 –
我編輯了問題@BrunoLowagie,這是我與康寶萊的一部分。 – gardarvalur