/// <summary>
/// Converts a text file to a PDF document.
/// </summary>
/// <param name="sourceTextFilename">The filename of source text file.</param>
/// <param name="destPdfFilename">The filename of destination PDF document.</param>
public static void ConvertTextFileToPdfDocument(
string sourceTextFilename,
string destPdfFilename)
{
// font name
string fontName = "Arial";
// font size, in points
int fontSize = 12;
// text padding, in points
Vintasoft.Imaging.PaddingF textPadding = new Vintasoft.Imaging.PaddingF(30);
// get text from text file
string text = System.IO.File.ReadAllText(sourceTextFilename, System.Text.Encoding.UTF8);
// set new line to '\n' character
text = text.Replace("\r\n", "\n");
text = text.Replace("\n\r", "\n");
text = text.Replace("\r", "\n");
// create PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(destPdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14))
{
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font;
// find TTF font that should be used for drawing a text
using (Vintasoft.Imaging.Fonts.FontProgramSearchResult fontProgramSearchResult =
document.FontProgramsController.GetTrueTypeFontProgram(new Vintasoft.Imaging.Fonts.FontInfo(fontName)))
{
// create PDF font based on TTF font program
font = document.FontManager.CreateCIDFontFromTrueTypeFont(fontProgramSearchResult.FontProgramFilename);
}
do
{
// create page of A4 size
Vintasoft.Imaging.Pdf.Tree.PdfPage pdfPage =
new Vintasoft.Imaging.Pdf.Tree.PdfPage(document,
Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4));
// add page to the PDF document
document.Pages.Add(pdfPage);
// get PdfGraphics that is associated with PDF page
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics =
Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(pdfPage))
{
// create a brush that should be used for drawing a text
Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush =
new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black);
// specify a rectangle where text should be drawn
System.Drawing.RectangleF rect = pdfPage.MediaBox;
// apply padding
rect = textPadding.ApplyTo(rect);
// get text that must be drawn on current page
string drawnText = GetDrawnText(graphics, font, fontSize, rect.Width, rect.Height, ref text);
// draw text on the PDF page
graphics.DrawString(drawnText, font, fontSize, brush, rect, Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Left, true);
}
} while (!string.IsNullOrEmpty(text));
// subset font
document.FontManager.PackAllFonts();
// pack PDF document
document.Pack();
}
}
/// <summary>
/// Returns the text portion that can be drawn on specified PDF graphics in rectange with specified size.
/// </summary>
/// <param name="graphics">The PDF graphics.</param>
/// <param name="font">The text font.</param>
/// <param name="fontSize">The text font size.</param>
/// <param name="maxWidth">The width of text rectangle.</param>
/// <param name="maxHeight">The height of text rectangle.</param>
/// <param name="text">The text.</param>
/// <returns>The text portion that can be drawn in rectange.</returns>
private static string GetDrawnText(
Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics,
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font,
float fontSize,
float maxWidth,
float maxHeight,
ref string text)
{
float lineHeight = fontSize;
System.Text.StringBuilder textToDraw = new System.Text.StringBuilder();
float totalHeight = 0;
string line = null;
while (text != null || line != null)
{
// if there is not text line to process
if (line == null)
// cut next line from source text
line = CutTextPart(ref text, '\n');
// add line to the result text
textToDraw.Append(GetDrawnLine(graphics, font, fontSize, maxWidth, ref line));
textToDraw.Append("\n");
// increase height of result text
totalHeight += lineHeight;
// if height of result text is greater than rectangle height
if (totalHeight + lineHeight > maxHeight)
break;
}
if (line != null)
text = line + '\n' + text;
return textToDraw.ToString();
}
/// <summary>
/// Returns the line portion that must be drawn on specified PDF graphics in rectange with specified size.
/// </summary>
/// <param name="graphics">The PDF graphics.</param>
/// <param name="font">The text font.</param>
/// <param name="fontSize">The text font size.</param>
/// <param name="maxWidth">The width of text rectangle.</param>
/// <param name="line">The text line.</param>
/// <returns>The text portion that must be drawn.</returns>
private static string GetDrawnLine(
Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics,
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font,
float fontSize,
float maxWidth,
ref string line)
{
string drawnLine = null;
string word = null;
while (line != null)
{
// cut next word from line
word = CutTextPart(ref line, ' ');
// create next drawn line
string nextDrawnLine;
if (drawnLine != null)
nextDrawnLine = drawnLine.ToString() + ' ' + word;
else
nextDrawnLine = word;
// measure next drawn line
float currentWidth, currentHeight;
graphics.MeasureString(nextDrawnLine, font, fontSize, float.MaxValue, false, out currentWidth, out currentHeight);
// if next draw line width greater than max width
if (currentWidth > maxWidth)
{
// if drawn line is empty
if (drawnLine == null)
{
// add part of word to drawn line
drawnLine = word;
word = "";
do
{
word = drawnLine.Substring(drawnLine.Length - 1) + word;
drawnLine = drawnLine.Substring(0, drawnLine.Length - 1);
graphics.MeasureString(drawnLine.ToString(), font, fontSize, float.MaxValue, false, out currentWidth, out currentHeight);
}
while (currentWidth > maxWidth);
}
break;
}
if (drawnLine != null)
drawnLine += ' ';
drawnLine += word;
word = null;
}
if (word != null)
line = word + ' ' + line;
return drawnLine;
}
/// <summary>
/// Cuts a text part from text start to the specified separator.
/// </summary>
/// <param name="text">The source text.</param>
/// <param name="separator">The separator.</param>
/// <returns>The result text.</returns>
private static string CutTextPart(ref string text, char separator)
{
int newLineIndex = text.IndexOf(separator);
string result;
if (newLineIndex < 0)
{
result = text;
text = null;
return result;
}
result = text.Substring(0, newLineIndex);
if (newLineIndex == text.Length - 1)
text = null;
else
text = text.Substring(newLineIndex + 1);
return result;
}