You are viewing limited content. For full access, please sign in.

Question

Question

Need help with a Workflow Script - Set size of exported pages

asked on January 2, 2020

I created a script to export PDFs for a customer, and they've requested the pages be scaled to 8.5x11 when the PDF export happens. There are documents where (strangely) the different pages are of different sizes, so the first page is 8.5x11, but second page and a few others are smaller or larger than that.

I found a property for DocumentExporter that sets the page size, and the enumeration for 8.5x11, but it doesn't seem to be working. The pages are all still different sizes.

 

namespace WorkflowActivity.Scripting.ScriptToExportPDFOfDocument
{
    using System;
    using Laserfiche.RepositoryAccess;
    using Laserfiche.DocumentServices;

    /// <summary>
    /// Provides one or more methods that can be run when the workflow scripting activity is performed.
    /// </summary>
    public class PdfExport : RAScriptClass102
    {
        /// <summary>
        /// This method is run when the activity is performed.
        /// </summary>
        protected override void Execute()
        {
            //Use the full PDF file path created in PDF Full Export FIle Path token
            string fullFilePath = GetTokenValue("PDF Full Export File Path").ToString();

            //Create Document Info object to feed current entry
            DocumentInfo sDoc = (DocumentInfo)this.BoundEntryInfo;

            //Lock Current Entry
            //sDoc.Lock(LockType.Exclusive);

            //Create a document exporter
            DocumentExporter dExp = new DocumentExporter();
            dExp.IncludeAnnotations = true;
            dExp.PdfPageSize = 0;

           //Export the PDF
            try
            {
               dExp.ExportPdf(sDoc, sDoc.AllPages,PdfExportOptions.RenderAnnotationsAsImage, fullFilePath);
            }
            catch (Exception ex)
            {
               throw new LaserficheRepositoryException ("Pdf export script failed to export the PDF.", ex);
            }

            //Release the Source Document
            //sDoc.Unlock();
            //sDoc.Dispose();
        }
    }
}

 

0 0

Answer

SELECTED ANSWER
replied on January 2, 2020 Show version history

It isn't really clarified in the documentation, but I believe I read somewhere on Answers that the PdfPageSize property only applies when exporting text-only pages (i.e., no image). The page size for exported images is determined by the pixel dimensions and resolution (DPI) so that setting wouldn't change anything.

1 0

Replies

replied on January 6, 2020

For anyone interested I found a solution to the issue. I had to shrink the pages of the source documents themselves before the PDF export was done. Fortunately, the customer was willing to accept a change to the source documents themselves.

Here is the C# code: 

namespace WorkflowActivity.Scripting.SDKScriptToShrinkPagestoBriefcaseArchiveFolder
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using System.IO;
    using Laserfiche.RepositoryAccess;
    using Laserfiche.DocumentServices;

    /// <summary>
    /// Provides one or more methods that can be run when the workflow scripting activity is performed.
    /// </summary>
    public class Script1 : RAScriptClass102
    {
        /// <summary>
        /// This method is run when the activity is performed.
        /// </summary>
        protected override void Execute()
        {
            // RASession will get the Repository Access session

            // set vars for page size conversion
            // Page width measurements
            decimal pageWidthWanted = 8.5M;
            decimal pageXRes = 0;
            decimal pageWidth = 0;
            decimal pageInchWidth = 0;
            int pageWidthScale = 0;

            // Page height measurements
            decimal pageHeightWanted = 11.0M;
            decimal pageYRes = 0;
            decimal pageHeight = 0;
            decimal pageInchHeight = 0;
            int pageHeightScale = 0;

            // Final amount to scale page by
            int scalePageTo = 10000;

            //get the document info for the current entry. This is the entry to export
            DocumentInfo docInfo = (DocumentInfo)this.BoundEntryInfo;

            //Create a Document Exporter
            DocumentExporter docExporter = new DocumentExporter();
            docExporter.IncludeAnnotations = true;
            docExporter.AllowInterpolation = true;
            docExporter.PageFormat = DocumentPageFormat.Tiff;

            //Iterate through each page of the document.
            for (int i = 1; i <= docInfo.PageCount ; i++)
            {
                //Get pageinfo of current page 1,2,3,4,etc...
                PageInfo currentPage = docInfo.GetPageInfo(i);

                pageXRes = currentPage.ImageXResolution; //pixels per inch
                pageWidth = currentPage.ImageWidth; // in pixels
                pageInchWidth = pageWidth / pageXRes;

                pageYRes = currentPage.ImageYResolution; //pixels per inch
                pageHeight = currentPage.ImageHeight; //in pixels
                pageInchHeight = pageHeight / pageYRes;

                //Work out amount to scale image by with 10,000 being 100% and 5000 being 50% for example.
                pageWidthScale = Decimal.ToInt32(Decimal.Divide(pageWidthWanted,pageInchWidth) * 10000);
                pageHeightScale = Decimal.ToInt32(Decimal.Divide(pageHeightWanted,pageInchHeight) * 10000);

                //Use the smaller of the two numbers between width or height scale for the amount to scale the page by
                if (pageWidthScale < pageHeightScale) { scalePageTo = pageWidthScale;}
                else {scalePageTo = pageHeightScale;}



                if (scalePageTo != 10000)
                {
                    docExporter.ScaleFactor = scalePageTo;
                    docExporter.AllowInterpolation = true;

                    using (MemoryStream ms = new System.IO.MemoryStream())
                    {
                        docExporter.ExportPage(docInfo, i, ms);

                        ms.Seek(0, System.IO.SeekOrigin.Begin);

                        // Write page back to Laserfiche document
                        using (Stream writer = currentPage.WritePagePart(PagePart.Image, (int)ms.Length))
                        {
                            byte[] buffer = new byte[0x8000];
                            int count;
                            while ((count = ms.Read(buffer, 0, buffer.Length)) > 0)
                                writer.Write(buffer, 0, count);
                        }
                    }
                }
            }
        }
    }
}

 

0 0
replied on January 6, 2020

Just a tip, you might want to leave some tolerance in your size comparison because it is very common for documents not to be "exactly" 8.5/11.

For example, in processes where I've rescaled documents, I found that it is extremely common to see 8.45, 8.6, and other "close" sizes that still export to an "8.5x11" PDF.

The reason I bring it up is that it rescaling a document by a small amount like that could be unnecessary and/or impact the quality.

Just keep in mind that if you look for an exact match on 10,000 you may end up doing a lot more work than is actually necessary.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.