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に保存してみる
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下にこのように保存されます。
まとめ
ゲームでPDFの書き出しはあまりやらないかもしれないですが、Unityをゲーム以外の目的で用いている人にとっては便利なライブラリかと思います。