Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在所有页面大小中保持页眉在页面顶部的一致性?_C#_Pdf_Header_Itextsharp_Itext - Fatal编程技术网

C# 如何在所有页面大小中保持页眉在页面顶部的一致性?

C# 如何在所有页面大小中保持页眉在页面顶部的一致性?,c#,pdf,header,itextsharp,itext,C#,Pdf,Header,Itextsharp,Itext,我已经设置了页眉(使用iTextSharp库)以及仅为A4页大小设置的页边距(根据其方向)-因为我已将文档的页面大小设置为仅为A4页-因此它仅适用于A4页并显示在顶部。有谁能告诉我,无论用户选择什么样的页面大小,如何使这个页眉在整个页面大小上保持一致?就像A4一样,它应该在每个预定义页面大小的顶部打印页眉,因为当我选择Pagesize.HALFLETTER时,页眉会在超出页面边界时消失。有什么帮助吗 更新问题 我的代码是: using System; using System.Colle

我已经设置了页眉(使用iTextSharp库)以及仅为A4页大小设置的页边距(根据其方向)-因为我已将文档的页面大小设置为仅为A4页-因此它仅适用于A4页并显示在顶部。有谁能告诉我,无论用户选择什么样的页面大小,如何使这个页眉在整个页面大小上保持一致?就像A4一样,它应该在每个预定义页面大小的顶部打印页眉,因为当我选择Pagesize.HALFLETTER时,页眉会在超出页面边界时消失。有什么帮助吗

更新问题

我的代码是:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.exceptions;
using System.Windows.Forms;
using System.IO;
namespace PDFGeneration
{

    public class Events : iTextSharp.text.pdf.PdfPageEventHelper
    {
        private String header;
        private iTextSharp.text.pdf.PdfTemplate total;
        private PdfContentByte cb;
        private PdfTemplate template;


        protected Font footer
        {
            get
            {

                //BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts\\adobe-arabic-regular-1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                BaseColor grey = new BaseColor(128, 128, 128);
                Font font = FontFactory.GetFont("Arial", 10.0f, Font.NORMAL, grey);
                //Font font = new Font(bfArialUniCode, 10.0f, Font.NORMAL, grey);
                return font;
            }
        }
        protected Font watermark
        {
            get
            {

                BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts\\adobe-arabic-regular-1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                BaseColor grey = new BaseColor(128, 128, 128);
                Font font = new Font(bfArialUniCode, 14.0f, Font.BOLDITALIC, grey);
                return font;
            }
        }
        public override void OnStartPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
        {
            PdfPTable headerTbl = new PdfPTable(1);
            headerTbl.TotalWidth = document.PageSize.Width;

            //Center the table on the page
            headerTbl.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
            Paragraph para = new Paragraph(String.Format("Urdu Word Processor                        Date : {0:dd/MMM/yyyy}                  Page Number : {1}", DateTime.Now, writer.PageNumber), footer);

            PdfPCell cell = new PdfPCell(para);


            cell.Border = 0;
            cell.PaddingLeft = 120.0f;
            //cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
            //add cell to table
            headerTbl.AddCell(cell);
            headerTbl.WriteSelectedRows(0, -1, 0, (document.BottomMargin + 790), writer.DirectContent); 

        }
        public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
        {


            //PdfContentByte cb = writer.DirectContent;

            //PdfPTable tablefooter = new PdfPTable(1);
            ////tablefooter.HeaderRows = 3;
            //tablefooter.HorizontalAlignment = 1;
            //float[] widthfooter = new float[] { 1f };
            //tablefooter.SetWidths(widthfooter);

            //tablefooter.SpacingBefore = 100f;

            //PdfPCell headerfooter = new PdfPCell(new Phrase(String.Format("Urdu Word Processor                 Date : {0:dd/MMM/yyyy}                  Page Number : {1}", DateTime.Now, writer.PageNumber), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
            ////Phrase headerfooter = new Phrase(new Phrase("Urdu Word Processor                            " + "Date: " + DateTime.Now.ToString("dd-MMM-yyyy-ss") + "                            " + "Page: " + writer.PageNumber, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
            //headerfooter.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
            //headerfooter.Border = iTextSharp.text.Rectangle.TOP_BORDER;

            //tablefooter.AddCell(headerfooter);
            ////document.Add(tablefooter);
            //ColumnText ct = new ColumnText(cb);
            //ct.SetSimpleColumn(new Phrase(new Chunk("بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ       ", FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.NORMAL))),
            //                   46, 190, 530, 36, 25, Element.ALIGN_LEFT | Element.ALIGN_TOP);
            //ct.Go();

            // water mark

            //ColumnText.ShowTextAligned(writer.DirectContentUnder,Element.ALIGN_CENTER,
            //    new Phrase("بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ", watermark),
            //    297.5f, 421, ((writer.PageNumber % 2) == 1) ? 45 : -45); // have made a loop so to have multiple orientations.
            ColumnText col = new ColumnText(writer.DirectContentUnder);
            col.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
            col.SetSimpleColumn(220, 810, 569, 36);
            col.AddElement(new Paragraph("بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ",watermark));
            col.Go();

        }
        //public void setHeader(String header)
        //{
        //    this.header = header;
        //}
        //public void onOpenDocument(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
        //{
        //    total = writer.DirectContent.CreateTemplate(30,16);
        //}

