ITextSharp在unity编辑器中完美工作,但是当应用程序构建到Windows独立版本时会生成损坏的0字节文件

问题描述 投票:1回答:1

下面的代码应该生成一个pdf文件,并将其保存在带有内容的桌面上,这在unity编辑器中可以正常进行,但是最终的Windows构建无法做到这一点。它生成了一个pdf文件,并保存在桌面上,但pdf文件已损坏并显示0byte大小。我找不到错误,这是代码:

 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(),我的代码中不是这种情况。我从一开始就拥有它。我该如何解决?

构建也成功构建,没有任何错误,并且一切正常。我猜代码中缺少一些东西。任何帮助都会有所帮助。

c# unity3d itext pdf-generation
1个回答
0
投票

升级到Itext7,它将得到修复。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.