2012-10-30 52 views
2

問題是一個頭文件,我必須在abcpdf生成的pdf文件的每個頁面上包含這個頭文件。ABCPDF計算標題大小和位置。

頭文件包含多個圖像文件和幾行文本,這些文本因案例而異。

問題是我不知道如何計算標題的大小。我需要使用它的大小來分配矩形位置,以便將每個頁面上的其餘html文件與標題一起放置。我正在使用C#。

回答

2

首先,您需要在頂部創建足夠空間的文檔,以允許添加標題。以下設置適用於標題大約爲頁面1/5的標準A4文檔。記得在一個PDF的座標是從右下角不左上角..

//Setting to create the document using ABCPdf 8 
var theDoc = new Doc(); 
theDoc.MediaBox.String = "A4"; 

theDoc.HtmlOptions.PageCacheEnabled = false; 
theDoc.HtmlOptions.ImageQuality = 101; 
theDoc.Rect.Width = 719; 
theDoc.Rect.Height = 590; 
theDoc.Rect.Position(2, 70); 
theDoc.HtmlOptions.Engine = EngineType.Gecko; 

下面出來的代碼放在一個頭在每個頁面上的文件,一個頭的圖像,然後用彩色框圖像與下一些自定義文本英寸

在這種情況下,標題圖像是1710 x 381,以保持圖像的分辨率儘可能高,以防止打印時看起來模糊。

private static Doc AddHeader(Doc theDoc) 
{ 
    int theCount = theDoc.PageCount; 
    int i = 0; 

    //Image header 
    for (i = 1; i <= theCount; i++) 
    { 
     theDoc.Rect.Width = 590; 
     theDoc.Rect.Height = 140; 
     theDoc.Rect.Position(0, 706); 

     theDoc.PageNumber = i; 
     string imagefilePath = HttpContext.Current.Server.MapPath("/images/pdf/pdf-header.png"); 

     Bitmap myBmp = (Bitmap)Bitmap.FromFile(imagefilePath); 
     theDoc.AddImage(myBmp); 
    } 

    //Blue header box 
    for (i = 2; i <= theCount; i++) 
    { 
     theDoc.Rect.String = "20 15 590 50"; 
     theDoc.Rect.Position(13, 672); 
     System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#468DCB"); 
     theDoc.Color.Color = c; 
     theDoc.PageNumber = i; 
     theDoc.FillRect(); 
    } 

    //Blue header text 
    for (i = 2; i <= theCount; i++) 
    { 
     theDoc.Rect.String = "20 15 586 50"; 
     theDoc.Rect.Position(25, 660); 
     System.Drawing.Color cText = System.Drawing.ColorTranslator.FromHtml("#ffffff"); 
     theDoc.Color.Color = cText; 
     string theFont = "Century Gothic"; 
     theDoc.Font = theDoc.AddFont(theFont); 
     theDoc.FontSize = 14; 
     theDoc.PageNumber = i; 
     theDoc.AddText("Your Text Here"); 
    } 
    return theDoc; 
}