使用iTextSharp将我的段落设置为从上边距开始

时间:2016-09-01 13:36:32

标签: c# itext

我正在尝试构建pdf来数字化我公司的报告系统。我使用过iTextSharp,到目前为止它看起来很棒,但我的边缘似乎没有正常工作。我已经将边距设置为配置文件,并且左右边距效果很好,但无论顶部和底部边距如何,我的段落似乎都会在页面下开始约30%。这是我正在使用的代码:

public int PrintPdf()
    {
        //Getting the path
        //Path.GetFileNameWithoutExtension("Test_Doc_Print") + ".pdf");
        object OutputFileName = this._path;

        //Making the PDF Doc
        iTextSharp.text.Document PDFReport = new iTextSharp.text.Document
        (
                PageSize.A4.Rotate(),
                /*this._left,
                this._right,
                this._top,
                this._bottom*/
                10,
                10,
                10,
                10
        );
        //Setting The Font
        string fontpath = @"C:\Windows\Fonts\";
        BaseFont monoFont = BaseFont.CreateFont(fontpath + "Consola.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
        Font fontPDF = new Font(monoFont, this._fontSize);

        // create file stream for writing the PDF
        FileStream fs = new FileStream(this._path, FileMode.Create, FileAccess.ReadWrite);
        //FileStream fs = new FileStream(@"c:\\Reportlocation", FileMode.Create);

        // Create an FCFC scan object to convert TextToPrint page at a time
        FCFCScanner page = new FCFCScanner(this._text);

        // Create PDF writer and associate with file stream
        //iTextSharp.text.pdf.PdfWriter writer = new iTextSharp.text.pdf.PdfWriter.GetInstance(PDFReport, fs);
        PdfWriter writer = PdfWriter.GetInstance(PDFReport, fs);

        //Opening the PDF Doc
        PDFReport.Open();

        //Load each page from the string into the PDF
        page.NextPage();
        do
        {
            if (page.PageLength > 0)
            {
                Paragraph prg = new Paragraph(page.Page, fontPDF);
                PDFReport.Add(prg);
                PDFReport.NewPage();
                page.NextPage();
            }
        } while (page.MorePages);

        PDFReport.Close();
        return (0);
    }

我已将边距设置为10(现在硬编码)以显示我正在使用的内容。该程序应该读取我从StringBuider类发送的字符串。 它旨在一次接收一页文本并将其转换为PDF文档。

好的,问题是:当构建页面时,第一段不会从上边距开始。如果我减少保证金,它不会将段落移动到新的保证金。这使得我的PDF文件更长,因为在打印机页面上轻松放置的文本需要两个PDF页面。任何帮助让我的段落只是从顶部边缘开始将非常感激。

我对编程比较陌生,这是我的第一篇文章,所以如果您需要更多信息,请告诉我,我会添加更多信息。

0 个答案:

没有答案
相关问题