4

All,

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

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

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

some one help please?

1
  • I don't see anywhere in your code where you told it to return a byte array. You have to tell it that. Commented May 8, 2016 at 3:13

1 Answer 1

8

This is a basic example:

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

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

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).

Update:

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

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().

Sign up to request clarification or add additional context in comments.

3 Comments

You should put the Document in a using inside the block where the MemoryStream is instantiated. 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(); } }
Thanks but i dont get it exactly. In my code i am using the documentcontent which contains tiff images in byte array. what in my code do i have to change
@kayze your comment doesn't make sense: what does TIFF have to do with this question about PDF? What is documentcontent? Do not hijack an answered question to post a new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.