Beispiel #1
0
        public virtual void manipulatePdf(string src, string dest)
        {
            PdfReader  reader  = new PdfReader(src);
            PdfStamper stamper = new PdfStamper(reader, new System.IO.FileStream(dest, System.IO.FileMode.Create, System.IO.FileAccess.Write));

            stamper.RotateContents = false;
            Phrase header = new Phrase("Copy", new Font(Font.FontFamily.HELVETICA, 14));

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                float x = reader.GetPageSize(i).Width / 2;
                float y = reader.GetPageSize(i).GetTop(20);
                ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_CENTER, header, x, y, 0);
            }
            stamper.Close();
            reader.Close();
        }
		/// <summary>
		/// 20130610 :: Add TextBackground modifiers to paint backgrounds.
		/// 20130718 :: Add arabic support
		/// </summary>
		/// <param name="chunk"></param>
		/// <param name="x"></param>
		/// <param name="width"></param>
		/// <param name="ELEMENT_ALIGN"></param>
		/// <param name="height"></param>
		/// <param name="txtBackgroundAttrs"></param>
		public void DrawBlockString(iTextSharp.text.Chunk chunk, float x, float width, int ELEMENT_ALIGN, float height, BlockStyle txtBackgroundAttrs )
		{
			//20130610 :: mellorasinxelas
			iTextSharp.text.pdf.ColumnText column = new iTextSharp.text.pdf.ColumnText(iPDFContent);
			
			float offsetTextInit = 0.0F;
			if(txtBackgroundAttrs != null && (txtBackgroundAttrs.BackgroundColor != null || txtBackgroundAttrs.BorderColor != null)){
				
				iPDFContent.SaveState();
				
				bool hasBorder = false;
				bool usePath = false;
				bool hasBackground = false;
				
				/*
				public const int TOP_BORDER = 1;
				public const int BOTTOM_BORDER = 2;
				public const int LEFT_BORDER = 4;
				public const int RIGHT_BORDER = 8;
				public const int NO_BORDER = 0;
				 */
				
				//iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle( x, current_y, width, height ); -> no runs
				
				//process border
				if(txtBackgroundAttrs.BorderColor != null && txtBackgroundAttrs.Borders != iTextSharp.text.Rectangle.NO_BORDER){
					iPDFContent.SetColorStroke( new BaseColor( txtBackgroundAttrs.BorderColor.Value ) );
					if(txtBackgroundAttrs.BorderWidth > 0.0F){
						iPDFContent.SetLineWidth( txtBackgroundAttrs.BorderWidth ); //add border width
						//rec.BorderWidth = txtBackgroundAttrs.BorderWidth;	-> no runs
						offsetTextInit = txtBackgroundAttrs.BorderWidth + 1; //add offset.
						hasBorder = true;
					}
					
					//process each border..
					if(txtBackgroundAttrs.Borders != 15){
						usePath = true; //paint border with lines..
					}
				}
				
				//process background
				if(txtBackgroundAttrs.BackgroundColor != null && txtBackgroundAttrs.FillBackground){
					iPDFContent.SetColorFill( new BaseColor( txtBackgroundAttrs.BackgroundColor.Value ) );
					//rec.BackgroundColor = new BaseColor( txtBackgroundAttrs.BackgroundColor.Value ); -> no runs
					hasBackground = true;
				}
				
				//iPDFContent.Rectangle( rec );//draw rectangle -> no runs
				if ((hasBorder && !usePath) || hasBackground){
					//put rectangle
					iPDFContent.Rectangle( x, current_y, width, height );//draw rectangle
				}
				
				if( (hasBorder && !usePath ) && hasBackground){
					iPDFContent.ClosePathFillStroke();
				}
				else if(hasBackground){
					iPDFContent.Fill();
				}
				else if(hasBorder && !usePath){
					iPDFContent.ClosePathStroke();
				}
				else{
					//nothing. If it has border color but hasn't border.
				}
				
				if(usePath){
					//need paint each border
					
					//first top
					if((txtBackgroundAttrs.Borders & iTextSharp.text.Rectangle.TOP_BORDER) == iTextSharp.text.Rectangle.TOP_BORDER ){
						iPDFContent.MoveTo(x, Current_y + height);
						iPDFContent.LineTo( x + width, Current_y + height);
						iPDFContent.Stroke();
					}
					//left
					if((txtBackgroundAttrs.Borders & iTextSharp.text.Rectangle.LEFT_BORDER) == iTextSharp.text.Rectangle.LEFT_BORDER ){
						iPDFContent.MoveTo(x, Current_y);
						iPDFContent.LineTo( x, Current_y + height);
						iPDFContent.Stroke();
					}
					//right
					if((txtBackgroundAttrs.Borders & iTextSharp.text.Rectangle.RIGHT_BORDER) == iTextSharp.text.Rectangle.RIGHT_BORDER ){
						iPDFContent.MoveTo(x + width, Current_y);
						iPDFContent.LineTo( x + width, Current_y + height);
						iPDFContent.Stroke();
					}
					//botton
					if((txtBackgroundAttrs.Borders & iTextSharp.text.Rectangle.BOTTOM_BORDER) == iTextSharp.text.Rectangle.BOTTOM_BORDER ){
						iPDFContent.MoveTo(x, Current_y);
						iPDFContent.LineTo( x + width, Current_y);
						iPDFContent.Stroke();
					}
				}
				
				iPDFContent.RestoreState();
			}
			//----
			
			//paint text column. Add offset
			column.SetSimpleColumn( x + offsetTextInit			, current_y ,
			                       x + width - offsetTextInit	, current_y + height
			                      );//add offset
			column.AddText(chunk);
			column.Alignment = ELEMENT_ALIGN;
			column.SetLeading(0, 1);
			
			//20130718 :: Add arabic support
			if (_detectRightToLeft && IsArabicHebrewText(chunk.Content))
			{
				column.RunDirection = iTextSharp.text.pdf.PdfWriter.RUN_DIRECTION_RTL;
			}
			//----
			
			column.Go();
		}
		/// <summary>
		/// Tested. Main draw operation.
		/// 
		/// 20130718 :: Add Arabic support
		/// </summary>
		/// <param name="cb"></param>
		/// <param name="phrase"></param>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <param name="width"></param>
		/// <param name="ELEMENT_ALIGN"></param>
		/// <param name="textBoxStyle"></param>
		public void DrawString(iTextSharp.text.pdf.PdfContentByte cb, iTextSharp.text.Phrase phrase,
		                       float x, float y, float width, int ELEMENT_ALIGN, IStyleContainer textBoxStyle)
		{
			iTextSharp.text.pdf.ColumnText column = new iTextSharp.text.pdf.ColumnText(cb);

			//add style, only SingleTextBoxStyle supported
			if (textBoxStyle != null && textBoxStyle is SingleTextBoxStyle)
			{
				Color? bckCol = ((SingleTextBoxStyle)textBoxStyle).BackgroundColor;
				if (bckCol != null && bckCol.HasValue)
				{
					if (phrase != null)
					{
						BaseColor color = new BaseColor(bckCol.Value);
						foreach (iTextSharp.text.Chunk act in phrase.Chunks)
						{
							act.SetBackground(color);//sets the same backcolor to the text.
						}
					}
				}
			}

			//column.SetSimpleColumn(x, y, x + width, (y + chunk.Font.Size + (chunk.Font.Size / 2) + 2));
			column.SetSimpleColumn(x, y - 2, (x + width), (y + 18));

			column.AddText(phrase);
			column.Alignment = ELEMENT_ALIGN;

			//20130718 :: Arabic support
			if (DetectRightToLeft && IsArabicHebrewText(phrase.Content))
			{
				column.RunDirection = iTextSharp.text.pdf.PdfWriter.RUN_DIRECTION_RTL;
			}
			//---

			column.Go();
		}
