PDF Itextsharp printing is adding margins on right

2019-06-11 09:04发布

问题:

I have a PDF export function(ality) where I have the following output when the result is rendered on the page/view

As you can see, the contents of the table are center aligned on the page and all seems perfect. But upon printing the contents (table) are shrinked slightly and output is pushed slightly left and slightly up, meaning there's extra space on the right and bottom of the page.

This distorted output appears like this on both paper printer and document writer printing outputs. I cannot pinpoint whats causing this as all seems fine in the code. The same code is used to create and output a PDF file and all contents appeared centered perfectly. Printing from the webpage (first image) is where the issue is. Is this a CSS issue or is it an XMLworker issue?

My export method is as follows (irrelevant code trimmed out)

    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");
        }

Part of the table CSS from this line string webgridstyle = style.GetCss();, to style contents. Centering table here with margin-left:auto; margin-right:auto; does not seem to have fixed the issue.

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

Zip file - includes two files both from the same web page app one is downloaded into a pdf which shows contents properly aligned the other is printed with content not aligned properly

UPDATE 1

So I just tried a random link thats not my own, that has its own pdf and I am getting the same issue. So its most likely not an html/css/writer issue then as stated by Bruno.

Link Stackoverflow - The Developer Ecosystem

View

Print

UPDATE 2

Another thing that totally slipped my mind was to check this on different browsers. I have been using Firefox all this time, and it were I was getting the issue. I tested on Chrome, Opera and Internet Explorer which output the printing perfectly fine.