        //public void onEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document) 
        //{
        //    iTextSharp.text.pdf.PdfPTable table = new PdfPTable(1);
        //        try 
        //        {
        //                //table.SetWidths(new int[]{24, 24, 2});
        //                table.SetTotalWidth(new float[] {527.0f,527.0f,527.0f});
        //                //table.LockedWidth = true;
        //                table.DefaultCell.FixedHeight = 20;
        //                table.DefaultCell.Border = iTextSharp.text.Rectangle.ALIGN_BOTTOM;
        //                table.AddCell(header);
        //                table.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
        //                table.AddCell( String.Format("Page:{0} of", writer.PageNumber));
        //               // MessageBox.Show(String.Format("Page:{0} of", writer.PageNumber));
        //                iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(iTextSharp.text.Image.GetInstance(total)); 
        //                cell.Border = iTextSharp.text.Rectangle.ALIGN_BOTTOM;
        //                table.AddCell(cell);
        //                table.WriteSelectedRows(0, -1,
        //                34, 803, writer.DirectContent);
        //        }
        //        catch(Exception de) 
        //        {
        //           // MessageBox.Show(de.Message.ToString());
        //        }
        //}
        //public void onCloseDocument(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document) 
        //{
        //    String pageNumber = (writer.PageNumber).ToString();
        //    pageNumber += " of " + (writer.PageNumber).ToString();
        //    iTextSharp.text.pdf.ColumnText.ShowTextAligned(writer.DirectContent, iTextSharp.text.Element.ALIGN_RIGHT, new iTextSharp.text.Phrase(pageNumber), 580, 10, -90);

        //}



        //override the OnPageEnd event handler to add our footer
        //public override void OnOpenDocument(PdfWriter writer, Document document)
        //{
        //    cb = writer.DirectContent;
        //    template = cb.CreateTemplate(50, 50);
        //}

        //public override void OnEndPage(PdfWriter writer, Document document)
        //{
        //    base.OnEndPage(writer, document);

        //    int pageN = writer.PageNumber;
        //    String text = "Page " + pageN.ToString() + " of ";

        //    //float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size);

        //    iTextSharp.text.Rectangle pageSize = document.PageSize;

        //    cb.SetRGBColorFill(100, 100, 100);

        //    cb.BeginText();
        //    //cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
        //    cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
        //    cb.ShowText(text);

        //    cb.EndText();

        //    cb.AddTemplate(template, document.LeftMargin + 10.0f, pageSize.GetBottom(document.BottomMargin));
        //}

        //public override void OnCloseDocument(PdfWriter writer, Document document)
        //{
        //    base.OnCloseDocument(writer, document);
        //    BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts\\adobe-arabic-regular-1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        //    template.BeginText();
        //    template.SetFontAndSize(bfArialUniCode, 14.0f);
        //    template.SetTextMatrix(0, 0);
        //    template.ShowText("" + (writer.PageNumber - 1));
        //    template.EndText();
        //}



    }// end of class
}
此代码仅在A4页面大小的顶部打印此输出,这应该是因为其硬编码值位于此代码中以对齐文本

乌尔都语文字处理器日期:2013年2月17日页码:1
如何确定方向,使其适用于所有页面大小?

在绘制页眉时,以一定的协调度进行绘制。用当前页面裁剪或媒体框计算的坐标替换硬编码坐标。你能给我举个例子吗?我不熟悉媒体或这方面的裁剪?或者您必须先获取页面大小,然后相应地设置标题的方向?如果是这样,那么如何获得预定义页面大小的宽度和高度?如果您提供了用于创建页眉的中心代码,则更容易显示要更改的内容。
                         بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