【问题标题】:How to print the details in landscape in pdf using itextsharp?如何使用 itextsharp 在 pdf 中以横向打印详细信息?
【发布时间】:2017-01-05 09:17:02
【问题描述】:

我的要求是横向打印细节,而不是页面旋转。我需要横向打印内容。

Document doc = new Document(iTextSharp.text.PageSize.A4, (float)MarginLeft, (float)MarginRight, (float)MarginTop, (float)MarginBottom);
//210 mm width * 297 mm height
PaperWidthAvailable = iTextSharp.text.Utilities.MillimetersToPoints(210f) - ((float)MarginLeft + (float)MarginRight);
PaperHeightAvailable = iTextSharp.text.Utilities.MillimetersToPoints(297f) - ((float)MarginTop + (float)MarginBottom);
wdtOFcell = (float)BarcodeWidth + (float)BarcodeSpaceHorizontal;
colNo = (int)Math.Floor(PaperWidthAvailable / wdtOFcell);
TableWidth = wdtOFcell * colNo;
htOFcell = (float)BarcodeHeight + (float)BarcodeSpaceVertical;
PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
doc.Open();
int noOfColumns = colNo;
// int additionalRow = imageBarcodeLists.Count % noOfColumns;
int i = 1;
PdfPTable table = new PdfPTable(noOfColumns);
table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
table.HorizontalAlignment = 0;
table.TotalWidth = TableWidth;
table.LockedWidth = true;
float[] widths = new float[colNo];
for (int j = 0; j < colNo; j++)
{
    widths[j] = wdtOFcell;
}
table.SetWidths(widths);
iTextSharp.text.Image itextBarcodeImage = null;
foreach (System.Drawing.Image barcodeImage in imageBarcodeLists)
{
    var imageCompressor = new ImageCompressionUtility();
    System.Drawing.Image barcodeImages = imageCompressor.TrimImageWhiteSpacesFromImage(barcodeImage);
    itextBarcodeImage = iTextSharp.text.Image.GetInstance(barcodeImages, BaseColor.BLUE);
    itextBarcodeImage.ScaleAbsolute((float)BarcodeWidth, (float)BarcodeHeight);
    PdfPCell cells = new PdfPCell(itextBarcodeImage);
    cells.Border = iTextSharp.text.Rectangle.NO_BORDER;
    cells.PaddingTop = 0f;
    cells.PaddingRight = 0f;
    cells.PaddingBottom = 0f;
    cells.PaddingLeft = 0f;
    cells.UseAscender = true;
    cells.FixedHeight = htOFcell;
    cells.BackgroundColor = BaseColor.WHITE;
    cells.Border = iTextSharp.text.Rectangle.NO_BORDER;
    table.AddCell(cells);
    i++;
}
doc.Add(table);
doc.Close();

【问题讨论】:

标签: c# asp.net asp.net-mvc-4 itext


【解决方案1】:

常量iTextSharp.text.PageSize.A4 实际上是一个Rectangle,它是这样创建的:

public static readonly Rectangle A4 = new Rectangle(595,842);

如果您想旋转页面,可以使用Rotate() 方法,如我对此问题的回答中所述:How to print custom page size as portrait in itextsharp

但是,根据您的评论“它只旋转纸张而不旋转所写的内容”,您可能正在寻找这样创建的页面大小:

Rectangle myA4 = new Rectangle(842,595);
Document doc = new Document(myA4);

如果这不起作用,请查看我对问题iText - Rotate page content while creating PDF的回答

在那个答案中,我使用页面事件来介绍页面旋转:

public class MyPdfPageEvent : iTextSharp.text.pdf.PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        writer.AddPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
    }
}

如果以上都没有达到你想要的效果,你应该改进你的问题(否则它将被关闭为“不清楚所问的内容”)。

【讨论】:

  • 我也旋转单元格。 cell.rotation=180;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多