jul 3 2009

Crea Tus Propios PDF

Category: Artículos | Web | Windowsjayson_quintero @ 07:33

Genera tus propios PDF

en uno de mis ultimos trabajos, estuve creando PDF al vuelo, en Asp.Net, consegui una
libreria bastante buena, aunque un poco dificil de entender al comienzo. Necesitaras
hacer referencia a la Dll (PdfSharp.dll)...

Voy a agregar algunos codigos, para que te sirvan de guia a la hora de crear tu PDF.

El Objetivo de este codigo es solo de guia,

Si tienes alguna pregunta no dudes es dejarnos un mensaje.

001public PdfDocument document;
002private PdfPage PagePDF;
003private XGraphics gfx;
004
005public XFont font()
006{
007    return new XFont("Tahoma", 9, XFontStyle.Regular);
008}
009
010public void NewDocument(string filename)
011{
012    document = new PdfDocument();
013    PagePDF = document.AddPage();
014    gfx = XGraphics.FromPdfPage(PagePDF);
015}
016 
017public void SetSecurity(string password)
018{
019    PdfSecuritySettings securitySettings = document.SecuritySettings;
020    securitySettings.UserPassword = password;
021    securitySettings.OwnerPassword = password;
022    // Restrict some rights.
023    securitySettings.PermitAccessibilityExtractContent = false;
024    securitySettings.PermitAnnotations = false;
025    securitySettings.PermitAssembleDocument = false;
026    securitySettings.PermitExtractContent = false;
027    securitySettings.PermitFormsFill = true;
028    securitySettings.PermitFullQualityPrint = false;
029    securitySettings.PermitModifyDocument = true;
030    securitySettings.PermitPrint = false;
031}
032
033public void SetPicture(string path, int xPosition, int yPosition)
034{
035    gfx.DrawImage(XImage.FromFile(path), xPosition, yPosition);
036}
037        
038public void SetText(string texto, XFont font, int MargenX, int Linea)
039{
040    gfx.DrawString(texto, font, XBrushes.Black, MargenX, Linea, XStringFormats.TopLeft);
041}
042
043public void PrintColumnsHeader(ref int positionY, XFont font, int dimension)
044{
045    XRect rect;
046    XPen pen;
047    int posr = 50;
048    int post = 55;
049    if ((positionY + 75) > 460)
050    {
051        int casoa = 1;
052        foreach (string s in ListaPiePagina)
053        {
054            PrintPiePagina(s, ePagina.Horizontal, ref positionY, casoa);
055            casoa += 1;
056        }
057        positionY = 70;
058        SaltoDePaginaHorizontal(ref positionY);
059    }
060    foreach (Headers h in ListaH)
061    {
062        rect = new XRect(posr, positionY, h.with, 15);
063        pen = new XPen(XColors.Silver, 1);
064        gfx.DrawRectangle(pen, XBrushes.Navy, rect);
065        gfx.DrawString(h.nombre, font, XBrushes.White, post, positionY, XStringFormats.TopLeft);
066        post += h.with;
067        posr += h.with;
068    }
069    positionY += 15;
070   
071}
072
073private void SaltoDePaginaHorizontal(ref int PosY)
074{
075    
076    PagePDF = document.AddPage();
077    PagePDF.Orientation = PdfSharp.PageOrientation.Landscape;
078    gfx = XGraphics.FromPdfPage(PagePDF);
079    PrintLogo(Alinea, ePagina.Horizontal);
080    PosY += 5;
081    PrintLine(titulo, "", ref PosY, fontTitle(), 150, WebTracer.UtilCode.objPdfFile.eLineAlignment.CenterTitle);
082    PosY += 15;
083    NumPaginas += 1;
084}
085
086public void PrintLine(string strString, string strvalue, ref int positionY, XFont font, int dimencion, eLineAlignment Alignment)
087{
088    if (string.IsNullOrEmpty(strString))
089    {
090        strString = "";
091    }
092    char[] c = { ' ' };
093    string[] arrayline = strString.Split(c);
094    string strLine = "";
095    int nLine = 1;
096    for (int i = 0; i <= arrayline.Length - 1; i++)
097    {
098        if (i == arrayline.Length - 1)
099        {
100            if (strLine != "")
101            {
102                strLine += arrayline[i].ToString();
103                Print(Alignment, font, ref positionY, strLine, strvalue, ref nLine);
104                strLine = "";
105            }
106            else
107            {
108                strLine = arrayline[i].ToString();
109                Print(Alignment, font, ref positionY, strLine, strvalue, ref nLine);
110                strLine = "";
111            }
112        }
113        else
114        {
115            if (strLine.Length < dimencion)
116            {
117                strLine += arrayline[i].ToString() + " ";
118            }
119            else
120            {
121                Print(Alignment, font, ref positionY, strLine, strvalue, ref nLine);
122                strLine = "";
123                strLine += arrayline[i].ToString() + " ";
124            }
125        }
126    }
127}
128
129public bool SaveDocument(string path, string filename)
130{
131    try
132    {
133        document.Save(path + filename);
134        return true;
135    }
136    catch
137    {
138        return false;
139    }
140}

### Bajar el DLL de este link ###

PdfSharp.dll (524.00 kb)

Tags:

Actualmente calificado con 5.0 por 2 personas

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comentarios