Here is my copy code:
I need to implement ScalePdfPage(page);
Thanx
ArrayList copy_pages = new ArrayList();
int page_num = in_doc.GetPageCount();
PageIterator itr = in_doc.GetPageIterator();
for (int i = 0; i < page_num; ++i, itr.Next())
{
copy_pages.Add(itr.Current());
}
// Import all the pages in 'copy_pages' list
ArrayList imported_pages = _doc.ImportPages(copy_pages);
// Now, place that pages in 'imported_pages' in page
sequence
for (int i = 0; i != imported_pages.Count; ++i)
{
Page page = (Page)imported_pages[i];
if (_scalePages)
{
Logger.Write("$test scaling!!!");
ScalePdfPage(page);
}
_doc.PagePushBack(page);
}
}//in_doc.Close();
return true;
}
---------------
A: You could call page.Scale(scalex, scaley) utility method. If this
does not work for you another option is that you copy page content on
a new page. For example, you could implement a utility function along
the following lines (if you search the forum you will find several
alternative implementations):
static private void TransformPage()
{
try
{
double dblScale = .8;
double dblX = 50;
double dblY = 50;
PDFNet.Initialize();
PDFDoc doc = new PDFDoc(@"Original.pdf");
doc.InitSecurityHandler();
int iPageCount = doc.GetPageCount();
for (int p = 1; p <= iPageCount; p++)
{
Page pageCurrent = doc.GetPage(p);
// Create new page
Page page = doc.PageCreate(pageCurrent.GetMediaBox());
page.SetRotation(pageCurrent.GetRotation());
// Get current page index
int iPage = pageCurrent.GetIndex();
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
writer.Begin(page);
// Create form that contains page contents
Element element = builder.CreateForm(pageCurrent, doc);
Matrix2D matrix = new Matrix2D(dblScale, 0, 0, dblScale,
dblX, dblY);
element.GetGState().SetTransform(matrix);
writer.WritePlacedElement(element);
// Alternative (equivalant) code to the above two lines
// Element element;
// int cnt = 0;
// while ((element = reader.Next()) != null) {
// if (cnt++ == 0)
// element.GetGState().Concat(matrix);
// writer.WriteElement(element);
// }
writer.End();
writer.Dispose();
// Copy annotations to form and position based on scaling
int iCount = pageCurrent.GetNumAnnots();
for (int i = 0; i < iCount; i++)
{
Annot annotOld = pageCurrent.GetAnnot(i);
Annot annot = new Annot(annotOld.GetSDFObj());
Rect rect = annot.GetRect();
double dblWidth = rect.Width();
double dblHeight = rect.Height();
// Shift and scale rect x-coordinate
rect.x1 = dblX + (rect.x1 * dblScale);
rect.x2 = rect.x1 + (dblWidth * dblScale);
// Shift and scale rect y-coordinate
rect.y1 = dblY + (rect.y1 * dblScale);
rect.y2 = rect.y1 + (dblHeight * dblScale);
// See if changing scale
if (dblScale != 1.0)
{
Annot.BorderStyle style = annot.GetBorderStyle();
if (style.width > 0)
{
// Scale border width (but minimum is one)
dblWidth = style.width * dblScale;
dblWidth = ((dblWidth - Math.Floor(dblWidth) >
0.5) ? Math.Ceiling(dblWidth) : Math.Floor(dblWidth));
style.width = Math.Min(1, (int)dblWidth);
annot.SetBorderStyle(style);
}
}
// Change rectangle
annot.SetRect(rect);
page.AnnotPushBack(annot);
// NOTE: Prevents dead references due to cloned
annotations still pointing
// to old page via the optional "P" (for 'parent
page') entry.
page.GetAnnot(i).GetSDFObj().Erase("P");
}
// Replace current page with transformed page
Obj curpg = pageCurrent.GetSDFObj(), newpg = page.GetSDFObj
();
newpg.Put("Parent", curpg.FindObj("Parent"));
doc.GetSDFDoc().Swap(curpg.GetObjNum(), newpg.GetObjNum
());
}
doc.Save(@"Transformed.pdf", SDFDoc.SaveOptions.e_linearized |
SDFDoc.SaveOptions.e_hex_strings);
doc.Close();
PDFNet.Terminate();
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
}
-----------------
A: If you use page.SetCropBox()/SetMediaBox() you will change only
the dimensions of the physical page. The actual content will not
resize or scale. To scale all content on the page (including the
apparent font size on the page), you can use page.Scale(ratio) method,
followed by optional adjustment to the page media/crop box.
If you need more control over the page scaling/transformation you
could place page on another page (along the lines of Imposition sample
project - http://www.pdftron.com/pdfnet/samplecode.html#Imposition) or
as described in the above article: