using System;using System.Collections.Generic;using System.Linq;using System.Text;using iTextSharp.text;using iTextSharp.text.pdf;using System.IO;namespace iTextSharpSample2{ class Program { static void Main(string[] args) { string path = @"C:\devjoker.pdf"; PdfReader reader = new PdfReader(path); FileStream fs = null; PdfStamper stamp = null; Document document = null; try { document = new Document(); string outputPdf = String.Format("C:\\temp\\{0}.pdf", Guid.NewGuid().ToString()); fs = new FileStream( outputPdf, FileMode.CreateNew, FileAccess.Write); stamp = new PdfStamper(reader, fs); BaseFont bf = BaseFont.CreateFont( @"c:\windows\fonts\arial.ttf", BaseFont.CP1252, true); PdfGState gs = new PdfGState(); gs.FillOpacity = 0.35F; gs.StrokeOpacity = 0.35F; for (int nPag = 1; nPag <= reader.NumberOfPages; nPag++) { Rectangle tamPagina = reader.GetPageSizeWithRotation(nPag); PdfContentByte over = stamp.GetOverContent(nPag); over.BeginText(); WriteTextToDocument(bf, tamPagina, over, gs, "www.devjoker.com"); over.EndText(); } } finally { // Garantizamos que aunque falle se cierran los objetos // alternativa:usar using reader.Close(); if (stamp != null) stamp.Close(); if (fs != null) fs.Close(); if (document != null) document.Close(); } Console.WriteLine("Fin de ejecucion"); Console.Read(); } private static void WriteTextToDocument(BaseFont bf, Rectangle tamPagina, PdfContentByte over, PdfGState gs, string texto) { over.SetGState(gs); over.SetRGBColorFill(220, 220, 220); over.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); over.SetFontAndSize(bf, 46); Single anchoDiag = (Single)Math.Sqrt(Math.Pow((tamPagina.Height - 120), 2) + Math.Pow((tamPagina.Width - 60), 2)); Single porc = (Single)100 * (anchoDiag / bf.GetWidthPoint(texto, 46)); over.SetHorizontalScaling(porc); double angPage = (-1) * Math.Atan((tamPagina.Height - 60) / (tamPagina.Width - 60)); over.SetTextMatrix((float)Math.Cos(angPage), (float)Math.Sin(angPage), (float)((-1F) * Math.Sin(angPage)), (float)Math.Cos(angPage), 30F, (float)tamPagina.Height - 60); over.ShowText(texto); } } }
|