Exemplo n.º 1
0
 /// <summary>
 /// Add Font Resources to the pdf page
 /// </summary>
 internal void AddResource(PdfFonts fonts, int contentRef)
 {
     foreach (PdfFontEntry font in fonts.Fonts.Values)
     {
         fontRef += string.Format("/{0} {1} 0 R", font.font, font.objectNum);
     }
     if (contentRef > 0)
     {
         contents = string.Format("/Contents {0} 0 R", contentRef);
     }
 }
Exemplo n.º 2
0
        public void Start()
        {
            // Create the anchor for all pdf objects
            CompressionConfig cc = RdlEngineConfig.GetCompression();

            anchor = new PdfAnchor(cc != null);

            //Create a PdfCatalog
            string lang;

            if (r.ReportDefinition.Language != null)
            {
                lang = r.ReportDefinition.Language.EvaluateString(this.r, null);
            }
            else
            {
                lang = null;
            }
            catalog = new PdfCatalog(anchor, lang);

            //Create a Page Tree Dictionary
            pageTree = new PdfPageTree(anchor);

            //Create a Font Dictionary
            fonts = new PdfFonts(anchor);

            //Create a Pattern Dictionary
            patterns = new PdfPattern(anchor);

            //Create an Image Dictionary
            images = new PdfImages(anchor);

            //Create an Outline Dictionary
            outline = new PdfOutline(anchor);

            //Create the info Dictionary
            info = new PdfInfo(anchor);

            //Set the info Dictionary.
            info.SetInfo(r.Name, r.Author, r.Description, "");          // title, author, subject, company

            //Create a utility object
            pdfUtility = new PdfUtility(anchor);

            //write out the header
            int size = 0;

            tw.Write(pdfUtility.GetHeader("1.5", out size), 0, size);
            filesize = size;
        }
Exemplo n.º 3
0
        public void Start()
        {
            // Create the anchor for all pdf objects
            CompressionConfig cc = RdlEngineConfig.GetCompression();
            anchor = new PdfAnchor(cc != null);

            //Create a PdfCatalog
            string lang;
            if (r.ReportDefinition.Language != null)
                lang = r.ReportDefinition.Language.EvaluateString(this.r, null);
            else
                lang = null;
            catalog= new PdfCatalog(anchor, lang);

            //Create a Page Tree Dictionary
            pageTree= new PdfPageTree(anchor);

            //Create a Font Dictionary
            fonts = new PdfFonts(anchor);

            //Create a Pattern Dictionary
            patterns = new PdfPattern(anchor);

            //Create an Image Dictionary
            images = new PdfImages(anchor);

            //Create an Outline Dictionary
            outline = new PdfOutline(anchor);

            //Create the info Dictionary
            info=new PdfInfo(anchor);

            //Set the info Dictionary.
            info.SetInfo(r.Name,r.Author,r.Description,"");	// title, author, subject, company

            //Create a utility object
            pdfUtility=new PdfUtility(anchor);

            //write out the header
            int size=0;
            tw.Write(pdfUtility.GetHeader("1.5",out size),0,size);
            filesize = size;
        }
