设置页边距,页眉和页脚为PDF无重叠 [英] Setting margins, headers and footers for PDF without overlap

查看:229
本文介绍了设置页边距,页眉和页脚为PDF无重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助我调整PDF页眉/页脚我旁边文本区域。第一页看起来不错,它只是得到的情况更糟糕。在页眉和页脚蚕食现有的利润率空间?

我想知道是怎么回事错的,什么我可以调整设置如下:


  1. 页面宽度

  2. 页边距宽度

  3. 标题

  4. 页脚

  5. 文本区域

我的头倍率功能如下:

 公共部分类标题:PdfPageEventHelper
{
公共覆盖无效的OnStartPage(PdfWriter作家,文档DOC)
    {
    PdfPTable headerTbl =新PdfPTable(2);
    headerTbl.SetWidths(新浮法[] {4,1});
    headerTbl.TotalWidth = doc.PageSize.Width;    iTextSharp.text.Image标志= iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(〜/图片/ view.gif));
    logo.ScalePercent(5);
    PdfPCell电池=新PdfPCell(标识);
    cell.Horizo​​ntalAlignment = Element.ALIGN_RIGHT;
    cell.PaddingRight = 20;
    cell.Border = Rectangle.NO_BORDER;
    字体timesH =新Font(Font.FontFamily.TIMES_ROMAN,20);
    字体次=新Font(Font.FontFamily.TIMES_ROMAN,10);
    块C1 =新的块(这是我头文本,timesH);
    大块C =新的块(\\ n次);
    块C2 =新的块(请HAVE A NICE DAY,次);
    短语P =新词();
    p.Add(C1);
    p.Add(C);
    p.Add(C2);
    PdfPCell CELL2 =新PdfPCell(对);
    cell2.Border = Rectangle.NO_BORDER;    headerTbl.AddCell(CELL 2);
    headerTbl.AddCell(细胞);    headerTbl.WriteSelectedRows(0,-1,0,(doc.PageSize.Height - 10),writer.DirectContent);    }
}

stringWrite是包含一串数据的StringWriter。更清晰的HERE.

我创建PDF如下:

  StringReader SR =新StringReader(stringWrite.ToString());
    文档pdfDoc =新的文件(新的Rectangle(288F,144F),10F,10F,30F,30F);
    pdfDoc.SetPageSize(PageSize.A4.Rotate());
    HTMLWorker的HTMLParser =新HTMLWorker(pdfDoc);
    PdfWriter pdfwriter = PdfWriter.GetInstance(pdfDoc,HttpContext.Current.Response.OutputStream);
    pdfwriter.PageEvent =新页脚();
    pdfwriter.PageEvent =新标题();
    pdfDoc.Open();
    htmlparser.Parse(SR);
    pdfDoc.Close();
    HttpContext.Current.Response.Write(pdfDoc);
    HttpContext.Current.Response.End();

我使用iTextSharp的,C#,Asp.net在我的申请。


解决方案

您初始化文件的创建与

 文档pdfDoc =新的文件(新的Rectangle(288F,144F),10F,10F,30F,30F);

这尤其意味着你预留10个单位的左,右和30个单位的顶部和底部作为保证金。剩余的内部空间的整体可以由自动内容布局的机制被使用。

标题物质,因此,必须要在边缘区绘制,否则很可能与页面内容重叠。

您code,另一方面,创建具有两行的段落,在一个20单元的字体的第一组,第二个在10个单位的字体,并把它封装在表中。因此,该表​​具有大于30个单位(这两条线的组合高度加上一些行间空间和可能的一些表格单元余量开销)的高度。然后,它吸引像这样

  headerTbl.WriteSelectedRows(0,-1,0,(doc.PageSize.Height  -  10),writer.DirectContent);

所以它开始加入10单位的页面的顶部的下方。您定义的页面上边距为仅仅是30,虽然。因此,头和​​条带中的页面内容重叠超过10台高。

因此​​,我建议您通过增加上边距20(的十余的加的有些距离为它的外观的):

 文档pdfDoc =新的文件(新的Rectangle(288F,144F),10F,10F,50F,30F);

