itextsharp添加页脚文本,但其部分可见(C#.NET)

itextsharp添加页脚文本,但其部分可见(C#.NET),c#,itextsharp,C#,Itextsharp,我一直在使用itextsharp SDK,我有一个包含一些数据的pdf文件,我想添加页眉或页脚文本,它正在添加,但在页面的最后,页眉/页脚文本部分可见 请参见示例代码: PdfReader reader = new PdfReader("C:\Source pdf file.pdf"); string f_textPrefix = "Page No : 1"; using (MemoryStream memoryStream = new MemoryStream()) { PdfStam

我一直在使用itextsharp SDK,我有一个包含一些数据的pdf文件,我想添加页眉或页脚文本,它正在添加,但在页面的最后,页眉/页脚文本部分可见

请参见示例代码:

PdfReader reader = new PdfReader("C:\Source pdf file.pdf");
string f_textPrefix = "Page No : 1";
using (MemoryStream memoryStream = new MemoryStream())
{
    PdfStamper pdfStamper = new PdfStamper(reader, memoryStream);
    bool flag = false;
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(i);
        PdfContentByte pdfPageContents = pdfStamper.GetOverContent(i);
        pdfPageContents.BeginText();
        BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD,
                                                            BaseFont.CP1252,
                                                            false);                        
        System.Drawing.Color m_Color = new System.Drawing.Color();
        pdfPageContents.SetFontAndSize(baseFont, Convert.ToInt32(p_objParam.pdfBatesDetail.FontSize));
        pdfPageContents.SetRGBColorFill(m_Color.R, m_Color.G,
                                                    m_Color.B);
        // this is to set the cursor  tp Bottom Middle 
        int yPos = 0;
        yPos = PDFLayout.BottomHeight;
        pdfPageContents.SetTextMatrix(pageSize.Width / 2, yPos);

        pdfPageContents.ShowText(f_textPrefix.Trim());

        pdfPageContents.EndText();
    }
    pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened. 
    pdfStamper.Close();              // Always close the stamper or you'll have a 0 byte stream. 
    byte []bt = btmemoryStream.ToArray();

    File.WriteAllBytes("C:\new_file.pdf", bt);
}
PdfReader=newpdfReader(“C:\Source pdf file.pdf”);
字符串f_textPrefix=“第1页”;
使用(MemoryStream MemoryStream=new MemoryStream())
{
PdfStamper PdfStamper=新的PdfStamper(读卡器、内存流);
布尔标志=假;

对于(int i=1;i您选择使用iText API的一个非常低级的部分来绘制文本,该部分基本上表示PDF中使用的精确文本绘制说明

这些绘图说明不使用您提供的y坐标作为文本底部,而是作为基线:

标志符号原点所在的线是基线

因此,如果使用页面底部的y坐标进行文本绘制,基线下方的图示符部分将被切断


为了防止出现这种情况,您可以查询
BaseFont
实例,了解要绘制的字符串中的下限框可以使用多少

/**
 * Gets the descent of a <CODE>String</CODE> in points. The descent will always be
 * less than or equal to zero even if all the characters have an higher descent.
 * @param text the <CODE>String</CODE> to get the descent of
 * @param fontSize the size of the font
 * @return the dexcent in points
 */
virtual public float GetDescentPoint(String text, float fontSize) 
从y坐标中减去这个值

或者您可以使用更高级别的API替代方案,其中一些允许您定义一个框,在其中绘制文本



另一个问题可能是,您假设页面底部位于坐标0处。虽然通常情况下是这样,但不一定如此。因此,您可能希望考虑相关页面裁剪框的底部。

您选择使用iText API中非常低级的部分来绘制文本,该部分是Sentily表示PDF中使用的精确文本绘图说明

这些绘图说明不使用您提供的y坐标作为文本底部,而是作为基线:

标志符号原点所在的线是基线

因此,如果使用页面底部的y坐标进行文本绘制,基线下方的图示符部分将被切断


为了防止出现这种情况,您可以查询
BaseFont
实例,了解要绘制的字符串中的下限框可以使用多少

/**
 * Gets the descent of a <CODE>String</CODE> in points. The descent will always be
 * less than or equal to zero even if all the characters have an higher descent.
 * @param text the <CODE>String</CODE> to get the descent of
 * @param fontSize the size of the font
 * @return the dexcent in points
 */
virtual public float GetDescentPoint(String text, float fontSize) 
从y坐标中减去这个值

或者您可以使用更高级别的API替代方案,其中一些允许您定义一个框,在其中绘制文本



另一个问题可能是,您假设页面底部位于坐标0处。虽然通常情况下是这样,但不一定如此。因此,您可能需要考虑相关页面裁剪框的底部。

因此,首先,我不太清楚它,我只使用了一次,我记得r也有同样的问题。以下是对我有效的方法,也许这也能帮助你:

我有以下扩展类:

public class ITextSharpExtension
{
//Add pagination
public class PageEventHelper : PdfPageEventHelper
{
    PdfContentByte cb;
    PdfTemplate template;


    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        cb = writer.DirectContent;
        template = cb.CreateTemplate(50, 50);
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        Font arial = FontFactory.GetFont("Arial", 10, GrayColor.GRAY);
        base.OnEndPage(writer, document);
        int pageN = writer.PageNumber;
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        string text = String.Format("Page {0} of ", pageN.ToString());
        float len = bf.GetWidthPoint(text, 8);

        iTextSharp.text.Rectangle pageSize = document.PageSize;

        cb.SetRGBColorFill(100, 100, 100);

        cb.BeginText();
        cb.SetFontAndSize(bf, 10);
        cb.SetTextMatrix(10, pageSize.GetBottom(10));
        cb.ShowText(text);

        cb.EndText();

        cb.AddTemplate(template, 60, pageSize.GetBottom(10));
    }

    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
        base.OnCloseDocument(writer, document);
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        template.BeginText();
        template.SetFontAndSize(bf, 10);
        template.SetTextMatrix(0, 0);
        template.ShowText("" + (writer.PageNumber - 1));
        template.EndText();
    }
}
}

