簡體   English   中英

pdf大小限制后無法查看其他表格

[英]Not able to see other tables after pdf size limit

我已使用iTextSharp生成PDF文件。

我創建了6個PdfPTable但僅顯示3個。

// Create new PDF document 
Document document = new Document(PageSize.A4, 20f, 20f, 20f, 20f);
try {
    PdfWriter writer = PdfWriter.GetInstance(document,
        new FileStream(filename, FileMode.Create));

    document.Open();
    int spacing = 0;

    for (int i = 0; i <= 6; i++) {

        PdfPTable table = new PdfPTable(2);

        table.TotalWidth = 144f;
        table.LockedWidth = false;
        PdfPCell cell = new PdfPCell(new Phrase("This is table" + i));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1;
        table.AddCell(cell);

        table.WriteSelectedRows(0, -1,
            document.Left + spacing, document.Top,
            writer.DirectContent);

        spacing = spacing + 200;
    }
}

catch (Exception ex) {}

finally {
    document.Close();
    ShowPdf(filename);
}

在這里,我將for循環放置了6次,但只顯示了3張表。

屏幕截圖

如何顯示全部6張桌子? 我想在換行后只顯示1行中的3個表,並顯示其他3個表。

我認為您的問題的標題實際上實際上是對問題的總結。

使用WriteSelectedRows ,您有責任提供要寫入的X和Y位置,並且繪圖超出頁面邊界。 A4有595個水平單元,而且空間不足。 這是100%有效的,但是大多數人看不到。 我猜您想將表“包裝”到下一行。 有兩種方法可以做到這一點:

頁面更大

切換到PageSize.A0東西,您應該有更多的空間。 頁面大小只是一個提示,無論如何,打印軟件將根據需要進行縮放。

MOD簽入循環

這稍微復雜一點,但是每隔n表,您會將x坐標重置為左邊緣,並將y增加到上一行表的最高位置。

int spacing = 0;
//The current Y position
float curY = document.Top;

//Well ask iText how tall each table was and set the tallest to this variable
float lineHeight = 0;

//Maximum number of tables that go on a line
const int maxPerLine = 3;

for (int i = 0; i <= 6; i++) {

    PdfPTable table = new PdfPTable(2);

    table.TotalWidth = 144f;
    table.LockedWidth = false;
    PdfPCell cell = new PdfPCell(new Phrase("This is table" + i));
    cell.Colspan = 3;
    cell.HorizontalAlignment = 1;
    table.AddCell(cell);

    table.WriteSelectedRows(0, -1,
        document.Left + spacing, curY,
        writer.DirectContent);

    spacing = spacing + 200;

    //Set the height to whichever is taller, the last table or this table
    lineHeight = Math.Max(lineHeight, table.TotalHeight);

    //If we're at the "last" spot in the "row"
    if (0 == (i + 1) % maxPerLine) {
        //Offset our Y by the tallest table
        curY -= lineHeight;

        //Reset "row level" variables
        spacing = 0;
        lineHeight = 0;
    }
}

包裝表

這是我真正的建議。 如果要“包裝”表,則只需使用一個外部表來保存內部表,就可以免費獲取所有內容,而不必與DirectContent (盡管您可能希望更改表的邊界)。

var outerTable = new PdfPTable(3);
outerTable.DefaultCell.Border = PdfPCell.NO_BORDER;

for (int i = 0; i <= 6; i++) {

    PdfPTable innerTable = new PdfPTable(2);

    innerTable.TotalWidth = 144f;
    innerTable.LockedWidth = false;
    PdfPCell cell = new PdfPCell(new Phrase("This is table" + i));
    cell.Colspan = 3;
    cell.HorizontalAlignment = 1;
    innerTable.AddCell(cell);

    outerTable.AddCell(innerTable);

}

document.Add(outerTable);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM