如何在 unity c# 中使用 itext7 将图像添加到 pdf?
How do I add image to pdf using itext7 in unity c#?
我正在尝试使用 itext7向我的 pdf 文档添加图像,但我收到 DirectoryNotFoundException。
我使用的是有效的 itextsharp,但 itext7 的情况并非如此,Whosebug 的一些类似问题向我展示了它是如何完成的,但它没有读取 Assets 文件夹 中的文件.这是我的代码:
var exportFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var exportFile = System.IO.Path.Combine(exportFolder, "Myfile.pdf");
//Create Writer
var writer = new PdfWriter(exportFile);
//Create Pdf Document Object
var pdf = new PdfDocument(writer);
//Create A Document
Document document = new Document(pdf, PageSize.A4);
//Add Cover Page 1
ImageData imagedata = ImageDataFactory.Create("Ganesh.jpg");
iText.Layout.Element.Image coverImage = new iText.Layout.Element.Image(imagedata);
document.Add(coverImage);
document.Add(new iText.Layout.Element.AreaBreak());
document.Close();
当我 运行 此代码带有按钮单击事件时,它抛出此错误:
DirectoryNotFoundException: Could not find a part of the path "C:\Users\Public\Documents\LO%20SHU%20GRID%20Windows\Ganesh.jpg".
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <7ba07f088431485bb722f3b3373e87ee>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) (at <7ba07f088431485bb722f3b3373e87ee>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) (at <7ba07f088431485bb722f3b3373e87ee>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
iText.IO.Util.UrlUtil.OpenStream (System.Uri url) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.ReadImageType (System.Uri source) (at <57da1b8d8a184e278c732544ebe6412a>:0)
Rethrow as IOException: io.exception
iText.IO.Image.ImageDataFactory.ReadImageType (System.Uri source) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.CreateImageInstance (System.Uri source, System.Boolean recoverImage) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.Create (System.Uri url, System.Boolean recoverImage) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.Create (System.String filename, System.Boolean recoverImage) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.Create (System.String filename) (at <57da1b8d8a184e278c732544ebe6412a>:0)
Panel.EnglishPdf () (at Assets/Scripts/Panel.cs:5755)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
如果有好心人能帮我解决这个错误,我将不胜感激。
我通过将图像转换为字节数组来修复它。谢谢。
我正在尝试使用 itext7向我的 pdf 文档添加图像,但我收到 DirectoryNotFoundException。 我使用的是有效的 itextsharp,但 itext7 的情况并非如此,Whosebug 的一些类似问题向我展示了它是如何完成的,但它没有读取 Assets 文件夹 中的文件.这是我的代码:
var exportFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var exportFile = System.IO.Path.Combine(exportFolder, "Myfile.pdf");
//Create Writer
var writer = new PdfWriter(exportFile);
//Create Pdf Document Object
var pdf = new PdfDocument(writer);
//Create A Document
Document document = new Document(pdf, PageSize.A4);
//Add Cover Page 1
ImageData imagedata = ImageDataFactory.Create("Ganesh.jpg");
iText.Layout.Element.Image coverImage = new iText.Layout.Element.Image(imagedata);
document.Add(coverImage);
document.Add(new iText.Layout.Element.AreaBreak());
document.Close();
当我 运行 此代码带有按钮单击事件时,它抛出此错误:
DirectoryNotFoundException: Could not find a part of the path "C:\Users\Public\Documents\LO%20SHU%20GRID%20Windows\Ganesh.jpg".
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <7ba07f088431485bb722f3b3373e87ee>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) (at <7ba07f088431485bb722f3b3373e87ee>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) (at <7ba07f088431485bb722f3b3373e87ee>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
iText.IO.Util.UrlUtil.OpenStream (System.Uri url) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.ReadImageType (System.Uri source) (at <57da1b8d8a184e278c732544ebe6412a>:0)
Rethrow as IOException: io.exception
iText.IO.Image.ImageDataFactory.ReadImageType (System.Uri source) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.CreateImageInstance (System.Uri source, System.Boolean recoverImage) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.Create (System.Uri url, System.Boolean recoverImage) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.Create (System.String filename, System.Boolean recoverImage) (at <57da1b8d8a184e278c732544ebe6412a>:0)
iText.IO.Image.ImageDataFactory.Create (System.String filename) (at <57da1b8d8a184e278c732544ebe6412a>:0)
Panel.EnglishPdf () (at Assets/Scripts/Panel.cs:5755)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
如果有好心人能帮我解决这个错误,我将不胜感激。
我通过将图像转换为字节数组来修复它。谢谢。