这一行是在底部添加边距的地方:
cb.AddTemplate(template,60,pageSize.GetBottom(10));
特别是这部分:
pageSize.GetBottom(10)


就像我说的,我不确定这是最好的方法,我猜这比你要求的要多(我猜你可以知道你需要哪一部分),但这对我来说很有效,所以请不要投反对票:)

所以,首先,我不知道它是什么,我只使用过一次,我记得有同样的问题。以下是对我有用的东西,也许这也能帮助你:

我有以下扩展类:

public class ITextSharpExtension
{
//Add pagination
public class PageEventHelper : PdfPageEventHelper
{
    PdfContentByte cb;
    PdfTemplate template;


    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        cb = writer.DirectContent;
        template = cb.CreateTemplate(50, 50);
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        Font arial = FontFactory.GetFont("Arial", 10, GrayColor.GRAY);
        base.OnEndPage(writer, document);
        int pageN = writer.PageNumber;
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        string text = String.Format("Page {0} of ", pageN.ToString());
        float len = bf.GetWidthPoint(text, 8);

        iTextSharp.text.Rectangle pageSize = document.PageSize;

        cb.SetRGBColorFill(100, 100, 100);

        cb.BeginText();
        cb.SetFontAndSize(bf, 10);
        cb.SetTextMatrix(10, pageSize.GetBottom(10));
        cb.ShowText(text);

        cb.EndText();

        cb.AddTemplate(template, 60, pageSize.GetBottom(10));
    }

    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
        base.OnCloseDocument(writer, document);
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        template.BeginText();
        template.SetFontAndSize(bf, 10);
        template.SetTextMatrix(0, 0);
        template.ShowText("" + (writer.PageNumber - 1));
        template.EndText();
    }
}
}

这一行是在底部添加边距的地方:
cb.AddTemplate(模板,60,pageSize.GetBottom(10))特别是这部分:
pageSize.GetBottom(10)


就像我说的,我不确定这是最好的方法,我猜这比你要求的要多(我猜你可以知道你需要哪一部分),但这对我很有效,所以请不要投反对票:)

PDFLayout.BottomHeight=0 p_objParam.pdfBatesDetail.FontSize=5.0;这些只是用户定义的对象有一些价值,请考虑这些值我不理解你的问题。选择y坐标。如果你选择的值太低以至于文本被截断,为什么不选择一个更大的值呢?你可以在页面底部添加一个边距,但我认为你必须构建一个模板才能做到这一点(我就是这样做的)@AlanVrog有任何答案可以回答你的问题吗?PDFLayout.BottomHeight=0 p_objParam.pdfBatesDetail.FontSize=5.0;这些只是用户定义的对象有一些价值,请考虑这些值我不理解你的问题。选择y坐标。如果你选择的值太低以至于文本被截断,为什么不选择一个更大的值呢?你可以在页面底部添加一个边距,但我认为你必须构建一个模板才能做到这一点(我就是这样做的)@AlanVrog有任何答案可以回答你的问题吗?