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

Discussion

Discussion

Convert TIFF to PDF

posted on May 7, 2024

What is the best way to convert TIFF to PDF using SDK script inside of a repository?

0 0
replied on May 8, 2024 Show version history

You can generate a PDF from TIFF pages using the DocumentExporter class. The PDF can then be written to a Laserfiche entry using the WriteEdoc method.

In the following example, the "doc" variable is the source document. If you're writing between two different document entries, change adjust the variables accordingly.

                // Initialize document export
                DocumentExporter dExp = new DocumentExporter();

                // Configure document exporter settings
                dExp.CompressionQuality = compression; // 0-100
                dExp.IncludeAnnotations = true;
                dExp.BlackoutRedactions = true;
                dExp.PageFormat = DocumentPageFormat.Jpeg;

                // Set PDF export options to flatten annotations and include searchable text
                PdfExportOptions ExportOptions = PdfExportOptions.RenderAnnotationsAsImage | PdfExportOptions.IncludeText;

                // Initialize memory stream and export PDF
                using(MemoryStream ms = new MemoryStream()){
                    dExp.ExportPdf(doc, doc.AllPages, ExportOptions, ms);

                    // Write PDF to document
                    using(Stream eDocStream = doc.WriteEdoc("application/pdf",ms.ToArray().LongLength)){
                        eDocStream.Write(ms.ToArray(),0,ms.ToArray().Length);
                    }
                }

                // Update document extension and save changes
                doc.Extension = ".pdf";

                // Save changes
                doc.Save();

If you only want to keep the PDF and not the TIFF pages, you can remove the page images after the PDF has been successfully generated and written to the target entry.

                // Read values of each page and verify image contents
                PageInfoReader pageReader = doc.GetPageInfos();
                foreach(PageInfo page in pageReader){
                    // Check if page has image and remove it
                    if(page.HasImage){
                        page.ClearPagePart(PagePart.Image);
                    }
                }

                // Save changes
                doc.Save();

I would normally do this as two separate steps to make sure the PDF is generated before removing the page images, but it can be done in one script; just don't call .Save() until both are complete.

4 0
replied on May 8, 2024 Show version history

The only thing that I would change is to put the

MemoryStream ms = new MemoryStream()

into a using statement as well to make sure that it is disposed of properly.

 

using(MemoryStream ms = new MemoryStream()){
    dExp.ExportPdf(doc, doc.AllPages, ExportOptions, ms);

    // Write PDF to document
    using(Stream eDocStream = doc.WriteEdoc("application/pdf",ms.ToArray().LongLength)){
        eDocStream.Write(ms.ToArray(),0,ms.ToArray().Length);
    }
}

If this does not work, then be sure to flush and dispose of the Memorystream

1 0
replied on May 8, 2024 Show version history

I didn't used worry about that for short scripts since, because of how the script threads are handled by default, the resources should be released when the Workflow script completes, but you're right, that is best practice.

I copied the code as-is from a very old post of mine, so I'll incorporate that change.

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

Sign in to reply to this post.