title: PDF Itextsharp printing is adding margins on right and bottom of page on print output
body: <p>I have a PDF export function(ality) where I have the following output when the result is rendered on the page/view</p>
  
  <p><a href="https://i.stack.imgur.com/hyijS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hyijS.png" alt="Output on Webpage"></a></p>
  
  <p>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.</p>
  
  <p><a href="https://i.stack.imgur.com/1Jc3P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1Jc3P.png" alt="Output on printer"></a></p>
  
  <p>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?</p>
  
  <p>My export method is as follows (irrelevant code trimmed out)</p>
  
  <pre><code>    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
              ("&lt;html&gt;" +
              "&lt;head&gt;{0}&lt;/head&gt;" +
              "&lt;body&gt;" +
              "&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;" +
              "{1}&lt;/body&gt;" +
              "&lt;/html&gt;",
              "&lt;style&gt;" + webgridstyle + "&lt;/style&gt;", 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");
          }
  </code></pre>
  
  <p>Part of the table CSS from this line <code>string webgridstyle = style.GetCss();</code>, to style contents. Centering table here with <code>margin-left:auto; margin-right:auto;</code> does not seem to have fixed the issue.</p>
  
  <pre><code>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;" +
                  "       }   " +
                  "       " +
  </code></pre>
  
  <p><a href="https://www.dropbox.com/s/v68eibb1jrg3neh/pdf_files_so.7z?dl=0" rel="nofollow noreferrer">PDF Files download</a></p>
  
  <p><a href="https://i.stack.imgur.com/chBah.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/chBah.png" alt="Download/Print File"></a></p>
  
  <p>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</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>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.</p>
  
  <p>Link <a href="https://www.stackoverflowbusiness.com/hubfs/Q3%20Report%20-%20Education/The%20Developer%20Ecosystem-%20Education%20And%20The%20New%20Generation%20Of%20Coders.pdf" rel="nofollow noreferrer">Stackoverflow - The Developer Ecosystem</a></p>
  
  <p>View</p>
  
  <p><a href="https://i.stack.imgur.com/Nst0D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nst0D.png" alt="View of pdf"></a></p>
  
  <p>Print</p>
  
  <p><a href="https://i.stack.imgur.com/5kWt2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5kWt2.png" alt="Output of PDF"></a></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>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. <strong>I tested on Chrome, Opera and Internet Explorer which output the printing perfectly fine.</strong></p>