ITextSharp 在统一编辑器中完美运行,但当应用程序构建为 windows 独立时生成损坏的 0 字节文件

ITextSharp works perfectly in unity editor but generates corrupt 0 byte file when app is build to windows standalone

下面的代码应该生成一个 pdf 并将其与其中的内容一起保存在桌面上,这在 unity 编辑器中运行良好,但最终的 windows 构建无法做到这一点。它生成了一个 pdf 并保存在桌面上,但 pdf 已损坏并显示 0 字节大小。我找不到错误,这是代码:

 public void EnglishPdf()
{

    FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\Loshu-Numerology.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
    Document doc = new Document(PageSize.A4);

    PdfWriter writer = PdfWriter.GetInstance(doc, fs);
    doc.Open();

    //Page0
    doc.NewPage();

    Paragraph title = new Paragraph("Loshu Grid Numerology Report");
    title.Alignment = Element.ALIGN_CENTER;

    Chunk nameChunk = new Chunk("Name: " + Gridvalues.instance.name_string + "\r\n");
    Chunk bday = new Chunk("Date of Birth: " + Gridvalues.instance.Date.text+ "-"+ Gridvalues.instance.Month.text+"-"+Gridvalues.instance.Year.text);
   /* iTextSharp.text.Image gridSample = iTextSharp.text.Image.GetInstance(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\ScreenShot.png");
    gridSample.Alignment = Element.ALIGN_LEFT;*/
    doc.Add(title);

    doc.Add(nameChunk);
    doc.Add(bday);



    //Page-1
    doc.NewPage();
    Paragraph Title1 = new Paragraph("Missing Number Effects\r\n\r\n");
    Title1.Alignment = Element.ALIGN_CENTER;
    Paragraph Description1 = new Paragraph("The missing Number in the Grid signifies what lessons a person has to learn in this lifetime and through proper analysis, we can help them in understanding what is required to learn and provide remedies for the same so that they can improve the difficult situations of life.The Numbers missing in your Grid are: \r\n\r\n");
    Description1.Alignment = Element.ALIGN_LEFT;
    Paragraph content1 = new Paragraph(missingdetailsFull);
    content1.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title1);
    doc.Add(Description1);
    doc.Add(content1);


    //Page-2
    doc.NewPage();
    Paragraph Title2 = new Paragraph("Repeating Numbers\r\n\r\n");
    Title2.Alignment = Element.ALIGN_CENTER;
    Paragraph Content2 = new Paragraph(repetitionDetails);
    Content2.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title2);
    doc.Add(Content2);


    //Page-3
    doc.NewPage();
    Paragraph Title3 = new Paragraph("Planes\r\n");
    Paragraph ArrowStrength = new Paragraph("Arrows of Strength\r\n");
    Paragraph StrengthDetails = new Paragraph(strengthArrows);
    Title3.Alignment = Element.ALIGN_CENTER;
    ArrowStrength.Alignment = Element.ALIGN_CENTER;
    StrengthDetails.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title3);
    doc.Add(ArrowStrength);
    doc.Add(StrengthDetails);

    Paragraph shortarrows = new Paragraph("\r\n\r\nFour Short Arrows\r\n");
    Paragraph shortArrowDetails = new Paragraph(shortArrows); 
    shortarrows.Alignment = Element.ALIGN_CENTER;
    StrengthDetails.Alignment = Element.ALIGN_LEFT;
    doc.Add(shortarrows);
    doc.Add(shortArrowDetails);



    Paragraph weaknessarrow = new Paragraph("\r\n\r\nArrows of Weakness\r\n");
    Paragraph weakDetails = new Paragraph(weaknessArrows); 
    weaknessarrow.Alignment = Element.ALIGN_CENTER;
    weakDetails.Alignment = Element.ALIGN_LEFT;
    doc.Add(weaknessarrow);
    doc.Add(weakDetails);

    //Page4
    doc.NewPage();
    Paragraph title4 = new Paragraph("Lucky Colors, Professions & Days");
    title4.Alignment = Element.ALIGN_CENTER;
    Paragraph luckyd = new Paragraph("Lucky Days\r\n\r\n");
    luckyd.Alignment = Element.ALIGN_CENTER;
    Paragraph lucky_days = new Paragraph(luckydays);
    lucky_days.Alignment = Element.ALIGN_LEFT; 
    doc.Add(title4);
    doc.Add(luckyd);
    doc.Add(lucky_days);


    Paragraph luckyc = new Paragraph("Lucky Colors\r\n\r\n");
    luckyc.Alignment = Element.ALIGN_CENTER;
    Paragraph lucky_color = new Paragraph(colors);
    lucky_color.Alignment = Element.ALIGN_LEFT;
    doc.Add(luckyc);
    doc.Add(lucky_color);

    Paragraph lucky_P = new Paragraph("Lucky Professions\r\n\r\n");
    lucky_P.Alignment = Element.ALIGN_CENTER;
    Paragraph luckyprof = new Paragraph(luckyProfessions);
    luckyprof.Alignment = Element.ALIGN_LEFT;
    doc.Add(lucky_P);
    doc.Add(luckyprof);


    //Page5
    doc.NewPage();
    Paragraph Title5 = new Paragraph("Avoid Professions, Colors");
    Title5.Alignment = Element.ALIGN_CENTER;
    Paragraph avoidC = new Paragraph("Avoid Colors\r\n\r\n");
    avoidC.Alignment = Element.ALIGN_CENTER;
    Paragraph avoidCol = new Paragraph(avoidColors);
    avoidCol.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title5);
    doc.Add(avoidC);
    doc.Add(avoidCol);

    Paragraph avoidP = new Paragraph("Avoid Professions\r\n\r\n");
    avoidP.Alignment = Element.ALIGN_CENTER;
    Paragraph avoidProf = new Paragraph(avoidProfessions);
    avoidProf.Alignment = Element.ALIGN_LEFT;
    doc.Add(avoidP);
    doc.Add(avoidProf);



    //Page6
    doc.NewPage();
    Paragraph Title6 = new Paragraph("KUA Number");
    Title6.Alignment = Element.ALIGN_CENTER;
    Paragraph kuaN = new Paragraph("Your Kua Number is " + kua);
    kuaN.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title6);
    doc.Add(kuaN);


    //Page7
    doc.NewPage();
    Paragraph Title7 = new Paragraph("PM-PY-PD");
    Title7.Alignment = Element.ALIGN_CENTER;
    Paragraph PyearNumber = new Paragraph("Your Personal Year Number is "+ Py);
    PyearNumber.Alignment = Element.ALIGN_LEFT;
    Paragraph PmonthNumber = new Paragraph("Your Personal Month Number is "+ Pm);
    PmonthNumber.Alignment = Element.ALIGN_LEFT;
    Paragraph PdateNumber = new Paragraph("Your Personal Day Number is "+ Pd);
    PdateNumber.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title7);
    doc.Add(PyearNumber);
    doc.Add(PmonthNumber);
    doc.Add(PdateNumber);


    //Page8
    doc.NewPage();
    Paragraph Title8 = new Paragraph("Vedic Power Boosters");
    Title8.Alignment = Element.ALIGN_CENTER;
    Paragraph boosterDetails = new Paragraph(vedicBooster);
    boosterDetails.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title8);
    doc.Add(boosterDetails);


    //Page9
    doc.NewPage();
    Paragraph Title9 = new Paragraph("Punchlines");
    Title9.Alignment = Element.ALIGN_CENTER;
    Paragraph punchLine = new Paragraph(punchlines);
    punchLine.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title9);
    doc.Add(punchLine);



    //Page10
    doc.NewPage();
    Paragraph Title10 = new Paragraph("Driver Characterstics");
    Title10.Alignment = Element.ALIGN_CENTER;
    Paragraph driverChar = new Paragraph(characterstics);
    driverChar.Alignment = Element.ALIGN_LEFT;
    doc.Add(Title10);
    doc.Add(driverChar);

    doc.Close();

}

我关注了一些与此类似的其他主题,但大多数主题最后都缺少 doc.Close(),这在我的代码中并非如此。我从一开始就拥有它。我该如何解决?

构建也成功构建,没有任何错误,一切运行良好。我猜代码中缺少某些东西。任何帮助都会有所帮助。

升级到Itext7,它会被修复。谢谢