PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Jul 18, 2024 1:34 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: combine pdf and tif
PostPosted: Tue Sep 13, 2011 9:45 am 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
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
Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Mon Sep 19, 2011 2:39 pm 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
Is my program too bad?


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Mon Sep 19, 2011 3:23 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Mon Sep 19, 2011 8:55 pm 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
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


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 7:53 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 8:48 am 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
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 13621 times ]
Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 8:49 am 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
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 13621 times ]
onepage.tif
onepage.tif [ 21.04 KiB | Viewed 13621 times ]
Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 12:37 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team
Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 1:45 pm 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
Ops, when I try to attach the pdf that causes the error message, the site says that the file is not valid.


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 1:58 pm 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 2:06 pm 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
Nooo, sorry. With the new pdf file, the error occurs again: it depends on the order of the tif files!:


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 2:10 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Attach a ZIP with the PDF.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 3:14 pm 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
zip file!


Top
 Profile  
Reply with quote  
 Post subject: Re: combine pdf and tif
PostPosted: Tue Sep 20, 2011 3:16 pm 
Offline

Joined: Mon Aug 15, 2011 11:10 am
Posts: 14
zip files


Attachments:
result.zip [170.76 KiB]
Downloaded 518 times
pdffile.zip [101.32 KiB]
Downloaded 498 times
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 37 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group