1

I've been using the sharpPDF API to try and create pdf images inside Unity3D. The hello world test went fine but no matter what I do I can't get images to work. Using the ddImage function makes the pdf reader crash and using newAddImage seems to do nothing at all. Unfortunately google has been no help at all so I'm hoping someone here can help me out!

I did look into a different library but pdfsharp just plain didn't work giving a ton of errors on the compiler and iTextSharp is to expensive.

API is here http://www.francescogallorini.com/2011/02/unity-sharp-pdf/

and my code is:

    //byte[] image;
    //image = File.ReadAllBytes("Assets/Sprites/128x128 player ship trial.png");

    pdfDocument myDoc = new sharpPDF.pdfDocument("Awesome Space Station", "Al Wyvern");
    pdfPage myPage = myDoc.addPage(500,500);
    myPage.newAddImage("Assets/Sprites/128x128 player ship trial.png", 10, 10);
    //myPage.addImage(image, 0,0,256,256);
    myPage.addText("Hello World", 128, 128, predefinedFont.csCourier, 20);
    myDoc.createPDF("Awesome Space Station.pdf");
    myPage = null;
    myDoc = null;

Thanks for any help you can give

Al Wyvern
  • 199
  • 1
  • 15
  • I wouldn't use the AddImage(string, int, int) method. Instead I would use the Addimage(image, int, int) method. The image should be made by using a byte[] and make sure the byte array is read from a file using Encoding.UTF8. – jdweng Mar 16 '16 at 21:16
  • Using addimage(byte[],int, int) doesn't work, when I do it that way, when you open the pdf adobe gives an out of memory error – Al Wyvern Mar 16 '16 at 21:18
  • You have to use a method to create image from byte array. See posting : http://stackoverflow.com/questions/9173904/bytearray-to-image-conversion – jdweng Mar 16 '16 at 21:43
  • I was using `File.ReadAllBytes`, was that not enough? – Al Wyvern Mar 16 '16 at 21:59
  • You should make it work in normal .NET application first, then “port” it to Unity. Otherwise, you never easily know if you use API in a wrong way or Unity's issues are at fault. – Maxim Kamalov Mar 16 '16 at 22:01
  • There is problems with ReadAllBytes if you are converting to a string object. A string is a two byte object and sometimes the packing of the bytes is done incorrectly. Encoding specifies how the bytes get packed into the string and the default that is often used in the Net library is Ascii encoding which removes all non printable character. When you have binary data that will create errors. I avoid putting binary data into string objects to prevent these errors. – jdweng Mar 16 '16 at 22:15
  • @MaximKamalov the API was made specifically for Unity – Al Wyvern Mar 16 '16 at 22:20
  • sharpPDF itself is intended for Windows Forms and ASP.NET as described here: http://sharppdf.sourceforge.net – Maxim Kamalov Mar 16 '16 at 22:24
  • There's a trick within the plugin that tries to use Unity's Web lib to convert image to byte array. UnityWebRequest www = UnityWebRequest.Get(path); byte[] b = www.downloadHandler.data; – Numabyte Aug 21 '20 at 05:02

1 Answers1

0
// in your poco
public string HeaderImage { get; set; } =
    Path.GetDirectoryName(Path.GetDirectoryName(
        System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName()
            .CodeBase))) + "\\assets\\your_logo.png";

// then in your function
myDoc.addImageReference(HeaderImage.Replace("file:\\", string.Empty), "Logo");
        pdfPage.addImage(myDoc.getImageReference("Logo"), 13, 720, 60, 60);
Phillip Holmes
  • 427
  • 4
  • 10