Exemplo n.º 4
0
        public void Start()
        {
            // Create the anchor for all pdf objects
            CompressionConfig cc = RdlEngineConfig.GetCompression();
            anchor = new PdfAnchor(cc != null);

            //Create a PdfCatalog
            string lang;
            if (r.ReportDefinition.Language != null)
                lang = r.ReportDefinition.Language.EvaluateString(this.r, null);
            else
                lang = null;
            catalog = new PdfCatalog(anchor, lang);

            //Create a Page Tree Dictionary
            pageTree = new PdfPageTree(anchor);

            //Create a Font Dictionary
            fonts = new PdfFonts(anchor);

            //Create a Pattern Dictionary
            patterns = new PdfPattern(anchor);

            //Create an Image Dictionary
            images = new PdfImages(anchor);

            //Create an Outline Dictionary
            outline = new PdfOutline(anchor);

            //Create the info Dictionary
            info = new PdfInfo(anchor);

            //Set the info Dictionary. 
            info.SetInfo(r.Name, r.Author, r.Description, "");	// title, author, subject, company

            //Create a utility object
            pdfUtility = new PdfUtility(anchor);

            //write out the header
            int size = 0;

            if (r.ItextPDF)
            {
                PdfWriter writer = PdfWriter.GetInstance(pdfdocument, ms);
                pdfdocument.Open();
                cb = writer.DirectContent;
                pdfdocument.AddAuthor(r.Author);
                pdfdocument.AddCreationDate();
                pdfdocument.AddCreator("Majorsilence Reporting");
                pdfdocument.AddSubject(r.Description);
                pdfdocument.AddTitle(r.Name);

            }
            else
            {
                tw.Write(pdfUtility.GetHeader("1.5", out size), 0, size);
            }

            //
            filesize = size;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Page Text element at the X Y position; multiple lines handled
        /// </summary>
        /// <returns></returns>
		private void iAddText (float x, float y, float height, float width, string[] sa,
			StyleInfo si, PdfFonts fonts, float[] tw, bool bWrap, string url, bool bNoClip, string tooltip)
		{
			int platform = (int)Environment.OSVersion.Platform;
			int version = (int)Environment.OSVersion.Version.Major;

			BaseFont bf;
			string face = iFontNameNormalize (si.FontFamily);
			string fontname = "";
			bool fonttype1 = true;
			var folder = FontFolder; //Call to determine folder and set value of dejavuFonts;
			if (face == "Times-Roman") {
				if (si.IsFontBold () && si.FontStyle == FontStyleEnum.Italic) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "TimesNewRomanPS-BoldItalicMT";
						fontname = "Times New Roman Bold Italic.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Serif Condensed Bold Italic" : "Times-BoldItalic";
						fontname = (dejavuFonts ? "DejaVuSerifCondensed-BoldItalic.ttf" : "timesbi.ttf");
					}
				} else if (si.IsFontBold ()) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "TimesNewRomanPS-BoldMT";
						fontname = "Times New Roman Bold.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Serif Condensed Bold" : "Times-Bold";
						fontname = (dejavuFonts ? "DejaVuSerifCondensed-Bold.ttf" : "timesbd.ttf");
					}
				} else if (si.FontStyle == FontStyleEnum.Italic) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "TimesNewRomanPS-ItalicMT";
						fontname = "Times New Roman Italic.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Serif Condensed Italic" : "Times-Italic";
						fontname = (dejavuFonts ? "DejaVuSerifCondensed-Italic.ttf" : "timesi.ttf");
					}
				} else {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "TimesNewRomanPSMT";
						fontname = "Times New Roman.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Serif Condensed" : face;
						fontname = (dejavuFonts ? "DejaVuSerifCondensed.ttf" : "times.ttf");
					}
				}
				fonttype1 = false;
			} else if (face == "Arial") {
				if (si.IsFontBold () && si.FontStyle == FontStyleEnum.Italic) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "Arial BoldItalicMT";
						fontname = "Arial Bold Italic.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Condensed Bold Oblique" : "Arial-BoldItalic";
						fontname = (dejavuFonts ? "DejaVuSansCondensed-BoldOblique.ttf" : "arialbi.ttf");
					}
				} else if (si.IsFontBold ()) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "Arial-BoldMT";
						fontname = "Arial Bold.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Condensed Bold" : "Arial-Bold";
						fontname = (dejavuFonts ? "DejaVuSansCondensed-Bold.ttf" : "arialbd.ttf");
					}
				} else if (si.FontStyle == FontStyleEnum.Italic) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "Arial-ItalicMT";
						fontname = "Arial Italic.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Condensed Oblique" : "Arial-Italic";
						fontname = (dejavuFonts ? "DejaVuSansCondensed-Oblique.ttf" : "ariali.ttf");
					}
				} else {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "ArialMT";
						fontname = "Arial.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Condensed" : face;
						fontname = (dejavuFonts ? "DejaVuSansCondensed.ttf" : "arial.ttf");
					}
				}
				fonttype1 = false;
			} else if (face == "Courier New") {
				if (si.IsFontBold () && si.FontStyle == FontStyleEnum.Italic) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "CourierNewPS-BoldItalicMT";
						fontname = "Courier New Bold Italic.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Mono Bold Oblique" : "Courier New-BoldItalic";
						fontname = (dejavuFonts ? "DejaVuSansMono-BoldOblique.ttf" : "courbi.ttf");
					}
				} else if (si.IsFontBold ()) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "CourierNewPS-BoldMT";
						fontname = "Courier New Bold.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Mono Bold" : "Courier New-Bold";
						fontname = (dejavuFonts ? "DejaVuSansMono-Oblique.ttf" : "courbd.ttf");
					}
				} else if (si.FontStyle == FontStyleEnum.Italic) {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "CourierNewPS-ItalicMT";
						fontname = "Courier New Italic.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Mono Oblique" : "Courier New-Italic";
						fontname = (dejavuFonts ? "DejaVuSansMono-Oblique.ttf" : "couri.ttf");
					}
				} else {
					//OSX
					if ((platform == 4 || platform == 6 || platform == 128) && version > 8) {
						face = "CourierNewPSMT";
						fontname = "Courier New.ttf";
					} else {
						face = dejavuFonts ? "DejaVu Sans Mono" : face;
						fontname = (dejavuFonts ? "DejaVuSansMono.ttf" : "cour.ttf");
					}
				}
				fonttype1 = false;
			} else {
                        if (si.IsFontBold() &&
                    si.FontStyle == FontStyleEnum.Italic)   // bold and italic?
                            face = face + "-BoldOblique";
                        else if (si.IsFontBold())           // just bold?
                            face = face + "-Bold";
                        else if (si.FontStyle == FontStyleEnum.Italic)
                            face = face + "-Oblique";
                        fonttype1 = true;
                    }
            //Get index of fontname in List font name
            int indexbf = BaseFontsname.FindIndex(delegate(string _fontname) { return _fontname == face; });
            //If not found then add new BaseFont
            if (indexbf == -1)
            {
                BaseFontsname.Add(face);
                if (fonttype1)
                {
                    bf = BaseFont.CreateFont(face, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                }
                else
                {
                    string path = System.IO.Path.Combine(folder, fontname);
                    bf = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                }
                BaseFonts.Add(bf);
            }
            else
            //Get from List
            {
                bf = BaseFonts[indexbf];
            }

            // Loop thru the lines of text
            for (int i = 0; i < sa.Length; i++)
            {
                string text = sa[i];
                float textwidth = tw[i];
                // Calculate the x positino
                float startX = x + si.PaddingLeft;						// TODO: handle tb_rl
                float startY = y + si.PaddingTop + (i * si.FontSize);	// TODO: handle tb_rl
                int align = 0;
                if (si.WritingMode == WritingModeEnum.lr_tb)
                {	// TODO: not sure what alignment means with tb_lr so I'll leave it out for now
                    switch (si.TextAlign)
                    {
                        case TextAlignEnum.Center:
                            if (width > 0)
                            {
                                startX = x + si.PaddingLeft + (width - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2;
                                align = Element.ALIGN_CENTER;
                            }
                            break;
                        case TextAlignEnum.Right:
                            if (width > 0)
                            {
                                startX = x + width - textwidth - si.PaddingRight;
                                align = Element.ALIGN_RIGHT;
                            }
                            break;
                        case TextAlignEnum.Left:
                        default:
                            align = Element.ALIGN_LEFT;
                            break;
                    }

                    // Calculate the y position
                    switch (si.VerticalAlign)
                    {
                        case VerticalAlignEnum.Middle:
                            if (height <= 0)
                                break;

                            // calculate the middle of the region
                            startY = y + si.PaddingTop + (height - si.PaddingTop - si.PaddingBottom) / 2 - si.FontSize / 2;
                            // now go up or down depending on which line
                            if (sa.Length == 1)
                                break;
                            if (sa.Length % 2 == 0)	// even number
                            {
                                startY = startY - ((sa.Length / 2 - i) * si.FontSize) + si.FontSize / 2;
                            }
                            else
                            {
                                startY = startY - ((sa.Length / 2 - i) * si.FontSize);
                            }
                            break;
                        case VerticalAlignEnum.Bottom:
                            if (height <= 0)
                                break;

                            startY = y + height - si.PaddingBottom - (si.FontSize * (sa.Length - i));
                            break;
                        case VerticalAlignEnum.Top:
                        default:
                            break;
                    }
                }
                else
                {
                    //25072008 GJL - Move x in a little - it draws to close to the edge of the rectangle (25% of the font size seems to work!) and Center or right align vertical text
                    startX += si.FontSize / 4;

                    switch (si.TextAlign)
                    {
                        case TextAlignEnum.Center:
                            if (height > 0)
                                startY = y + si.PaddingLeft + (height - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2;
                            break;
                        case TextAlignEnum.Right:
                            if (width > 0)
                                startY = y + height - textwidth - si.PaddingRight;
                            break;
                        case TextAlignEnum.Left:
                        default:
                            break;
                    }

                }

                // Draw background rectangle if needed (only put out on the first line, since we do whole rectangle)
                if (!si.BackgroundColor.IsEmpty && height > 0 && width > 0 && i == 0)
                {	// background color, height and width are specified
                    iAddFillRect(x, y, width, height, si.BackgroundColor);
                }

                // Set the clipping path, (Itext have no clip)
                if (height > 0 && width > 0)
                {
                    cb.SetRGBColorFill(si.Color.R, si.Color.G, si.Color.B);

                    if (si.WritingMode == WritingModeEnum.lr_tb)
                    {
                        //If textline after measure with word break can fit just simple show Text
                        if (width >= textwidth)
                        {
                            cb.BeginText();
                            cb.SetFontAndSize(bf, si.FontSize);
                            cb.SetTextMatrix(startX, (_pSize.yHeight - startY - si.FontSize));
                            cb.ShowText(text);
                            cb.EndText();
                        }
                        else
                        {
                            //else use Column text to wrap or clip (wrap: for example a text like an URL so word break is not working here, itextsharp ColumnText do the work for us)
                            ColumnText ct = new ColumnText(cb);
                            Phrase myPhrase = new Phrase(text, new iTextSharp.text.Font(bf, si.FontSize));
                            ct.SetSimpleColumn(myPhrase, x + si.PaddingLeft, _pSize.yHeight - startY, x + width - si.PaddingRight, _pSize.yHeight - y - si.PaddingBottom - height, 10f, align);
                            ct.Go();
                        }
                    }
                    else
                    {
                        //Not checked
                        double rads = -283.0 / 180.0;
                        double radsCos = Math.Cos(rads);
                        double radsSin = Math.Sin(rads);
                        cb.BeginText();
                        cb.SetFontAndSize(bf, si.FontSize);
                        cb.SetTextMatrix((float)radsCos, (float)radsSin, (float)-radsSin, (float)radsCos, startX, _pSize.yHeight - startY);
                        cb.ShowText(text);
                        cb.EndText();
                    }

                    //add URL
                    if (url != null)
						pdfdocument.Add(new Annotation(x, _pSize.yHeight - (y + height), width + x, _pSize.yHeight - y, url));
                    //add tooltip
                    if (tooltip != null)
						pdfdocument.Add(new Annotation(x, _pSize.yHeight - (y + height), width + x, _pSize.yHeight - y, tooltip));

                }

                // Handle underlining etc.
                float maxX;
                switch (si.TextDecoration)
                {
                    case TextDecorationEnum.Underline:
                        maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth;
                        iAddLine(startX, startY + si.FontSize + 1, maxX, startY + si.FontSize + 1, 1, si.Color, BorderStyleEnum.Solid);
                        break;
                    case TextDecorationEnum.LineThrough:
                        maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth;
                        iAddLine(startX, startY + (si.FontSize / 2) + 1, maxX, startY + (si.FontSize / 2) + 1, 1, si.Color, BorderStyleEnum.Solid);
                        break;
                    case TextDecorationEnum.Overline:
                        maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth;
                        iAddLine(startX, startY + 1, maxX, startY + 1, 1, si.Color, BorderStyleEnum.Solid);
                        break;
                    case TextDecorationEnum.None:
                    default:
                        break;
                }
            }

            iAddBorder(si, x, y, height, width);			// add any required border

            return;
        }
Exemplo n.º 6
0
		/// <summary>
		/// Page Text element at the X Y position; multiple lines handled
		/// </summary>
		/// <returns></returns>
		internal void AddText(float x, float y, float height, float width, string[] sa, 
			StyleInfo si, PdfFonts fonts, float[] tw, bool bWrap, string url, bool bNoClip,string tooltip)
		{
			// Calculate the RGB colors e.g. RGB(255, 0, 0) = red = 1 0 0 rg
			double r = si.Color.R; 
			double g = si.Color.G; 
			double b = si.Color.B;
			r = Math.Round((r/255),3);
			g = Math.Round((g/255),3);
			b = Math.Round((b/255),3);

			string pdfFont = fonts.GetPdfFont(si);	// get the pdf font resource name

			// Loop thru the lines of text
			for (int i=0; i < sa.Length; i++)
			{
				string text = sa[i];
				float textwidth = tw[i];
				// Calculate the x position
				float startX = x + si.PaddingLeft;						// TODO: handle tb_rl
				float startY = y + si.PaddingTop + (i * si.FontSize);	// TODO: handle tb_rl

				if (si.WritingMode == WritingModeEnum.lr_tb)
				{	// TODO: not sure what alignment means with tb_lr so I'll leave it out for now
					switch(si.TextAlign)
					{
						case TextAlignEnum.Center:
							if (width > 0)
								startX = x + si.PaddingLeft + (width - si.PaddingLeft - si.PaddingRight)/2 - textwidth/2;	 
							break;
						case TextAlignEnum.Right:
							if (width > 0)
								startX = x + width - textwidth - si.PaddingRight;	
							break;
						case TextAlignEnum.Left:
						default:
							break;
					}

					// Calculate the y position
					switch(si.VerticalAlign)
					{
						case VerticalAlignEnum.Middle:
							if (height <= 0)
								break;

							// calculate the middle of the region
							startY = y + si.PaddingTop + (height - si.PaddingTop - si.PaddingBottom)/2 - si.FontSize/2;	 
							// now go up or down depending on which line
							if (sa.Length == 1)
								break;
							if (sa.Length % 2 == 0)	// even number
							{
								startY = startY - ((sa.Length/2 - i) * si.FontSize) + si.FontSize/2;
							}
							else
							{
								startY = startY - ((sa.Length/2 - i) * si.FontSize);
							}
							break;
						case VerticalAlignEnum.Bottom:
							if (height <= 0)
								break;
							
							startY = y + height - si.PaddingBottom - (si.FontSize * (sa.Length-i));	
							break;
						case VerticalAlignEnum.Top:
						default:
							break;
					}
				}
                else
                {
					//25072008 GJL - Move x in a little - it draws to close to the edge of the rectangle (25% of the font size seems to work!) and Center or right align vertical text
                    startX += si.FontSize / 4;
                   
                    switch (si.TextAlign)
                    {
                        case TextAlignEnum.Center:
                            if (height > 0)
                                startY = y + si.PaddingLeft + (height - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2;
                            break;
                        case TextAlignEnum.Right:
                            if (width > 0)
                                startY = y + height - textwidth - si.PaddingRight;
                            break;
                        case TextAlignEnum.Left:
                        default:
                            break;
                    }
 
                }

				// Draw background rectangle if needed (only put out on the first line, since we do whole rectangle)
				if (!si.BackgroundColor.IsEmpty && height > 0 && width > 0 && i == 0)
				{	// background color, height and width are specified
					AddFillRect(x, y, width, height, si.BackgroundColor);
				}

				// Set the clipping path
				if (height > 0 && width > 0)
				{
					if (bNoClip)	// no clipping but we still want URL checking
						elements.Append("\r\nq\t");
					else
						elements.AppendFormat(NumberFormatInfo.InvariantInfo,
							"\r\nq\t{0} {1} {2} {3} re W n",
							x, pSize.yHeight-y-height, width, height);
					if (url != null)
						p.AddHyperlink(x, pSize.yHeight-y, height, width, url);
                    if (tooltip != null)
                        p.AddToolTip(x, pSize.yHeight - y, height, width, tooltip);

				}
				else
					elements.Append("\r\nq\t");

				// Escape the text
                string newtext = PdfUtility.UTF16StringQuoter(text);
                //string newtext = text.Replace("\\", "\\\\");
                //newtext = newtext.Replace("(", "\\(");
                //newtext = newtext.Replace(")", "\\)");
				if (si.WritingMode == WritingModeEnum.lr_tb)
				{
					elements.AppendFormat(NumberFormatInfo.InvariantInfo,
						"\r\nBT/{0} {1} Tf\t{5} {6} {7} rg\t{2} {3} Td \t({4}) Tj\tET\tQ\t",
						pdfFont,si.FontSize,startX,(pSize.yHeight-startY-si.FontSize),newtext, r, g, b);
				}
				else
				{	// Rotate text -90 degrees=-.5 radians (this works for english don't know about true tb-rl language)
					//   had to play with reader to find best approximation for this rotation; didn't do what I expected
					//    see pdf spec section 4.2.2 pg 141  "Common Transformations"

					elements.AppendFormat(NumberFormatInfo.InvariantInfo,
						"\r\nBT/{0} {1} Tf\t{5} {6} {7} rg\t{8} {9} {10} {11} {2} {3} Tm \t({4}) Tj\tET\tQ\t",
						pdfFont,si.FontSize,startX,(pSize.yHeight-startY),newtext, r, g, b,
						radsCos, radsSin, -radsSin, radsCos);
				}

				// Handle underlining etc.
				float maxX;
				switch (si.TextDecoration)
				{
					case TextDecorationEnum.Underline:
						maxX = width > 0? Math.Min(x + width, startX+textwidth): startX+textwidth;
						AddLine(startX, startY+si.FontSize+1, maxX, startY+si.FontSize+1, 1, si.Color, BorderStyleEnum.Solid);
						break;
					case TextDecorationEnum.LineThrough:
						maxX = width > 0? Math.Min(x + width, startX+textwidth): startX+textwidth;
						AddLine(startX, startY+(si.FontSize/2)+1, maxX, startY+(si.FontSize/2)+1, 1, si.Color, BorderStyleEnum.Solid);
						break;
					case TextDecorationEnum.Overline:
						maxX = width > 0? Math.Min(x + width, startX+textwidth): startX+textwidth;
						AddLine(startX, startY+1, maxX, startY+1, 1, si.Color, BorderStyleEnum.Solid);
						break;
					case TextDecorationEnum.None:
					default:
						break;
				}
			}

			AddBorder(si, x, y, height, width);			// add any required border
		
			return;
		}
Exemplo n.º 7
0
        /// <summary>
        /// Page Text element at the X Y position; multiple lines handled
        /// </summary>
        /// <returns></returns>
        internal void AddText(float x, float y, float height, float width, string[] sa,
                              StyleInfo si, PdfFonts fonts, float[] tw, bool bWrap, string url, bool bNoClip)
        {
            // Calculate the RGB colors e.g. RGB(255, 0, 0) = red = 1 0 0 rg
            double r = si.Color.R;
            double g = si.Color.G;
            double b = si.Color.B;

            r = Math.Round((r / 255), 3);
            g = Math.Round((g / 255), 3);
            b = Math.Round((b / 255), 3);

            string pdfFont = fonts.GetPdfFont(si);              // get the pdf font resource name

            // Loop thru the lines of text
            for (int i = 0; i < sa.Length; i++)
            {
                string text      = sa[i];
                float  textwidth = tw[i];
                // Calculate the x position
                float startX = x + si.PaddingLeft;                                      // TODO: handle tb_rl
                float startY = y + si.PaddingTop + (i * si.FontSize);                   // TODO: handle tb_rl

                if (si.WritingMode == WritingModeEnum.lr_tb)
                {                       // TODO: not sure what alignment means with tb_lr so I'll leave it out for now
                    switch (si.TextAlign)
                    {
                    case TextAlignEnum.Center:
                        if (width > 0)
                        {
                            startX = x + si.PaddingLeft + (width - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2;
                        }
                        break;

                    case TextAlignEnum.Right:
                        if (width > 0)
                        {
                            startX = x + width - textwidth - si.PaddingRight;
                        }
                        break;

                    case TextAlignEnum.Left:
                    default:
                        break;
                    }

                    // Calculate the y position
                    switch (si.VerticalAlign)
                    {
                    case VerticalAlignEnum.Middle:
                        if (height <= 0)
                        {
                            break;
                        }

                        // calculate the middle of the region
                        startY = y + si.PaddingTop + (height - si.PaddingTop - si.PaddingBottom) / 2 - si.FontSize / 2;
                        // now go up or down depending on which line
                        if (sa.Length == 1)
                        {
                            break;
                        }
                        if (sa.Length % 2 == 0)                                 // even number
                        {
                            startY = startY - ((sa.Length / 2 - i) * si.FontSize) + si.FontSize / 2;
                        }
                        else
                        {
                            startY = startY - ((sa.Length / 2 - i) * si.FontSize);
                        }
                        break;

                    case VerticalAlignEnum.Bottom:
                        if (height <= 0)
                        {
                            break;
                        }

                        startY = y + height - si.PaddingBottom - (si.FontSize * (sa.Length - i));
                        break;

                    case VerticalAlignEnum.Top:
                    default:
                        break;
                    }
                }

                // Draw background rectangle if needed (only put out on the first line, since we do whole rectangle)
                if (!si.BackgroundColor.IsEmpty && height > 0 && width > 0 && i == 0)
                {                       // background color, height and width are specified
                    AddFillRect(x, y, width, height, si.BackgroundColor);
                }

                // Set the clipping path
                if (height > 0 && width > 0)
                {
                    if (bNoClip)                        // no clipping but we still want URL checking
                    {
                        elements.Append("\r\nq\t");
                    }
                    else
                    {
                        elements.AppendFormat(NumberFormatInfo.InvariantInfo,
                                              "\r\nq\t{0} {1} {2} {3} re W n",
                                              x, pSize.yHeight - y - height, width, height);
                    }
                    if (url != null)
                    {
                        p.AddHyperlink(x, pSize.yHeight - y, height, width, url);
                    }
                }
                else
                {
                    elements.Append("\r\nq\t");
                }

                // Escape the text

                string newtext = PdfUtility.UTF16StringQuoter(text);

                //string newtext = text.Replace("\\", "\\\\");
                //newtext = newtext.Replace("(", "\\(");
                //newtext = newtext.Replace(")", "\\)");

                if (si.WritingMode == WritingModeEnum.lr_tb)
                {
                    elements.AppendFormat(NumberFormatInfo.InvariantInfo,
                                          "\r\nBT/{0} {1} Tf\t{5} {6} {7} rg\t{2} {3} Td \t({4}) Tj\tET\tQ\t",
                                          pdfFont, si.FontSize, startX, (pSize.yHeight - startY - si.FontSize), newtext, r, g, b);
                }
                else
                {                       // Rotate text -90 degrees=-.5 radians (this works for english don't know about true tb-rl language)
                                        //   had to play with reader to find best approximation for this rotation; didn't do what I expected
                                        //    see pdf spec section 4.2.2 pg 141  "Common Transformations"

                    elements.AppendFormat(NumberFormatInfo.InvariantInfo,
                                          "\r\nBT/{0} {1} Tf\t{5} {6} {7} rg\t{8} {9} {10} {11} {2} {3} Tm \t({4}) Tj\tET\tQ\t",
                                          pdfFont, si.FontSize, startX, (pSize.yHeight - startY), newtext, r, g, b,
                                          radsCos, radsSin, -radsSin, radsCos);
                }

                // Handle underlining etc.
                float maxX;
                switch (si.TextDecoration)
                {
                case TextDecorationEnum.Underline:
                    maxX = width > 0? Math.Min(x + width, startX + textwidth): startX + textwidth;
                    AddLine(startX, startY + si.FontSize + 1, maxX, startY + si.FontSize + 1, 1, si.Color, BorderStyleEnum.Solid);
                    break;

                case TextDecorationEnum.LineThrough:
                    maxX = width > 0? Math.Min(x + width, startX + textwidth): startX + textwidth;
                    AddLine(startX, startY + (si.FontSize / 2) + 1, maxX, startY + (si.FontSize / 2) + 1, 1, si.Color, BorderStyleEnum.Solid);
                    break;

                case TextDecorationEnum.Overline:
                    maxX = width > 0? Math.Min(x + width, startX + textwidth): startX + textwidth;
                    AddLine(startX, startY + 1, maxX, startY + 1, 1, si.Color, BorderStyleEnum.Solid);
                    break;

                case TextDecorationEnum.None:
                default:
                    break;
                }
            }

            AddBorder(si, x, y, height, width);                                 // add any required border

            return;
        }
Exemplo n.º 8
0
		/// <summary>
		/// Add Font Resources to the pdf page
		/// </summary>
		internal void AddResource(PdfFonts fonts,int contentRef)
		{
			foreach (PdfFontEntry font in fonts.Fonts.Values)
			{
				fontRef+=string.Format("/{0} {1} 0 R",font.font,font.objectNum);
			}
			if(contentRef>0)
			{
				contents=string.Format("/Contents {0} 0 R",contentRef);
			}
		}