Search code examples
c#unity-game-engineitext

ItextSharp generates pdf file of 0 bytes in PC build. Works in editor


I am using iTextSharp.dll to generate a pdf in a PC build. Version: 2018.4.9f - OS 10 - Build:PC - C# - Visual Studio 2017 community

The pdf is being generated in the editor as it should with the text I put in the script but after building for PC it generated a pdf with 0 Bytes. When it is opened it pops a dialog box saying file type not recognized or Damaged pdf. How does it achieve the same result in PC build? What am I missing?

In my scene, I have a camera with a c# script on it. Script has a single method which is called when a UI button is pressed. Here is the code:

using UnityEngine;
using UnityEngine.UI;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;

public class Panel : MonoBehaviour
{
     WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();

     public void EnglishPdf()
     {
         FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\\Test.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
         Document doc = new Document(PageSize.A4);
         PdfWriter writer = PdfWriter.GetInstance(doc, fs);
         doc.Open();

         //Page0
         doc.NewPage();
         // PdfPTable page0 = new PdfPTable(2);
         Paragraph title = new Paragraph("Test PDF");
         title.Alignment = Element.ALIGN_CENTER;

         Chunk nameChunk = new Chunk("Name: "  + "\r\n");
         Chunk bday = new Chunk("Date of Birth: "  );
         doc.Add(title);
         // doc.Add(page0);
         doc.Add(nameChunk);
         doc.Add(bday);

         doc.Close();
     }
}

Solution

  • Thanks, it was version issue. I fixed it by upgrading to itext7