How to Flatten PDF Transparencies
What is PDF Transparency Flattening?
Transparency flattening is a process used in PDF files to convert transparent graphical elements into a format that can be printed or displayed correctly on devices that do not support transparency. In PDFs, transparency is often used to create special effects such as drop shadows, transparency masks, and blending modes. However, not all output devices support transparency, so when a PDF file is opened on such a device, the transparent objects may not appear as expected, leading to unpredictable results.
To avoid this issue, transparency flattening is used to convert these transparent objects into a format that can be displayed correctly. This process typically involves analyzing the file and converting transparent objects into a combination of opaque objects that mimic the visual appearance of the original transparent objects. Different flattening settings can be used, such as low resolution or high resolution, depending on the specific needs of the document. Flattening can also result in larger file sizes and longer processing times, so balancing transparency with the practicality of file size and processing requirements is important.
What does PDF transparency flattening do?
By flattening transparencies, the file is essentially "flattened" into a single layer, where all overlapping elements are merged into a single image. This ensures that the file will look the same regardless of where it is viewed, printed, or processed. Flattening transparencies can also reduce a PDF's file size, making it easier to share or upload. It's important to note that flattening can also result in a loss of quality, particularly if the file contains complex transparencies or gradients.
How do you flatten PDF transparencies?
To flatten transparencies using the Adobe PDF Library, check out our code samples that show how our transparency flattening works. We have samples in C++, .NET, and Java.
Here's a look at the code sample in .NET:
namespace FlattenTransparency
{
class FlattenTransparency
{
static void Main(string[] args)
{
Console.WriteLine("FlattenTransparency sample:");
// ReSharper disable once UnusedVariable
using (Library lib = new Library())
{
String sInput1 = Library.ResourceDirectory + "Sample_Input/trans_1page.pdf";
String sOutput1 = "FlattenTransparency-out1.pdf";
String sInput2 = Library.ResourceDirectory + "Sample_Input/trans_multipage.pdf";
String sOutput2 = "FlattenTransparency-out2.pdf";
if (args.Length > 0)
sInput1 = args[0];
if (args.Length > 1)
sInput2 = args[1];
if (args.Length > 2)
sOutput1 = args[2];
if (args.Length > 3)
sOutput2 = args[3];
// Open a document with a single page.
Document doc1 = new Document(sInput1);
// Verify that the page has transparency. The parameter indicates
// whether to include the appearances of annotations or not when
// checking for transparency.
Page pg1 = doc1.GetPage(0);
bool isTransparent = pg1.HasTransparency(true);
// If there is transparency, flatten the document.
if (isTransparent)
{
// Flattening the document will check each page for transparency.
// If a page has transparency, PDFL will create a new, flattened
// version of the page and replace the original page with the
// new one. Because of this, make sure to dispose of outstanding Page objects
// that refer to pages in the Document before calling flattenTransparency.
pg1.Dispose();
doc1.FlattenTransparency();
Console.WriteLine("Flattened single page document " + sInput1 + " as " + sOutput1 + ".");
doc1.Save(SaveFlags.Full, sOutput1);
}
// Open a document with multiple pages.
Document doc2 = new Document(sInput2);
// Iterate over the pages of the document and find the first page that has
// transparency.
isTransparent = false;
int totalPages = doc2.NumPages;
int pageCounter = 0;
while (!isTransparent && pageCounter <= totalPages)
{
Page pg = doc2.GetPage(pageCounter);
if (pg.HasTransparency(true))
{
isTransparent = true;
// Explicitly delete the page here, to ensure the reference is gone before we
// attempt to flatten the document.
pg.Dispose();
break;
}
pageCounter++;
}
if (isTransparent)
{
// Set up some parameters for the flattening.
FlattenTransparencyParams ftParams = new FlattenTransparencyParams();
// The Quality setting indicates the percentage (0%-100%) of vector information
// that is preserved. Lower values result in higher rasterization of vectors.
ftParams.Quality = 50;
// Flatten transparency in the document, starting from the first page
// that has transparency.
doc2.FlattenTransparency(ftParams, pageCounter, Document.LastPage);
Console.WriteLine("Flattened a multi-page document " + sInput2 + " as " + sOutput2 + ".");
doc2.Save(SaveFlags.Full, sOutput2);
}
}
}
}
}
Visit Adobe PDF Library to download a free trial and start flattening PDF transparencies today!