PDF Itextsharp打印在打印输出的页面右下角添加边距

时间:2017-10-31 15:33:06

标签: c# pdf printing itext

我有一个PDF导出函数(ality),当在页面/视图上呈现结果时,我有以下输出

Output on Webpage

如您所见,表格的内容在页面中心对齐,一切看起来都很完美。但是在打印时,内容(表格)会略微收缩,输出会略微向左轻微推动,这意味着页面右侧和底部会有额外的空间。

Output on printer

这种扭曲的输出在纸张打印机和文档刻录机打印输出上都显示如下。我无法确定导致这种情况的原因,因为代码中的一切似乎都很好。相同的代码用于创建和输出PDF文件,所有内容都显示为完美居中。从网页打印(第一张图片)是问题所在。这是一个CSS问题还是XMLworker问题?

我的导出方法如下(无关代码已修剪)

    public ActionResult ExportFileToPrint()
    { 
        var printList = conStrings.GetReport(search);     

        string webgridstyle = style.GetCss(); //Pdf css

        /*Grid with records data (rows)*/
        string gridHtml = style.GetGrid(printList);

        string exportData = String.Format
            ("<html>" +
            "<head>{0}</head>" +
            "<body>" +
            "<table><tr><td></td></tr></table>" +
            "{1}</body>" +
            "</html>",
            "<style>" + webgridstyle + "</style>", gridHtml);

        var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
        using (var input = new MemoryStream(bytes))
        {
            //Create a System.IO.FileStream object:
            var output = new MemoryStream();
            var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
            //Create a iTextSharp.text.pdf.PdfWriter object, to write the Document to the Specified FileStream:
            var writer = PdfWriter.GetInstance(document, output);

            writer.CloseStream = false;

            //Page event handler to insert TIMESTAMP and PAGE X of Y
            writer.PageEvent = new PdfHeaderFooter();

            //Parse to PDF (open document for writing)
            document.Open();

            //document header Table that only appears on first page
            PdfPTable tables = style.GetHeaderTable();
            //tables.HorizontalAlignment = Element.ALIGN_CENTER; //
            //CENTER CONTENTS HERE??
            tables.HorizontalAlignment = 0;
            //tables.
            //Add table to document                                    
            document.Add(tables);

            var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
            xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);

            //Close the Document - this saves the document contents to the output stream
            document.Close();
            output.Position = 0;

            //return file to browser page
            return new FileStreamResult(output, "application/pdf");
        }

此行string webgridstyle = style.GetCss();中表格CSS的一部分,用于样式内容。这里使用margin-left:auto; margin-right:auto;的中心表似乎没有解决问题。

public string GetCss()
    {
        string webgridstyle = " .webgrid-table {    " +
                "           font-family: Arial,Helvetica,sans-serif;    " +
                "           font-size: 11px;    " +
                "           font-weight: normal;    " +
                "           width: 100%;    " +
                //"           display: table; " +
                "           border-collapse: collapse;  " +
                "           border-spacing: 0; " +
                "           border: solid 0.1px #000000; " +
                "           background-color: white;  repeat-header:yes;  margin-left:auto; margin-right:auto;" +
                "       }   " +
                "       " +

PDF Files download

Download/Print File

Zip文件 - 包含来自同一网页应用的两个文件 一个下载到pdf中,显示正确对齐的内容 另一个打印的内容未正确对齐

更新1

所以我只是尝试了一个不属于我自己的随机链接,它有自己的pdf,我遇到了同样的问题。所以它最有可能不是布鲁诺所说的html / css / writer问题。

链接Stackoverflow - The Developer Ecosystem

查看

View of pdf

打印

Output of PDF

更新2

另一件让我完全忘记的事情就是在不同的浏览器上查看。我一直在使用Firefox,而且我遇到了问题。 我在Chrome,Opera和Internet Explorer上进行了测试,可以完美地输出打印效果。

0 个答案:

没有答案