iTextSharp page size not working

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

iTextSharp page size not working

keith
i have set the itextsharp document to iTextSharp.text.PageSize.A5 (half of A4 paper size), but still cannot get the correct size when printed out, can some expert advise me?

 photo itextsparpsize.png

below is my source code

markup language :

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="PrintReceipt.aspx.vb" Inherits="PrintReceipt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
                                                                                                                               
                    aaa
               
                    bbb
               
                    aaa
               
                    bbb
               
   
</body>
</html>

code behind :

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Text.RegularExpressions
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.xml
Imports System.Xml
Imports iTextSharp.text.html.simpleparser

Partial Class PrintReceipt
    Inherits System.Web.UI.Page

    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
        Dim mem As New MemoryStream()
        Dim twr As New StreamWriter(mem)
        Dim myWriter As New HtmlTextWriter(twr)
        MyBase.Render(myWriter)
        myWriter.Flush()
        myWriter.Dispose()
        Dim strmRdr As New StreamReader(mem)
        strmRdr.BaseStream.Position = 0
        Dim pageContent As String = strmRdr.ReadToEnd()
        strmRdr.Dispose()
        mem.Dispose()
        writer.Write(pageContent)
        CreatePDFDocument(pageContent)


    End Sub
    Public Sub CreatePDFDocument(ByVal strHtml As String)

        Dim strFileName As String = HttpContext.Current.Server.MapPath("test.pdf")
        ' step 1: creation of a document-object
        'Dim document As New Document(PageSize.A5, 0, 0, 0, 0)

        Dim document As New iTextSharp.text.Document(iTextSharp.text.PageSize.A5, 10, 10, 10, 10)


        ' step 2:
        ' we create a writer that listens to the document

        PdfWriter.GetInstance(document, New FileStream(strFileName, FileMode.Create))
        Dim se As New StringReader(strHtml)
        Dim obj As New HTMLWorker(document)
        document.Open()
        obj.Parse(se)
        document.Close()
        ShowPdf(strFileName)

    End Sub
    Public Sub ShowPdf(ByVal strFileName As String)
        Response.ClearContent()
        Response.ClearHeaders()
        Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName)
        Response.ContentType = "application/pdf"
        Response.WriteFile(strFileName)
        Response.Flush()
        Response.Clear()
    End Sub
End Class
Loading...