3

我正在使用 Gmanny 的 Pechkin 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);
//            }
        }

但是现在我想添加页眉、页脚和页码,有人可以建议怎么做吗?我知道可以通过我尝试过的对象配置但它不起作用..

4

1 回答 1

11

使用以下非常基本的示例的页眉和页脚对我有用。

使用佩奇金:

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);

不过,我建议您切换到Tuesspechkin。这是 Pechkin 的一个活动分支,其中包括许多错误修复。不幸的是,自 2013 年以来,Pechkin 的积极开发就停止了。

使用图斯佩金:

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);
于 2014-11-04T02:47:35.780 回答