When scaling a PDF page, some graphic patterns are turning black afterwards

Question:
I’m using Page.SetScale to adjust the page for printing. We want to add a header and footer to the page.

However, after using SetScale some patterns turn to black.

Answer:
The problem is that there is a pattern, and the pattern location is “hardcoded” and is ignoring the scaling, so the act of scaling is changing the appearance of the content. I’ve never seen this before, but I’m seeing the same output in all PDF viewers I tried, so there is no bug to fix in that sense.

I took a quick look in our library, on how we scale pages from printing, while keeping graphic state, and the following code is a very simplified version. You can see similar code in our ImpositionTest sample project. I left out some code, again look at the Imposition test sample, or ElementBuilder for more example code

This code will place your source page on a newer, larger master page, while making sure the coordinate system of the patterns in the source page remains unchanged.

`

Page srcPage = // import/get your source page
Page masterpage = // create your target page of the desired page dimension
ElementWriter writer = new ElementWriter();
writer.Begin(masterpage);
// …
Element element = builder.CreateForm(srcPage);
Matrix2D mat = new Matrix2D::IdentityMatrix();

// figure out your scale

mat.Scale(scale, scale);
// you need to also shift the image as it scales from the origin
mat.Translate(dx, dy);
element->GetGState()->SetTransform(mat);
writer.WritePlacedElement(element);
writer.End();
`

The above code will work much better than trying to get Page.Scale to work as you want it to