LoginSignup
5
11

More than 5 years have passed since last update.

UnityでiTextSharpを使ってPDFを作成する

Last updated at Posted at 2017-12-12

UnityでPDFを生成する必要があったので、それに用いたiTextSharpというライブラリの導入方法を説明します。

iTextSharpのダウンロード

iTextSharpはAGPL-3.0 Licenseの元、無料で使えるC#用のPDF生成に使えるライブラリです。Unity 5.6.4(Mac版)で試したところ、こちら問題なく使えるようでした。

下記のNuGetのページからManual downloadリンクをクリックして、.nupkgファイルをダウンロードします。

iTextSharp
itextsharp.xmlworker
itextsharp.pdfa
itextsharp.xtra

ダウンロードした.nupkgの拡張子を.zipに変更し、解凍し、それぞれのパッケージからdllを取り出します。

Unityにインポートする

Pluginsフォルダを作り、そこに取り出した4つのdll (itextsharp.dll, itextsharp.pdfa.dll, itextsharp.xmlworker.dll, itextsharp.xtra.dll)をいれます。

スクリーンショットをPDFに保存してみる

PDFTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System;

public class PDFTest : MonoBehaviour {

    public Camera mainCam;
    private Texture2D capturedTex;


    public void Start(){
        GeneratePDF();
    }


    public void GeneratePDF () {
        string path = Application.persistentDataPath + "/screenshot-"+System.DateTime.Now.ToString("yyy-MM-dd_HH-mm-ss")+".pdf";
        StartCoroutine(CreatePDF(path));
    }

    // create pdf to specific path
    public IEnumerator CreatePDF (string path) {
        Document doc = new Document(PageSize.A4.Rotate()); 

        var writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None));


        doc.Open();
        doc.NewPage();

        yield return StartCoroutine(TakeScreenshot(mainCam, Screen.width, Screen.height));


        AddImageToPDF(writer, capturedTex, 0, 0);


        doc.Close();

    }

    // coroutine to take screenshot
    public IEnumerator TakeScreenshot (Camera cam, int width, int height) {

        RenderTexture rt = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
        rt.antiAliasing = 8; 
        cam.targetTexture = rt;
        cam.Render();

        yield return new WaitForEndOfFrame();

        RenderTexture.active = rt;

        capturedTex = new Texture2D(width, height, TextureFormat.RGB24, false);
        capturedTex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        capturedTex.Apply();

        cam.targetTexture = null;
    }

    // add image to pdf
    public void AddImageToPDF(PdfWriter pdfWriter, Texture2D img, float posX, float posY){
        byte[] imageBytes = img.EncodeToPNG();
        iTextSharp.text.Image finalImage = iTextSharp.text.Image.GetInstance(imageBytes);
        finalImage.ScaleAbsolute(PageSize.A4.Rotate());
        finalImage.SetAbsolutePosition(posX, posY);

        var pdfContentByte = pdfWriter.DirectContent;
        pdfContentByte.AddImage(finalImage);
    }


}

PDFはpersistentDataPath下にこのように保存されます。

Screen Shot 2017-12-12 at 17.30.00.png

まとめ

ゲームでPDFの書き出しはあまりやらないかもしれないですが、Unityをゲーム以外の目的で用いている人にとっては便利なライブラリかと思います。

5
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
11