PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

combine pdf and tif
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1782
Page 1 of 1

Author:  ginopittaro [ Tue Sep 13, 2011 9:45 am ]
Post subject:  combine pdf and tif

I have a problem creating a new pdf from a pdf file and tiff files:
1) I insert 1 or 2 pages from a pdf file.
2) Then I insert other pages from tiff files (some tiff have only 1 page, others 2 pages)
- If I insert only tiff files with 1 page: OK
- If I insert only tiff files with 2 pages: OK
- If I insert a tiff file with 1 page and then a tiff file with 2 page: OK
- If I insert a tiff file with 2 pages and then a tiff file with 1 page: ERROR ('not enough space for an image).
Here is the code (thanks):


Code:
private Boolean TeuthTif()
{
    try
    {

        int numeroPagine;

        // Nuovo Output per il documento
        PdfDocument outputDocument = new PdfDocument();

        if (_pdfPrimo == "S")
        {

            //devo creare il documento
            // Singola pagina!
            outputDocument.PageLayout = PdfPageLayout.SinglePage;
            // Dati base 
            XFont font = new XFont("Verdana", 8, XFontStyle.Bold);
            XStringFormat format = new XStringFormat();
            format.Alignment = XStringAlignment.Center;
            format.LineAlignment = XLineAlignment.Far;

            outputDocument.PageLayout = PdfPageLayout.SinglePage;


        }
        else
        {
            //punto al  documento originale e aggiungo le pagine successive
            // Apro documento Ori
            // Nuovo Output per il documento
            PdfDocument inputDocumentOri = PdfReader.Open(_pdfOri, PdfDocumentOpenMode.Modify);

            outputDocument = inputDocumentOri;
        }


        //inserisco il pdf Teuth
        XPdfForm formTeuth = XPdfForm.FromFile(_pdf1);

        numeroPagine = formTeuth.PageCount;
        for (int pagina = 1; pagina <= numeroPagine; pagina++)
        {

            // Aggiungo una pagina sul nuovo documento
            PdfPage page = outputDocument.AddPage();
            page.Size = PageSize.A4;

            double width = page.Width;
            double height = page.Height;

            XGraphics gfx;
            XRect box;

            gfx = XGraphics.FromPdfPage(page);
            box = new XRect(0, 0, width, height);
            // Disegno la pagina come immagine
            formTeuth.PageNumber = pagina;
            gfx.DrawImage(formTeuth, box);

        }


        // passo i .tif del teuth
        string[] files = GetFiles();
        foreach (string file in files)
        {
            // Open the document to import pages from it.
            Bitmap bm = new Bitmap(file);
            int total = bm.GetFrameCount(FrameDimension.Page);
            MessageBox.Show(total.ToString());
            if (total == 1)
            {
                Bitmap bm2 = new Bitmap(file);
                XImage image = XImage.FromGdiPlusImage(bm2);
                PdfPage page1 = outputDocument.AddPage();
                page1.Size = PageSize.A4;

                XGraphics gfxT = XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append);
                gfxT.DrawImage(image, 0, 0, page1.Width, page1.Height);
                gfxT.Dispose();
                image.Dispose();
                page1.Close();
                bm2.Dispose();

            }
            else
            {

                for (Int32 k = 0; k < total; k++)
                {
                    Bitmap bm2 = new Bitmap(file);
                    bm2.SelectActiveFrame(FrameDimension.Page, k);
                    XImage image = XImage.FromGdiPlusImage(bm2);
                    PdfPage page1 = outputDocument.AddPage();
                    //
                    // Get Image width, Height and Resolution and Set output document Width and Height in Inches
                    //page1.Width = XUnit.FromInch(image.Width / image.HorizontalResolution);
                    //page1.Height = XUnit.FromInch(image.Height / image.VerticalResolution);

                    page1.Size = PageSize.A4;

                    XGraphics gfxT = XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append);
                    //gfxT.DrawImage(image, 0, 0);
                    gfxT.DrawImage(image, 0, 0, page1.Width, page1.Height);
                    gfxT.Dispose();
                    image.Dispose();
                    page1.Close();
                    bm2.Dispose();
                }
            }
        }


        // Salvo il documento
        outputDocument.Save(_pdfOri);

    }


    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Anomalia in esecuzione TifPdf", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }



    return true;
}


Attachments:
tt.7z [207.36 KiB]
Downloaded 525 times

Author:  ginopittaro [ Mon Sep 19, 2011 2:39 pm ]
Post subject:  Re: combine pdf and tif

Is my program too bad?

Author:  Thomas Hoevel [ Mon Sep 19, 2011 3:23 pm ]
Post subject:  Re: combine pdf and tif

