C# iTextsharp 横向文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10040161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:13:56  来源:igfitidea点击:

iTextsharp landscape document

c#itextsharp

提问by DotnetSparrow

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

我正在尝试使用 iTextSharp 创建横向 PDF,但它仍然显示纵向。我在旋转中使用以下代码:

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.

谢谢。

采纳答案by Shoaib Shaikh

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();

您必须确保在设置页面大小时在调用之前进行 Doc.Open();

Regards.

问候。

回答by COeDev

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)

(4.1.6.0)