【问题标题】:c# How to return a byte array from pdf using iTextsharpc#如何使用iTextsharp从pdf返回一个字节数组
【发布时间】:2016-05-08 03:08:54
【问题描述】:

全部,

我创建了以下方法来接收具有多个 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();
    }

有人帮忙吗?

【问题讨论】:

  • 我在您的代码中没有看到您告诉它返回字节数组的任何地方。你必须告诉它。

标签: c# pdf itextsharp tiff


【解决方案1】:

这是一个基本的例子:

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 获取字节之前.在您的代码 sn-p 中,您有:

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

根据前面的回答,你可以改成:

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

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

更新:

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

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() 之前被} 隐式关闭。

【讨论】:

  • 您应该将Document 放在using 中,在MemoryStream 被实例化的块内。 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(); } }
  • 谢谢,但我不完全明白。在我的代码中,我使用的是包含字节数组中的 tiff 图像的文档内容。在我的代码中我需要改变什么
  • @kayze 您的评论没有意义:TIFF 与这个关于 PDF 的问题有什么关系? documentcontent 是什么?不要劫持已回答的问题来发布新问题
猜你喜欢
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 2013-01-16
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多