Unity 3d SharpPdf C#生成和显示

时间:2015-08-10 06:51:18

标签: c# pdf unity3d multiplatform sharppdf

我试图找出如何在Unity应用中生成和显示pdf。 我希望该应用程序和pdf可用于iOS,Android和Web应用程序。 按照本教程,我能够生成一个pdf文件并存储在模拟器中的app主文件夹中。

http://www.devindia.biz/unity-pdf-generation-with-sharppdf-plugin/

不幸的是,当我尝试使用

打开应用程序内的pdf时
Application.OpenURL("FILE://" + path);

它不起作用。

我也尝试过iTextSharp结果不佳,我相信它不再是免费的。

任何建议都非常受欢迎。

感谢名单

更新

所以我试图向前迈进,但我有点卡在中间:我尝试了Gallorini改进的SharpPDF程序,生成的pdf文档,我打电话给#34; NewPDF.pdf&#34 ;存储在主应用程序文件夹中。确实,当我在编辑器中运行代码时,我能够在那里找到它。 我构建了一个简单的gui按钮,并为其运行此c#代码附加了一个空格,以显示该文档作为您的建议:

public void openPdf(){
string path = Application.persistentDataPath + "/" + "newPDF.pdf";
Application.OpenURL(path);
Debug.log("Button Clicked");

}

当我点击按钮时,除了当然控制台记录按钮单击时没有任何反应。

当我为网络播放器构建程序时,虽然ApplicationOpenURL正常工作,但我没有生成pdf,而且我能够通过编辑器显示以前生成的pdf ....我有点困惑。

当我为Mac Osx构建程序时,根本没有任何事情发生。

在iPhone上,我从Xcode控制台获取此日志:

-> applicationDidBecomeActive()
Requesting Resolution: 1920x1080
Init: screen size 1920x1080
Initializing Metal device caps
Initialize engine version: 5.1.2f1 (afd2369b692a)

Adding Image Length 0 HEIGTH :8 WIDTH :8
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
sharpPDF.c__Iterator0:MoveNext()

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)

UnauthorizedAccessException: Access to the path "/newPDF.pdf" is denied.
at Mono.Security.Cryptography.RSAManaged.GenerateKeyPair () [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in :0
at sharpPDF.pdfDocument.createPDF (System.String outputFile) [0x00000] in :0
at SimplePDF+c__Iterator1.MoveNext () [0x00000] in :0

(Filename: currently not available on il2cpp Line: -1)

Adding Image Length 0 HEIGTH :8 WIDTH :8
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
sharpPDF.c__Iterator0:MoveNext()

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)

UnauthorizedAccessException: Access to the path "/newPDF.pdf" is denied.
at Mono.Security.Cryptography.RSAManaged.GenerateKeyPair () [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0
at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in :0
at sharpPDF.pdfDocument.createPDF (System.String outputFile) [0x00000] in :0
at SimplePDF+c__Iterator1.MoveNext () [0x00000] in :0

(Filename: currently not available on il2cpp Line: -1)

Button Clicked
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
SimplePDF:openPdf()
UnityEngine.Events.UnityAction:Invoke()
UnityEngine.Events.InvokableCall:Invoke(Object[])
UnityEngine.Events.InvokableCallList:Invoke(Object[])
UnityEngine.Events.UnityEventBase:Invoke(Object[])
UnityEngine.Events.UnityEvent:Invoke()
UnityEngine.UI.Button:Press()
UnityEngine.UI.Button:OnPointerClick(PointerEventData)
UnityEngine.EventSystems.ExecuteEvents:Execute(IPointerClickHandler, BaseEventData)
UnityEngine.EventSystems.EventFunction`1:Invoke(ICancelHandler, BaseEventData)
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.TouchInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
UnityEngine.EventSystems.TouchInputModule:ProcessTouchEvents()
UnityEngine.EventSystems.TouchInputModule:Process()
UnityEngine.EventSystems.EventSystem:Update()

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)

我完全糊涂了。你能帮帮我吗?

再次谢谢。

更新#2

我部分解决了这个问题:

我能够在Unity Editor和OSX上的独立应用程序中生成.pdf文件。我意识到我在Unity中存储/加载文件时遇到了麻烦。

// this is working on Editor. Application.dataPath are reached by the application and files are stored in Assets/StreamingAssets folder under project in Editor     

    #if UNITY_EDITOR || UNITY_EDITOR_64 
        myDoc.createPDF(Application.dataPath + "/StreamingAssets/" + attacName);


        myTable = null;

        #endif

// This is working on Mac standalone application. The file is correctly saved under Application.app/Contents folder

  #if UNITY_STANDALONE

    myDoc.createPDF(Application.dataPath + "/" + attacName);
    myTable = null;
        #endif

检索并打开文件的代码:

public void openPdf() {
    string path;

        // tested working on mac

        #if UNITY_EDITOR || UNITY_EDITOR_64


        path = "file:" +Application.dataPath + "/StreamingAssets/" + attacName;


        #endif

        #if UNITY_STANDALONE

         path = "file:" +Application.dataPath + "/" + attacName;

        #endif

        Application.OpenURL(path);
        Debug.Log ("Button Clicked");

    }

到目前为止,一切都运作良好。

痛苦来自ios。

我能够使用Application.persistentDataPath中的上述代码生成一个pdf文件,并从iTunes中获取它(以验证它已创建),在info.plist下启用UIFileSharingEnabled。这是代码:

   #if UNITY_IOS 
      myDoc.createPDF(Application.persistentDataPath + "/" + attacName);
      myTable = null;
    #endif

问题是当我尝试Application.OpenUrl(path)时。似乎c#代码对Application.persistentDataPath是盲目的。顺便提一句是/var/mobile/Containers/Data/Application/"ApplicationName"/Documents/"pdfName".pdf使用

path = "file://" + Application.persistentDataPath + "/" + attacName;
    Application.OpenURL(path);

我还尝试将文件保存在StreamingAssets文件夹中,但Xcode并不喜欢它:

UnauthorizedAccessException:访问路径" /private/var/mobile/Containers/Bundle/Application/F105683E-B2EA-4B0C-81B7-61302C58D56C/PdfReader.app/Data/newPDF.pdf"被拒绝。   在Mono.Security.Cryptography.RSAManaged.GenerateKeyPair()[0x00000] in:0   在System.IO.FileStream..ctor(System.String路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,布尔匿名,FileOptions选项)[0x00000] in:0   在System.IO.FileStream..ctor(System.String路径,FileMode模式)[0x00000] in:0   在sharpPDF.pdfDocument.createPDF(System.String outputFile)[0x00000] in:0   在simplepdf + c__Iterator1.MoveNext()[0x00000] in:0

(文件名:目前在il2cpp Line上不可用:-1)

然后我决定使用这个包装器

https://www.assetstore.unity3d.com/en/#!/content/17591

但它只能将已格式化的pdf文件从StreamingAssets文件夹复制到iOS设备上的/ Raw文件夹。

我想我必须编写自己的包装器来从iOS上的Application.persistentDataPath加载数据,你有什么建议可以开始吗?

再次感谢你。

0 个答案:

没有答案