gpt4 book ai didi

iTextSharp - 将新文档创建为 Byte[]

转载 作者:行者123 更新时间:2023-12-02 21:43:15 24 4
gpt4 key购买 nike

有一个小方法可以进入数据库并从 varbinary 列中检索 pdf 文档,然后向其中添加数据。我想添加代码,以便如果找不到此文档(公司文具),则会创建并返回一个新的空白文档。该方法可以返回 Byte[] 或 Stream。

问题是 else 子句中的变量“bytes”为空。

有什么问题吗?

private Byte[] GetBasePDF(Int32 AttachmentID)
{
Byte[] bytes = null;
DataTable dt = ServiceFactory
.GetService().Attachments_Get(AttachmentID, null, null);

if (dt != null && dt.Rows.Count > 0)
{
bytes = (Byte[])dt.Rows[0]["Data"];
}
else
{
// Create a new blank PDF document and return it as Byte[]
ITST.Document doc =
new ITST.Document(ITST.PageSize.A4, 50f, 50f, 25f, 25f);
MemoryStream ms = new MemoryStream();

PdfCopy copy = new PdfCopy(doc, ms);
ms.Position = 0;

bytes = ms.ToArray();

}

return bytes;
}

最佳答案

您正在尝试使用 PdfCopy 但这是针对现有文档而不是新文档。您只需要使用 PdfWriterDocument 创建一个“空白”文档。 iText 不会让您创建一个 100% 空的文档,但下面的代码基本上通过添加一个空格来实现。

private static Byte[] CreateEmptyDocument() {
using (var ms = new System.IO.MemoryStream()) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, ms)) {
doc.Open();
doc.Add(new Paragraph(" "));
doc.Close();
}
}
return ms.ToArray();
}
}

关于iTextSharp - 将新文档创建为 Byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20031982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com