Hack4Life
Hack4Life

Reputation: 563

iTextSharp PDF rotating page is shifted

I try to create a multipage pdf document using iTextSharp. I have a object that contains the orientation of itself (landscape or portrait). When The first Object contains informations that it needs landscape mode, i create the document with Document doc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f). This works pretty good until the next element is in portrait mode! If an element is in portrait mode, i set the pagesize again: doc.SetPageSize(PageSize.A4);.

At this point the element should be on a portrait A4 page in the PDF document but it is still in landscape mode. It switches pages not until a new object is reached OR a pagebreak is reached within the current element!

Here is my code:

TableObject to_first = myTables.First();
//current object need landscape orientation
if (to_first._orientation == "landscape")
{
    //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
    using (Document doc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f))
    {
        //Create a writer that's bound to our PDF abstraction and our stream
        using (PdfWriter writer = PdfWriter.GetInstance(doc, ms))
        {
            //Open the document for writing
            doc.Open();

            //writer.CloseStream = false;
            //loop all tableobjects inside the document & the instance of PDFWriter itself! 
            foreach (TableObject to in myTables.ToList())
            {
                doc.NewPage();
                //look for the requested orientation by the current object and apply it
                if (to._orientation == "landscape")
                {
                    doc.SetPageSize(PageSize.A4.Rotate());
                }
                else if (to._orientation == "portrait")
                {
                    doc.SetPageSize(PageSize.A4);
                }
                currentTable = to;
                //Get the data from database corresponding to the current tableobject and fill all the stuff we need!
                DataTable dt = getDTFromID(currentTable._tableID);
                Object[] genObjects = new Object[5];
                genObjects = gen.generateTable(dt, currentTable._tableName, currentTable._tableID.ToString(), currentTable, true);

                StringBuilder sb = (StringBuilder)genObjects[1];
                String tableName = sb.ToString();
                Table myGenTable = (Table)genObjects[0];
                String table = genObjects[2].ToString();

                using (StringReader srHtml = new StringReader(table))
                {
                    //Parse the HTML
                    iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                }
            }

            //After all of the PDF "stuff" above is done and closed but **before** we
            //close the MemoryStream, grab all of the active bytes from the stream
            doc.Close();
            bytes = ms.ToArray();
        }
    }
}

How can I ensure that every page is rotated correctly?

Upvotes: 0

Views: 1070

Answers (1)

mkl
mkl

Reputation: 96074

doc.SetPageSize only sets the size used for creating new pages, not for the existing pages. Thus, you should move your

doc.NewPage();

call after the SetPageSize calls:

//look for the requested orientation by the current object and apply it
if (to._orientation == "landscape")
{
    doc.SetPageSize(PageSize.A4.Rotate());
}
else if (to._orientation == "portrait")
{
    doc.SetPageSize(PageSize.A4);
}

// After setting the page size, trigger the generation of the new page
doc.NewPage();

Upvotes: 2

Related Questions