Converting documents to PDF and printing them to fit pages

Product:PDFtron SDK for c# / WinForms

Product Version: Current

We are currently developing an application whose single task it is to convert all the attachments received in a single mail into a single PDF file and then to print this PDF file. The attachments may be diffent file types like doc(x), JPG, GIF and also PDF itself.

The problem we are facing occurs when we ary trying to convert JPEG files to PDF. Most of the JPG files we receive have been created by using some kind of scanner and have approximately 3.000 x 2.000 pixel per page (Letter/A4).

Please see the following code snippet:

                foreach (Outlook.Attachment receivedAttachment in receivedMail.Attachments)
                {
                    string FileName = "", Extension = "";
                    FileName = receivedAttachment.FileName;
                    Extension = Path.GetExtension(FileName).ToUpper();
                    if ((Extension == ".TXT") || (Extension == ".DOC") || (Extension == ".DOCX") || (Extension == ".PDF") || (Extension == ".JPG") || (Extension == ".TIF") ||
                            (Extension == ".TIFF") || (Extension == ".ODT"))
                    {
                        // Datei mit zulässiger Extension
                        if (newMail.PdfFileName == "")
                        {
                            pdfFileName = "BU-" + uniqueID;
                            newMail.PdfFileName = @"c:\temp\" + pdfFileName + @".PDF";
                        }

                        attachmentCounter++;

                        // Leere PDF anlegen
                        //if (attachmentCounter == 1)
                        //    doc = new PDFDoc();

                        // Mailanhang in Datei kopieren
                        receivedAttachment.SaveAsFile("c:\\temp\\" + FileName);

                        pdftron.PDF.Convert.ToPdf(doc, "c:\\temp\\" + FileName);

                        File.Delete("c:\\temp\\" + FileName);
                    }
                    else
                    {
                        // ToDo: Warnung bei nicht konvertierbarem Anhang erzeugen
                    }
                }

When looking at the PDF file that has been generated with Adobe reader we see that not each of the attachments is being placed on a new page - which is what we would like to get. If the source document (e., g. JPG file) doesn’t fit on a page, we’d like to have it fitted on a page.

After collecting all attachments an having them inserted into a PDF file using the code snippet from above, we try to print the PDF document created using the following code:

        if (File.Exists (FileName))
        using (pdfdoc = new PDFDoc(FileName))
        {
            pdfdoc.InitSecurityHandler();
            PrinterMode printerMode = new PrinterMode();
            printerMode.SetAutoCenter(true);
            printerMode.SetAutoRotate(true);
            printerMode.SetCollation(true);
            printerMode.SetCopyCount(1);
            printerMode.SetDPI(600); // regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
            printerMode.SetDuplexing(PrinterMode.DuplexMode.e_Duplex_None); // Kein Duplexdruck
            printerMode.SetNUp(PrinterMode.NUp.e_NUp_1_1, PrinterMode.NUpPageOrder.e_PageOrder_LeftToRightThenTopToBottom);
            printerMode.SetOrientation(PrinterMode.Orientation.e_Orientation_Portrait);
            printerMode.SetOutputAnnot(PrinterMode.PrintContentTypes.e_PrintContent_DocumentAndAnnotations);

            // If the XPS print path is being used, then the printer spooler file will
            // ignore the grayscale option and be in full color
            printerMode.SetOutputColor(PrinterMode.OutputColor.e_OutputColor_Grayscale);
            printerMode.SetOutputPageBorder(false);
            printerMode.SetOutputQuality(PrinterMode.OutputQuality.e_OutputQuality_Medium);
            printerMode.SetPaperSize(new Rect(0, 0, 612, 792));
            PageSet pagesToPrint = new PageSet(1, pdfdoc.GetPageCount(), PageSet.Filter.e_all);

            // You can get the name of the default printer by using:
            // PrinterSettings ps = new PrinterSettings();
            // String printerName ps.PrinterName();
            // however Print.StartPrintJob can also determine this for you, just pass an empty printer name 

            // Print the document on the default printer, name the print job the name of the 
            // file, print to the printer not a file, and use printer options:
            Print.StartPrintJob(pdfdoc, printerName, pdfdoc.GetFileName(), "", pagesToPrint, printerMode, null);
        }

Those parts of the resulting document that have been generated from “oversize” JPG images are now being printed largely oversize, for example one single JPG image takes 2 x 3 pages to print.

So, what we like to get is: An import/conversion that truly fits a JPG (or some other kind of image) file to a single, newly created, page of the pDF document and we’d like to get a printout where each page of the PDF document fits exactly one page on the printer output.

I think this expectation is rather intuitive - we’re just looking fpr away to get it accomplished.

Kind regards

Axel

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

If the source document (e., g. JPG file) doesn’t fit on a page, we’d like to have it fitted on a page.

The API pdftron.PDF.Convert.ToPdf when given an image will check the image metadata/EXIF data, and generate a PDF page that matches the DPI specified in the image metadata. If nothing is specified, then we use the PDF default 72 DPI. So unless your “3.000 x 2.000” image has metadata, then by default it would be about 41"x27". Either way it would always be on one page. The only time you would see multiple pages with that API is if you pass in a multi-image TIFF.

If you want to customize the image to PDF calculations (like you want every image to be mapped to A4 page size, then replace ToPdf with code from this sample.

for example one single JPG image takes 2 x 3 pages to print.
As far as I am aware, this is not possible with our ToPdf.

An import/conversion that truly fits a JPG (or some other kind of image) file to a single, newly created, page of the pDF document
Yes, this is what that ToPdf does.

If the above information does not answer your question, then to diagnose further please provide the following.

  1. Input file(s)
  2. Generated PDF
  3. Code used to generate (2) from (1)