1. Must use FontFactory.GetFont(...) to create a Font. Cannot use below to create a Font
paragraphTitleContent.Font=new Font();
paragraphTitleContent.Font.SetFontFamily("Your Font Family");
Paragraph paragraphTitleContent = new Paragraph("273509239@qq.com");
paragraphTitleContent.IndentationLeft = 0;
paragraphTitleContent.Alignment = Element.ALIGN_LEFT;
//here must use FontFactory.GetFont, cannot use new Font() and paragraphTitleContent.Font.SetFontFamily("fontFamily"), because the latter not work
paragraphTitleContent.Font =FontFactory.GetFont("Your Font Family");
paragraphTitleContent.Font.SetColor(255, 255, 25);
paragraphTitleContent.Font.Size = 12;
2. Set Style (Italic,Bold,Underline are bool type)
private static void SetStyleForTableOfContentParagraph(Paragraph paragraphContent, ReportSetupFontStyle fontStyle)
{
if (fontStyle.FontStyle.Italic && fontStyle.FontStyle.Bold && fontStyle.FontStyle.Underline)
{
paragraphContent.Font.SetStyle(Font.BOLDITALIC | Font.UNDERLINE);
}
else if (fontStyle.FontStyle.Italic && fontStyle.FontStyle.Bold)
{
paragraphContent.Font.SetStyle(Font.BOLDITALIC);
}
else if (fontStyle.FontStyle.Italic && fontStyle.FontStyle.Underline)
{
paragraphContent.Font.SetStyle(Font.ITALIC | Font.UNDERLINE);
}
else if (fontStyle.FontStyle.Bold && fontStyle.FontStyle.Underline)
{
paragraphContent.Font.SetStyle(Font.BOLD | Font.UNDERLINE);
}
else if (fontStyle.FontStyle.Underline)
{
paragraphContent.Font.SetStyle(Font.UNDERLINE);
}
else if (fontStyle.FontStyle.Bold)
{
paragraphContent.Font.SetStyle(Font.BOLD);
}
else if (fontStyle.FontStyle.Italic)
{
paragraphContent.Font.SetStyle(Font.ITALIC);
}
else
{
paragraphContent.Font.SetStyle(Font.NORMAL);
}
}
3. GetFont weight
internal static float GetFontWidth(BaseFont baseFont, string text, float fontSize)
{
fontSize = fontSize > 0 ? fontSize : ReportingPlatformConstant.DefaultFontSize;
return baseFont.GetWidthPoint(text, fontSize);
}
4. Add bookmark in PDF
public static void AddBookMarkInPDF(string outputFileAbsolutPath, BookMark bookmark)
{
if (System.IO.File.Exists(outputFileAbsolutPath) && bookmark != null && bookmark.BookMarkList != null && bookmark.BookMarkList.Count > 0)
{
List<TableOfContent> tocList = bookmark.BookMarkList.Where(x => x.HiddenTableOfContents == false).ToList();
if (tocList != null && tocList.Count > 0)
{
//temp file
string[] fileArray = outputFileAbsolutPath.Split('\\');
string fileNameTmp = fileArray[fileArray.Length - 1];
string[] alisNameArray = fileNameTmp.Split('.');
string tempFile = outputFileAbsolutPath.Replace(fileNameTmp, Guid.NewGuid().ToString() + "" + alisNameArray[alisNameArray.Length - 1]);
//string tempFile = outputFileAbsolutPath.Substring(0, outputFileAbsolutPath.LastIndexOf(".") - 1) + Guid.NewGuid().ToString() + ".pdf";
Document document = new Document(PageSize.LETTER.Rotate());
document.ResetPageCount();
FileStream fileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, fileStream);
document.Open();
PdfReader reader = new PdfReader(outputFileAbsolutPath);
copy.AddDocument(reader);
reader.Close();
List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
var temp = copy.GetImportedPage(reader, 1); // get 1st page
var h = temp.Height; // get height of 1st page
for (int i = 0; i < tocList.Count; i++)
{
// Add first item to bookmarks.
Dictionary<string, object> test = new Dictionary<string, object>();
test.Add("Action", "GoTo");
test.Add("Title", tocList[i].Title);
test.Add("Page", tocList[i].ActualStartPageNumber + " XYZ 0 " + h + " 0"); // use height of 1st page
bookmarks.Add(test);
}
// Do your worst and afterwards set the bookmarks to Outline. So yeah.
copy.Outlines = bookmarks;
document.Close();
System.IO.File.Delete(outputFileAbsolutPath);
FileInfo info = new FileInfo(tempFile);
info.MoveTo(outputFileAbsolutPath);
System.IO.File.Delete(tempFile);
}
}
}