Beispiel #4
0
        // experimentell!
        public void createPdfFile(string filespec)
        {
            iTextSharp.text.Document doc = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(0, 0, box.Width, box.Height));
            dynamic writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new System.IO.FileStream(filespec, System.IO.FileMode.Create));
            dynamic bf = iTextSharp.text.pdf.BaseFont.CreateFont();
            doc.Open();
            iTextSharp.text.pdf.PdfContentByte over = writer.DirectContent;
            for (i = 0; i <= p_objects.Count - 1; i++) {
                RECT r = p_objects[i].GetOuterBounds;
                if (p_objects[i] is VTextbox) {
                    VTextbox src = p_objects[i];

                    //ColumnText can contain paraphraphs as well as be abolsutely positioned
                    iTextSharp.text.pdf.ColumnText Col = new iTextSharp.text.pdf.ColumnText(over);

                    //Set the x,y,width,height
                    Col.SetSimpleColumn(r.Left, box.Height - r.Top, r.Right, box.Height - r.Bottom);

                    //Create our paragraph
                    iTextSharp.text.Paragraph P = new iTextSharp.text.Paragraph();
                    //Create our base font, assumes you have arial installed in the normal location and that CP1252 works with it
                    //Dim BF = BaseFont.CreateFont(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Windows), "Fonts\arial.ttf"), BaseFont.CP1252, False)
                    //Add two chunks that will be placed side-by-side but with different font weights
                    //P.Add(New iTextSharp.text.Chunk("BOLD TEXT: ", New iTextSharp.text.Font(bf, 10.0, iTextSharp.text.Font.BOLD)))
                    string fontFilespec = Helper.GetFontFilespec(src.Font.FontFamily.Name);
                    iTextSharp.text.Font fnt = default(iTextSharp.text.Font);
                    if (!string.IsNullOrEmpty(fontFilespec)) {
                        fnt = new iTextSharp.text.Font(iTextSharp.text.pdf.BaseFont.CreateFont(fontFilespec, iTextSharp.text.pdf.BaseFont.CP1252, false), src.Font.SizeInPoints, iTextSharp.text.Font.NORMAL);
                    } else {
                        fnt = new iTextSharp.text.Font(bf, src.Font.SizeInPoints, iTextSharp.text.Font.NORMAL);
                    }
                    P.Add(new iTextSharp.text.Chunk(src.Text, fnt));
                    //Add the paragraph to the ColumnText
                    Col.AddText(P);
                    //Call to stupid Go() method which actually writes the content to the stream.
                    Col.Go();

                //
                //Dim txtbox As New iTextSharp.text.pdf.PdfPCell()

                //Dim f = New iTextSharp.text.Font(bf)
                //f.SetColor(0, 0, 0)
                //over.SetFontAndSize(bf, 10)
                //over.BeginText()
                //over.ShowTextAligned(1, src.Text, r.Left, box.Height - r.Bottom, 0)
                //over.EndText()
                } else {
                    dynamic img = iTextSharp.text.Image.GetInstance(p_objects[i].GetAsImage, System.Drawing.Imaging.ImageFormat.Png);
                    img.SetAbsolutePosition(r.Left, box.Height - r.Bottom);
                    img.SetDpi(72, 72);
                    //doc.Add(img)
                    over.AddImage(img);
                }
            }
            doc.Close();
        }