c# 如何使用 iTextsharp 从 pdf 返回字节数组

标签 c# pdf itext tiff

全部,

我创建了以下方法来接收包含多个 tiff 页面文档的 tiff 字节数组

我需要将其转换为pdf,然后返回一个pdf字节数组

这段代码有两个问题 1 - 我想返回一个字节[]。 2 - 生成的 pdf 重复页面。

    public void convertImage(byte[] documentContent)
    {
        Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);

        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Data\Output.pdf", FileMode.Create)); --for testing purposes



        Bitmap oldImage;

        using (var ms = new MemoryStream(documentContent))
        {
            oldImage = new Bitmap(ms);
        }


        Size newSize = new Size(1024, 737);


        using (Bitmap bmp1 = new Bitmap(oldImage, newSize))
        {
            int total = oldImage.GetFrameCount(FrameDimension.Page);

            document.Open();

            PdfContentByte cb = writer.DirectContent;

            for (int k = 0; k < total; ++k)
            {
                bmp1.SelectActiveFrame(FrameDimension.Page, k);

                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bmp1, ImageFormat.Bmp);

                var scaleparcent = 72f / img.DpiX * 100;

                img.ScalePercent(scaleparcent);

                img.ScaleAbsoluteHeight(document.PageSize.Height);
                img.ScaleAbsoluteWidth(document.PageSize.Width);

                img.SetAbsolutePosition(0, 0);

                cb.AddImage(img);

                document.NewPage();

            }
        }

        byte[] bytes = null;

        document.Close();
    }

有人帮忙吗?

最佳答案

这是一个基本的例子:

private byte[] CreatePdf()
{
    Document document = new Document();
    using (MemoryStream ms = new MemoryStream())
    {
        PdfWriter.GetInstance(document, ms);
        document.Open();
        document.Add(new Paragraph("Hello World"));
        document.Close();
        return ms.ToArray();
    }
}

它类似于以前的答案,但在那个答案中并没有明确说明您需要 Close() document 实例 MemoryStream 中获取字节。在您的代码片段中,您有:

byte[] bytes = null;
document.Close();

根据之前的答案,您可以将其更改为:

byte[] bytes = ms.ToArray();
document.Close();

那是错误的,因为 bytes 数组不会包含完整的 PDF。 document.Close() 后,大量重要数据被写入输出流(信息字典、根字典、交叉引用表)。

更新:

在 C# 中,习惯使用 using,如注释中所示:

private byte[] CreatePdf()
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (Document document = new Document())
        {
            PdfWriter.GetInstance(document, ms);
            document.Open();
            document.Add(new Paragraph("Hello World"));
        }
        return ms.ToArray();
    }
}

我关于需要关闭 document 才能获得完整 PDF 的论点仍然有效:document 实例由 隐式关闭就在 return ms.ToArray() 之前。

关于c# 如何使用 iTextsharp 从 pdf 返回字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095802/

相关文章:

c# - 从 double 转换为十进制时避免 OverflowException

c# - 编辑 Xml 文件

java - iText PDF 文档无法打开 : java. io.IOException : No message found for the. document.has.no.pages

html - PDF 作为 HTML 中的空白页

java - IndexOutofBoundException 为什么?

c# - Entity Framework 中匿名类型的返回列表

c# - 工厂设计模式(需要批判)

python - 如何使用 Python 提取与内容相关的所有 PDF 标签?

Java 注册提供者抛出 EOFException

c# - 如何替换字节