如何使用 itextsharp 在 pdf 中打印横向细节



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

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

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

最新更新