c#如何使用iTextsharp从pdf返回字节数组 [英] c# How to return a byte array from pdf using iTextsharp

查看:132
本文介绍了c#如何使用iTextsharp从pdf返回字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

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

i created the following method to take in a tiff byte array with multiple tiff page document

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

i need to convert this to pdf, then return a pdf byte array

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

i have 2 problems with this code 1 - i want to RETURN a byte []. 2 - the pdf generated is repeating the pages.

    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();
    }
}

它类似于之前的答案,但在那里回答并不清楚你需要关闭() 文件实例之前您从 MemoryStream 中获取字节。在您的代码段中,您有:

It is similar to a previous answer, but in that answer in isn't made clear that you need to Close() the document instance before you get the bytes from the MemoryStream. In your code snippet, you have:

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

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

Based on the previous answer, you might change this into:

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

这是错误的,因为字节数组不包含完整的PDF。在 document.Close()时,许多基本数据被写入输出流(信息字典,根字典,交叉引用表)。

That would be wrong, because the bytes array wouldn't contain the full PDF. Upon document.Close(), a lot of essential data is written to the output stream (the info dictionary, the root dictionary, the cross-reference table).

更新:

在C#中,可以自定义使用使用,如评论中所示:

In C#, it is custom to use using as indicated in the comments:

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();
    }
}

我的论据是文件需要关闭才能使完整的PDF保持有效:} 文档实例c $ c>在之前返回ms.ToArray()

My argument that the document needs to be closed to get a complete PDF remains valid: the document instance is closed implicitly by the } right before return ms.ToArray().

这篇关于c#如何使用iTextsharp从pdf返回字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