2014-10-30 156 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

相关问题