Multiple PdfTextElement per page

0
Hi, is there an example that shows how to use multiple PdfTextElement per page? I have to display text from different Textfiles using different fonts.
pdf
 
asked 12/13/2018 8:35:52 AM
add a comment

3 Answers

0
Generally I have the problem that there will be many texts in this document where I write something in german with my standard font and then write the english term in italic and a smaller font next to the german text. For example: **Seriennummer** / *serial number*: xxx1234567 Or I need to write an address with company name bold and everything else aligned underneath in my header: **Company** Street 123 City So, what is the best way to write/align texts with different fonts without having to use DrawString and manually adding the positions for every part of the texts.
edited 12/14/2018 9:01:47 AM
answered 12/14/2018 8:53:55 AM
add a comment
0
Hi, my sample is based on the header/footer sample: namespace Report { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool _paginateStart = true; private RectangleF _bounds; private string CommonDataPath = @"..\.."; private void button1_Click(object sender, EventArgs e) { // Create a new instance of PdfDocument class. PdfDocument doc = new PdfDocument(); // Set document's information. doc.DocumentInformation.Author = "Author"; doc.DocumentInformation.Producer = "Producer"; doc.DocumentInformation.Creator = "Creator"; doc.DocumentInformation.Title = "Title"; //Add a page PdfPage page = doc.Pages.Add(); //Create fonts PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 11.5f); PdfStandardFont italicFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Italic); Font ttf = new Font("Calibri", 14f, FontStyle.Bold); PdfTrueTypeFont heading = new PdfTrueTypeFont(ttf, true); //Adding Header AddHeader(doc, "Header Title", "Header Description"); //Adding Footer AddFooter(doc, "@Copyright " DateTime.Now.Year); //Set formats PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Justify; string text = "About ..."; RectangleF column = new RectangleF(0, 20, page.Graphics.ClientSize.Width - 10f, page.Graphics.ClientSize.Height); _bounds = column; //Create text element to control the text flow PdfTextElement element = new PdfTextElement(text, font); element.Brush = new PdfSolidBrush(Color.Black); element.StringFormat = format; PdfLayoutSettings layoutFormat = new PdfLayoutSettings(); layoutFormat.Break = PdfLayoutBreakType.FitColumnsToPage; layoutFormat.Layout = PdfLayoutType.Paginate; //Get the remaining text that flows beyond the boundary. PdfTextLayoutResult result = element.Draw(page, _bounds, layoutFormat); //First Textblock string expath = CommonDataPath "\\Texts\\Text.de-DE.txt"; StreamReader reader = new StreamReader(expath, Encoding.GetEncoding(1252)); text = reader.ReadToEnd(); reader.Close(); element = new PdfTextElement(text, font); element.Brush = new PdfSolidBrush(Color.Black); element.StringFormat = format; // Next paragraph flow from last line of the previous paragraph. _bounds.Y = result.LastLineBounds.Y 35f; //Raise the event when the text flows to next page. element.BeforePageLayout = BeginPageLayout2; //Raise the event when the text reaches the end of the page. element.AfterPageLayout = EndPageLayout2; //result.Page.Graphics.DrawString("UltimatePdf", heading, PdfBrushes.DarkBlue, new PointF(_bounds.X, _bounds.Y)); _bounds.Y = result.LastLineBounds.Y 60f; result = element.Draw(result.Page, new RectangleF(_bounds.X, _bounds.Y, _bounds.Width, -1), layoutFormat); //Second Textblock expath = CommonDataPath "\\Texts\\Text.en-US.txt"; reader = new StreamReader(expath, Encoding.GetEncoding(1252)); text = reader.ReadToEnd(); reader.Close(); element = new PdfTextElement(text, italicFont); element.Brush = new PdfSolidBrush(Color.Black); element.StringFormat = format; // Next paragraph flow from last line of the previous paragraph. _bounds.Y = result.LastLineBounds.Y 35f; //Raise the event when the text flows to next page. element.BeforePageLayout = BeginPageLayout2; //Raise the event when the text reaches the end of the page. element.AfterPageLayout = EndPageLayout2; //result.Page.Graphics.DrawString("UltimatePdf", heading, PdfBrushes.DarkBlue, new PointF(_bounds.X, _bounds.Y)); _bounds.Y = result.LastLineBounds.Y 60f; result = element.Draw(result.Page, new RectangleF(_bounds.X, _bounds.Y, _bounds.Width, -1), layoutFormat); //Write Output if (!Directory.Exists(CommonDataPath)) Directory.CreateDirectory(CommonDataPath); // Save and close the document. var outputPath = Path.Combine(".\\", this.GetType().Name "_" Guid.NewGuid().ToString() ".pdf"); doc.Save(outputPath); doc.Close(true); } private static void EndPageLayout2(object sender, AfterPageLayoutEventArgs e) { AfterTextPageLayoutEventArgs args = (AfterTextPageLayoutEventArgs)e; PdfTextLayoutResult tlr = args.Result; args.NextPage = tlr.Page; } private void BeginPageLayout2(object sender, BeforePageLayoutEventArgs e) { RectangleF bounds = e.Bounds; // First column. if (!_paginateStart) { bounds.X = bounds.Width 20f; bounds.Y = 10f; } else { // Second column. bounds.X = 0f; _paginateStart = false; } e.Bounds = bounds; } } }
 
answered 12/14/2018 8:45:40 AM
add a comment
0
Please send us your code snippet using that `PdfTextElement` class so we can update the code for you.
 
answered 12/13/2018 4:18:40 PM
add a comment

Your Answer

Not the answer you're looking for? Browse other questions tagged pdf or ask your own question.