【问题标题】:I want to increase the width of the first column in itextsharp pdf creation我想增加itextsharp pdf创建中第一列的宽度
【发布时间】:2020-03-08 05:53:10
【问题描述】:

我想增加 itextsharp pdf 创建中第一列矩阵的宽度 图片显示了我想要的 enter image description here

PdfPTable pdfTable = new PdfPTable(dataGridView4.ColumnCount);
        //pdfTable. = 5;

        pdfTable.DefaultCell.Padding = 3;
        pdfTable.DefaultCell.HorizontalAlignment = 0;
        pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;


        pdfTable.WidthPercentage = 100;
       // pdfTable.SetWidths(GetHeaderWidths(font.GetFont("ARIAL", 30), headers));

        pdfTable.DefaultCell.VerticalAlignment = Element.ALIGN_CENTER;
        pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
        //pdfTa = Element.ALIGN_MIDDLE;
        pdfTable.DefaultCell.BorderWidth = 1;
        //pdfTable.DefaultCell.Width = 10;(
        //pdfTable.TotalWidth = 300;
        pdfTable.DefaultCell.UseAscender = true;
        iTextSharp.text.Font fon = FontFactory.GetFont("ARIAL", 30);
foreach (DataGridViewColumn column in dataGridView4.Columns)
        {

            int c = 0;


            PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
            if (c == 0)
            {
                // pdfTable.SetWidths(c);

               // column.HeaderText = "330";
               // dataGridView4.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                // pdfTable.AddCell(cell);
            }

【问题讨论】:

    标签: c# pdf datagridview itext


    【解决方案1】:

    您可以使用 pdfptable 自定义宽度

    PdfPTable table = new PdfPTable(5); // this will be your # of columns.
    table.HorizontalAlignment = 0;
    table.TotalWidth = 500f;
    table.LockedWidth = true;
    float[] widths = new float[] { 20f, 60f, 60f, 30f, 50f };
    table.SetWidths(widths);
    

    这是你想要专注的:

     float[] widths = new float[] { 20f, 60f, 60f, 30f, 50f };
    

    这篇文章可能对您有所帮助: Source

    另一个完整的例子:

    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("TableColumnWidth.pdf"));
            doc.open();
    
            PdfPTable table = new PdfPTable(4);
            table.addCell(new PdfPCell(new Phrase("Cell 1")));
            table.addCell(new PdfPCell(new Phrase("Cell 2")));
            table.addCell(new PdfPCell(new Phrase("Cell 3")));
            table.addCell(new PdfPCell(new Phrase("Cell 4")));
    
            // Defiles the relative width of the columns
            float[] columnWidths = new float[]{10f, 20f, 30f, 10f};
            table.setWidths(columnWidths);
    
            doc.add(table);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
    

    source

    【讨论】:

    猜你喜欢
    • 2018-07-29
    • 2012-03-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多