2013-06-13 15 views

risposta

18

Può darsi, tuttavia, che non si vuole creare sempre un PDF con le dimensioni e margini di default, in modo iTextSharp fornisce modi per personalizzare queste impostazioni. Ci sono 2 ulteriori costruttori all'oggetto Document:

public Document(iTextSharp.text.Rectangle pageSize); 
    public Document(iTextSharp.text.Rectangle pageSize, float, float, float, float); 

Il primo può essere usato così:

var doc = new Document (PageSize.A5);

La classe PageSize contiene un numero di oggetti Rectangle che rappresentano i formati di carta più comuni da A0 a A10, da B0 a B10, LEGAL, LEDGER, LETTER, POSTCARD, TABLOID e così via. Se si desidera applicare un formato personalizzato che non è disponibile all'interno della classe PageSize, si definisce il proprio oggetto Rectangle, impostare le sue proprietà e passa che al costruttore come argomento:

var doc = new Document(new Rectangle(100f, 300f)); 
PdfWriter.GetInstance(doc, new FileStream(path + "/Doc2.pdf", FileMode.Create)); 
doc.Open(); 
    doc.Add(new Paragraph("This is a custom size")); 
doc.Close(); 
+0

È possibile ottenere dettagli da http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp –

+0

Grazie amico, Funziona per me :) – Senps

+5

Per riferimento, ci sono 72 punti per il pollice. –

1

Di seguito il codice dimostrerà come per implementare il PDF personalizzato utilizzando le coordinate PDF in C# .net. Per questo compito è necessario conoscere le coordinate pdf.

BaseFont f_cn; 
    string poath = Server.MapPath("~/fonts/CALIBRI.TTF"); 

f_cn = BaseFont.CreateFont(poath, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 

using (System.IO.FileStream fs = new FileStream(Server.MapPath("~/TempPdf") + "\\" + "download.pdf", FileMode.Create)) 
      { 
Document document = new Document(PageSize.A4, 25, 25, 30, 30); 
       PdfWriter writer = PdfWriter.GetInstance(document, fs);     
       Paragraph p = new Paragraph(); 
       // Add meta information to the document 
       document.AddAuthor("Mikael Blomquist"); 
       document.AddCreator("Sample application using iTestSharp"); 
       document.AddKeywords("PDF tutorial education"); 
       document.AddSubject("Document subject - Describing the steps creating a PDF document"); 
       document.AddTitle("The document title - Amplified Resource Group"); 
       // Open the document to enable you to write to the document 
       document.Open(); 
       // Makes it possible to add text to a specific place in the document using 
       // a X & Y placement syntax. 
       PdfContentByte cb = writer.DirectContent; 
       cb.SetFontAndSize(f_cb, 16); 
       // First we must activate writing 
       cb.BeginText(); 
       // Add an image to a fixed position from disk 
       iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/arg.png")); 
       png.SetAbsolutePosition(200, 738); 
       cb.AddImage(png); 
       writeText(cb, "Header", 30, 718, f_cb, 14); 
} 
private void writeText(PdfContentByte cb, string Text, int X, int Y, BaseFont font, int Size) 
    { 
     cb.SetFontAndSize(font, Size); 
     cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Text, X, Y, 0); 
    }