iTextsharp landscape document

只愿长相守 提交于 2019-12-18 11:51:11

问题


I am trying to create Landscape PDF using iTextSharp but It is still showing portrait. I am using following code with rotate:

Document document = new Document(PageSize.A4, 0, 0, 150, 20);
FileStream msReport = new FileStream(Server.MapPath("~/PDFS/") + "Sample1.pdf", FileMode.Create);

try
{
    // creation of the different writers
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);

    document.Open();

    PdfPTable PdfTable = new PdfPTable(1);
    PdfTable.SpacingBefore = 30f;


    PdfPCell PdfPCell = null;

    Font fontCategoryheader = new Font(Font.HELVETICA, 10f, Font.BOLD, Color.BLACK);

    for (int i = 0; i < 20; i++)
    {
        PdfPCell = new PdfPCell(new Phrase(new Chunk("Sales Manager: ", fontCategoryheader)));
        PdfPCell.BorderWidth = 0;
        PdfPCell.HorizontalAlignment = Element.ALIGN_LEFT;

        if (i % 2 == 0)
            PdfPCell.BackgroundColor = Color.LIGHT_GRAY;

        PdfPCell.PaddingBottom = 5f;
        PdfPCell.PaddingLeft = 2f;
        PdfPCell.PaddingTop = 4f;
        PdfPCell.PaddingRight = 4f;
        PdfTable.AddCell(PdfPCell);
    }

    document.Add(PdfTable);
    document.NewPage();

}
catch (Exception ex)
{
    Console.Error.WriteLine(ex.Message);
}

finally
{
    // we close the document 
    document.Close();
}

Please suggest solution.

Thanks.


回答1:


Try this

Document Doc = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
Doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

you might also need this to expand a table to max width.

var _pdf_table = new PdfPTable(2); // table with two  columns
PdfPCell hc = new PdfPCell();
_pdf_table.WidthPercentage = 100; //table width to 100per
_pdf_table.SetTotalWidth(new float[] { 25, iTextSharp.text.PageSize.A4.Rotate().Width - 25 });// width of each column

You must make sure that when setting the page size you do it before a call to Doc.Open();

Regards.




回答2:


No need to initialize the Document and reset the page size...

Document doc = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 10, 10);

...will do the trick.

(4.1.6.0)



来源:https://stackoverflow.com/questions/10040161/itextsharp-landscape-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!