ginopittaro wrote:
Is my program too bad?

I don't know. I don't understand too much Italian (?) comments and I cannot expand .7z files.

What sort of error do you get? An Exception? If so, it would help if you would post exception details here.
Do you get the same error with both the GDI+ and the WPF build?

Author:  ginopittaro [ Mon Sep 19, 2011 8:55 pm ]
Post subject:  Re: combine pdf and tif

1) I insert 1 or 2 pages from a pdf file.
2) Then I insert other pages from tiff files (some tiff have only 1 page, others 2 pages)
- If I insert only tiff files with 1 page: OK
- If I insert only tiff files with 2 pages: OK
- If I insert a tiff file with 1 page and then a tiff file with 2 page: OK
- If I insert a tiff file with 2 pages and then a tiff file with 1 page: ERROR ('not enough space for an image).

Sorry for 7zip files, tomorrow I Try to send .zip files

Author:  Thomas Hoevel [ Tue Sep 20, 2011 7:53 am ]
Post subject:  Re: combine pdf and tif

What sort of error do you get? An Exception? If so, it would help if you would post exception details here.
Do you get the same error with both the GDI+ and the WPF build?

Author:  ginopittaro [ Tue Sep 20, 2011 8:48 am ]
Post subject:  Re: combine pdf and tif

1) I insert 1 or 2 pages from a pdf file.
2) Then I insert other pages from tiff files (some tiff have only 1 page, others 2 pages)
- If I insert only tiff files with 1 page: OK
- If I insert only tiff files with 2 pages: OK
- If I insert a tiff file with 1 page and then a tiff file with 2 page: OK
- If I insert a tiff file with 2 pages and then a tiff file with 1 page: ERROR ('not enough space for an image).


When I open the pdf file, Acrobat gives me the message: "Not enough space for an image".
I attache the .tif files

Attachments:
onepage.tif
onepage.tif [ 21.04 KiB | Viewed 13618 times ]

Author:  ginopittaro [ Tue Sep 20, 2011 8:49 am ]
Post subject:  Re: combine pdf and tif

1) I insert 1 or 2 pages from a pdf file.
2) Then I insert other pages from tiff files (some tiff have only 1 page, others 2 pages)
- If I insert only tiff files with 1 page: OK
- If I insert only tiff files with 2 pages: OK
- If I insert a tiff file with 1 page and then a tiff file with 2 page: OK
- If I insert a tiff file with 2 pages and then a tiff file with 1 page: ERROR ('not enough space for an image).


When I open the pdf file, Acrobat gives me the message: "Not enough space for an image".
I attache the .tif files

Thanks a lot!

Attachments:
twopages.tif
twopages.tif [ 51.29 KiB | Viewed 13618 times ]
onepage.tif
onepage.tif [ 21.04 KiB | Viewed 13618 times ]

Author:  Thomas Hoevel [ Tue Sep 20, 2011 12:37 pm ]
Post subject:  Re: combine pdf and tif

I used your code and your TIFF files to create a PDF - and Adobe Reader opens it without error message.

I tried Adobe Reader X and Adobe Acrobat 8.

Please find the file attached to this post.

Please upload a PDF that causes this error for further investigation.

Attachments:
new_pdf_by_thho.zip [70.8 KiB]
Downloaded 500 times

Author:  ginopittaro [ Tue Sep 20, 2011 1:45 pm ]
Post subject:  Re: combine pdf and tif

Ops, when I try to attach the pdf that causes the error message, the site says that the file is not valid.

Author:  ginopittaro [ Tue Sep 20, 2011 1:58 pm ]
Post subject:  Re: combine pdf and tif

Mmmmmmmmmmmmmm, thanks for your help. I tried to insert another pdf file before the tiff files, and it works!
Then I think the problem is my pdf file (a pdf file create by our programs). I tried to attach my pdf file but the upload says that my file is not valid.

Author:  ginopittaro [ Tue Sep 20, 2011 2:06 pm ]
Post subject:  Re: combine pdf and tif

Nooo, sorry. With the new pdf file, the error occurs again: it depends on the order of the tif files!:

Author:  Thomas Hoevel [ Tue Sep 20, 2011 2:10 pm ]
Post subject:  Re: combine pdf and tif

Attach a ZIP with the PDF.

Author:  ginopittaro [ Tue Sep 20, 2011 3:14 pm ]
Post subject:  Re: combine pdf and tif

zip file!

Author:  ginopittaro [ Tue Sep 20, 2011 3:16 pm ]
Post subject:  Re: combine pdf and tif

zip files

Attachments:
result.zip [170.76 KiB]
Downloaded 518 times
pdffile.zip [101.32 KiB]
Downloaded 498 times

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/