簡體   English   中英

從文件中讀取行並寫入多個PDF不起作用

[英]Read lines from a file and write to multiple PDFs doesn't work

因此,我嘗試逐行讀取文件並將其寫入多個PDF,每個PDF文件應僅包含50行,如果一個PDF已滿,則將創建另一個PDF直到所有行完成。

我幾乎完成了它。 但問題是每次只有第一個PDF有內容。 其他都是空的。 這是我的代碼。

if ((System.IO.File.Exists(@"C:\Coil master 28-02-2019.csv")) == true)
{
    int lineNumber = 1;
    int PDFNumber = 0;
    for (; PDFNumber < 4; PDFNumber++)
    {
        string path = Application.StartupPath;
        var pgSize = new iTextSharp.text.Rectangle(2976, 4194);

        Document pdfdoc = new Document(pgSize); // Setting the page size for the PDF
        PdfWriter writer = PdfWriter.GetInstance(pdfdoc, new FileStream(path + "/MasterCoils/" + (1 + (50 * PDFNumber)) + "-" + (50 + (PDFNumber * 50)) + ".pdf", FileMode.Create)); //Using the PDF Writer class to generate the PDF
        pdfdoc.Open(); // Opening the PDF to write the data from the textbox

        foreach (string line in System.IO.File.ReadLines(@"C:\Coil master 28-02-2019.csv").Skip(1 + (50 * PDFNumber)).Take(50))
        {
                string[] inputArray = line.Split(delimiters); // split the input string by using the delimiter ','

                COILID = inputArray[0];
                TYPE = inputArray[1];
                COLOR = inputArray[2];
                WEIGHT = Int32.Parse(inputArray[3]);
                GAUGE = inputArray[4];
                WIDTH = inputArray[5];

                PdfContentByte cb = writer.DirectContent;
                // we tell the ContentByte we're ready to draw text
                cb.BeginText();

                // set up Font and Size for Content to be shown in PDF
                BaseFont mybf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

                cb.SetFontAndSize(mybf, 15);
                cb.SetTextMatrix(250 + (595 * i), (419 * (9 - j)) + 280);
                cb.ShowText(COILID + "+" + TYPE + "+" + COLOR + "+" + WEIGHT + "+" + GAUGE + "+" + WIDTH);

                // we tell the contentByte, we've finished drawing text
                cb.EndText();

                lineNumber++;
        }

        pdfdoc.Close();
    }
}

如果我刪除了foreach語句,那么所有PDF都會有一些內容。 所以我想問題出在foreach,但是我找不到確切的地方。 請給我一些幫助。 謝謝!

我運行了這段代碼,它完全按預期工作,讀取了一個csv並制作了4個新PDF,所有這些都包含數據。

我真正改變的唯一一件事是PDF的寫入方式。

我不是ITextSharp專家,但希望對您有幫助。

if (File.Exists(@"Coil master 28-02-2019.csv") == true)
{
    int fileNumber = 0;

    for (; fileNumber < 4; fileNumber++)
    {
        var pgSize = new Rectangle(2976, 4194);

        var pdfdoc = new Document(pgSize); // Setting the page size for the PDF

        pdfdoc.Open(); // Opening the PDF to write the data from the textbox

        foreach (string line in File.ReadLines("Coil master 28-02-2019.csv").Skip(1 + (50 * fileNumber)).Take(50))
        {
            string[] inputArray = line.Split(','); // split the input string by using the delimiter ','

            var COILID = inputArray[0];
            var TYPE = inputArray[1];
            var COLOR = inputArray[2];
            var WEIGHT = Int32.Parse(inputArray[3]);
            var GAUGE = inputArray[4];
            var WIDTH = inputArray[5];

            var p = new Paragraph(COILID + "+" + TYPE + "+" + COLOR + "+" + WEIGHT + "+" + GAUGE + "+" + WIDTH);
            pdfdoc.Add(p);
        }

        pdfdoc.Close();
    }
}

暫無
暫無

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

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