2014-10-30 197 views
2

我正在使用Gmanny的Pechkin Pdf庫,它的工作完美。這裏的是我的代碼:如何添加頁眉和頁腳到生成的PDF

private void CreatePdfPechkin(string htmlString, string fileName) 
     { 
      //Transform the HTML into PDF 
      var pechkin = Factory.Create(new GlobalConfig() 
      .SetMargins(new Margins(100, 50, 100, 100)) 
       .SetDocumentTitle("Test document") 
       .SetPaperSize(PaperKind.A4) 
       .SetCopyCount(1) 
          //.SetPaperOrientation(true) 
          // .SetOutputFile("F:/Personal/test.pdf") 

      ); 
      ObjectConfig oc = new ObjectConfig(); 
      oc.Footer.SetLeftText("[page]"); 
      oc.Footer.SetTexts("[page]", "[date]", "[time]"); 
      oc.Header.SetCenterText("TEST HEADER TEST1"); 
      oc.Header.SetHtmlContent("<h1>TEST HEADER V2</h1>"); 
      oc.SetAllowLocalContent(true); 
      //// create converter 
      //IPechkin ipechkin = new SynchronizedPechkin(pechkin); 

      // set it up using fluent notation 
      var pdf = pechkin.Convert(new ObjectConfig() 
         .SetLoadImages(true).SetZoomFactor(1.5) 
         .SetPrintBackground(true) 
         .SetScreenMediaType(true) 
         .SetCreateExternalLinks(true), htmlString); 

      //Return the PDF file 
      Response.Clear(); 
      Response.ClearContent(); 
      Response.ClearHeaders(); 
      Response.ContentType = "application/pdf"; 
      Response.AddHeader("Content-Disposition", string.Format("attachment;filename=test.pdf; size={0}", pdf.Length)); 
      Response.BinaryWrite(pdf); 
      Response.Flush(); 
      Response.End(); 
//   byte[] pdf = new Pechkin.Synchronized.SynchronizedPechkin(
//new Pechkin.GlobalConfig()).Convert(
// new Pechkin.ObjectConfig() 
// .SetLoadImages(true) 
// .SetPrintBackground(true) 
// .SetScreenMediaType(true) 
// .SetCreateExternalLinks(true), htmlString); 
//   using (FileStream file = System.IO.File.Create(@"F:\Pankaj WorkSpace\"+ fileName)) 
//   { 
//    file.Write(pdf, 0, pdf.Length); 
//   } 
     } 

但現在我要添加頁眉,頁腳和頁碼,可有人建議如何做到這一點? 我知道這是可能的通過對象配置我試過了,但它不工作..

回答

9

使用下面的非常基本的例子的頁眉和頁腳爲我工作。

使用Pechkin:

GlobalConfig gc = new GlobalConfig(); 
gc.SetMargins(new Margins(300, 100, 150, 100)) 
    .SetDocumentTitle("Test document") 
    .SetPaperSize(PaperKind.Letter); 
IPechkin pechkin = new SynchronizedPechkin(gc); 

ObjectConfig oc = new ObjectConfig(); 
oc.SetCreateExternalLinks(false); 
oc.SetFallbackEncoding(Encoding.ASCII); 
oc.SetLoadImages(false); 
oc.Footer.SetCenterText("I'm a footer!"); 
oc.Footer.SetLeftText("[page]"); 
oc.Header.SetCenterText("I'm a header!"); 

byte[] result = pechkin.Convert(oc, "<h1>My Website</h1>"); 
System.IO.File.WriteAllBytes(@"c:\pechkinTest.pdf", result); 

我建議你改用Tuespechkin雖然。這是Pechkin的一個活躍分支,其中包含許多錯誤修正。不幸的是在Pechkin ceased since 2013上進行了積極的開發。

使用Tuespechkin:

var document = new HtmlToPdfDocument 
{ 
    GlobalSettings = 
    { 
     ProduceOutline = true, 
     DocumentTitle = "My Website", 
     PaperSize = PaperKind.A4, 
     Margins = 
     { 
      All = 1.375, 
      Unit = Unit.Centimeters 
     } 
    }, 
    Objects = { 
     new ObjectSettings 
     { 
      HtmlText = "<h1>My Website</h1>", 
      HeaderSettings = new HeaderSettings{CenterText = "I'm a header!"}, 
      FooterSettings = new FooterSettings{CenterText = "I'm a footer!", LeftText = "[page]"} 
     } 
    } 
}; 

IPechkin converter = Factory.Create(); 
byte[] result = converter.Convert(document); 
System.IO.File.WriteAllBytes(@"c:\tuespechkinTest.pdf", result); 

你的問題是這樣的。您不需要創建新的第二個ObjectConfig,您需要傳遞之前創建的包含頁眉和頁腳的OC。只需將它們組合如下:

ObjectConfig oc = new ObjectConfig(); 
oc.SetLoadImages(true); 
oc.SetZoomFactor(1.5); 
oc.SetPrintBackground(true); 
oc.SetScreenMediaType(true); 
oc.SetCreateExternalLinks(true); 
oc.Footer.SetLeftText("[page]"); 
oc.Footer.SetCenterText("I'm a footer!"); 
oc.Header.SetCenterText("TEST HEADER TEST1"); 

var result = pechkin.Convert(oc, "<h1>My Website</h1>"); 
System.IO.File.WriteAllBytes(@"c:\pechkinTest.pdf", result); 
+0

正如我已經說過的,我嘗試過,但PDF中沒有頁眉頁腳。所以,你可以請更新你的完整功能 – 2014-11-05 05:10:06

+0

添加一個通用的例子後,我意識到你的代碼中的錯誤,我以前錯過了。查看編輯答案底部的代碼。 – Nicholas 2014-11-05 06:28:47

+0

燁工作的人..謝謝..現在切換到週二皮膚:) – 2014-11-05 06:43:04

相關問題