为什么第一页看着没事了最有可能的是,你的HTML一些空的空间(至少尽可能的 HTMLWorker 而言)。

补充说明:


  • 添加在的OnStartPage 内容气馁。您应该使用的OnEndPage 为内容的所有此类操作,页眉,页脚,背景图片,...


  • HTMLWorker 是德precated。您应该使用 XMLWorker


  • 有一个原因,你为什么不从一开始就设定的最终页面大小(新文件),而是分开?


I need some help with tweaking my PDF header/footer alongside my text areas. The first page looks okay and it just gets worse from there. Is the header and footer eating into my existing margin spaces?

I would like to know what is going wrong and what I can tweak to set the below:

  1. Page widths
  2. Margin widths
  3. Header
  4. Footer
  5. Text area

My header override function is as below:

public partial class Header : PdfPageEventHelper
{
public override void OnStartPage(PdfWriter writer, Document doc)
    {
    PdfPTable headerTbl = new PdfPTable(2);
    headerTbl.SetWidths(new float[] { 4, 1 });
    headerTbl.TotalWidth = doc.PageSize.Width;

    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/view.gif"));
    logo.ScalePercent(5);
    PdfPCell cell = new PdfPCell(logo);
    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
    cell.PaddingRight = 20;
    cell.Border = Rectangle.NO_BORDER;


    Font timesH = new Font(Font.FontFamily.TIMES_ROMAN, 20);
    Font times = new Font(Font.FontFamily.TIMES_ROMAN, 10);
    Chunk c1= new Chunk("THIS IS MY HEADER TEXT", timesH);
    Chunk c = new Chunk("\n", times);
    Chunk c2=new Chunk("PLEASE HAVE A NICE DAY", times);
    Phrase p = new Phrase();
    p.Add(c1);
    p.Add(c);
    p.Add(c2);
    PdfPCell cell2 = new PdfPCell(p);
    cell2.Border = Rectangle.NO_BORDER;

    headerTbl.AddCell(cell2);
    headerTbl.AddCell(cell);

    headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);

    }
}

stringWrite is a StringWriter that contains a bunch of data. More clarity HERE.

I create the pdf as follows:

    StringReader sr = new StringReader(stringWrite.ToString());
    Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 30f, 30f);
    pdfDoc.SetPageSize(PageSize.A4.Rotate());
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter pdfwriter = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
    pdfwriter.PageEvent = new Footer();
    pdfwriter.PageEvent = new Header();
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    HttpContext.Current.Response.Write(pdfDoc);
    HttpContext.Current.Response.End();

I'm using iTextSharp, C#, Asp.net in my application.

解决方案

You initialize the document creation with

Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 30f, 30f);

This means especially that you reserve 10 units both left and right and 30 units both top and bottom as margin. The whole remaining inner space can be used by the automatic content layout mechanisms.

Header material, therefore, has to be drawn in that margin area, otherwise it may overlap with page content.

Your code, on the other hand, creates a paragraph with two lines, the first one set in a 20 unit font, the second one in a 10 unit font, and wraps it in a table. Thus, this table has a height of more than 30 units (the combined height of those two lines plus some inter-line space and possibly some table cell margin overhead). Then it draws it like this

headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);

So it starts 10 units beneath the top of the page. You defined the top page margin to be merely 30, though. Thus, the header and the page content overlap in a stripe more than 10 units high.

Thus, I would propose you increase the top margin by 20 (more than ten plus some distance for the looks of it):

Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 50f, 30f);

The reason why the first page looked all right most likely is that your HTML starts with some empty space (at least as far as the HTMLWorker is concerned).

Additional remarks:

  • Adding content in OnStartPage is discouraged. You should use OnEndPage for all such manipulations of the content, headers, footers, background images, ...

  • HTMLWorker is deprecated. You should use XMLWorker.

  • Is there a reason why you don't set the final page size from the start (in new Document) but instead separately?

这篇关于设置页边距,页眉和页脚为PDF无重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