I have a template PDF that is slightly larger than A4 and has cross hairs near each corner to indicate the A4 size part of page. How should I look to extract that centered A4 part from the oversized PDF to merge with another PDF that is A4 and produce a final A4 PDF?
So breaking this down to the template crop/clip issue. I have used this code adapted from a Java example. It does appear to Crop/Clip as I wish ... except it still ends up with the original template page size (bigger than A4) not A4 as I was expecting / wanting? Then I have to work out how to merge with the pdf that contains text to be merged with the template once it is A4.
private void manipulatePdf(string sTemplate, string sDoc, string sOutput)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(sOutput));
Document doc = new Document(pdfDoc, PageSize.A4);
doc.SetMargins(0f,0f,0f,0f);
PdfDocument docTemplateA4Plus = new PdfDocument(new PdfReader(sTemplate));
PdfDocument docTextA4 = new PdfDocument(new PdfReader(sDoc));
PdfPage pageTemplateA4Plus = docTemplateA4Plus.GetPage(1);
PdfPage pageTextA4 = docTextA4.GetPage(1);
iText.Kernel.Geom.Rectangle mediaBoxTemplateA4Plus = pageTemplateA4Plus.GetMediaBox();
iText.Kernel.Geom.Rectangle mediaBoxTextA4 = pageTextA4.GetMediaBox();
float template_llx = mediaBoxTemplateA4Plus.GetX();
float template_lly = mediaBoxTemplateA4Plus.GetY();
float template_w = mediaBoxTemplateA4Plus.GetWidth();
float template_h = mediaBoxTemplateA4Plus.GetHeight();
float text_llx = mediaBoxTextA4.GetX();
float text_lly = mediaBoxTextA4.GetY();
float text_w = mediaBoxTextA4.GetWidth();
float text_h = mediaBoxTextA4.GetHeight();
float llx = ((template_w - text_w) / 2);
float lly = ((template_h - text_h) / 2);
float w = text_w;
float h = text_h;
docTemplateA4Plus.CopyPagesTo(1,1,pdfDoc);
string sCommand = "\nq " + llx.ToString() + " " + lly.ToString() + " " + w.ToString() + " " + h.ToString() + " re W n\nq\n";
PdfPage pdfPage = pdfDoc.GetPage(1);
new PdfCanvas(pdfPage.NewContentStreamBefore(), pdfPage.GetResources(), pdfDoc)
.WriteLiteral(sCommand);
new PdfCanvas(pdfPage.NewContentStreamAfter(), pdfPage.GetResources(), pdfDoc)
.WriteLiteral("\nQ\nQ\n");
doc.Close();
pdfDoc.Close();
}
so to answer my own question, I'm not sure it is most elegant but to solve the page size I added
var rect = new Rectangle(llx,lly,w,h);
pdfPage.SetMediaBox(rect);
and for the merging i added the 5 new lines near bottom relating to canvas in my complete code below.
private void manipulatePdf(string sTemplate, string sDoc, string sOutput)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(sOutput));
Document doc = new Document(pdfDoc, PageSize.A4);
doc.SetMargins(0f,0f,0f,0f);
PdfDocument docTemplateA4Plus = new PdfDocument(new PdfReader(sTemplate));
PdfDocument docTextA4 = new PdfDocument(new PdfReader(sDoc));
PdfPage pageTemplateA4Plus = docTemplateA4Plus.GetPage(1);
PdfPage pageTextA4 = docTextA4.GetPage(1);
iText.Kernel.Geom.Rectangle mediaBoxTemplateA4Plus = pageTemplateA4Plus.GetMediaBox();
iText.Kernel.Geom.Rectangle mediaBoxTextA4 = pageTextA4.GetMediaBox();
float template_llx = mediaBoxTemplateA4Plus.GetX();
float template_lly = mediaBoxTemplateA4Plus.GetY();
float template_w = mediaBoxTemplateA4Plus.GetWidth();
float template_h = mediaBoxTemplateA4Plus.GetHeight();
float text_llx = mediaBoxTextA4.GetX();
float text_lly = mediaBoxTextA4.GetY();
float text_w = mediaBoxTextA4.GetWidth();
float text_h = mediaBoxTextA4.GetHeight();
float llx = ((template_w - text_w) / 2);
float lly = ((template_h - text_h) / 2);
float w = text_w;
float h = text_h;
var rect = new Rectangle(llx,lly,w,h);
docTemplateA4Plus.CopyPagesTo(1,1,pdfDoc);
string sCommand = "\nq " + llx.ToString() + " " + lly.ToString() + " " + w.ToString() + " " + h.ToString() + " re W n\nq\n";
PdfPage pdfPage = pdfDoc.GetPage(1);
new PdfCanvas(pdfPage.NewContentStreamBefore(), pdfPage.GetResources(), pdfDoc)
.WriteLiteral(sCommand);
new PdfCanvas(pdfPage.NewContentStreamAfter(), pdfPage.GetResources(), pdfDoc)
.WriteLiteral("\nQ\nQ\n");
pdfPage.SetMediaBox(rect);
PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), pdfDoc.GetFirstPage().GetResources(), pdfDoc);
PdfFormXObject page;
srcDoc = new PdfDocument(new PdfReader(path));
page = srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);
canvas.AddXObject(page, 0, 0);
doc.Close();
pdfDoc.Close();
}