繁体   English   中英

如何使用itextsharp以pdf格式打印横向细节?

[英]How to print the details in landscape in pdf using itextsharp?

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

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

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

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

如果要旋转页面,可以使用Rotate()方法,如我对此问题的回答中所述: 如何在itextsharp中将自定义页面大小打印为纵向

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

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

如果这不起作用,请查看我对iText问题的回答- 在创建PDF时旋转页面内容

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

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

如果以上都没有你想要的效果,你应该改进你的问题(或者它将被关闭为“不清楚被问到什么”)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM