Hi!
I have Document object, imported from a PdfDocument object. As my object model is built I can no longer utilize the original PdfDocument. The reason this is done is because I have to present the document as an object to the users, enabling them to add pages, objects, templates, append documents etc. before saving them to disk.
To set a template I import a page from another pdf file, that I want to use as a background template on one or more of the pages in my Document.
public void SetTemplate(MemoryStream pdfStream, int pageNumber)
{
PdfDocument pdfDocument = new PdfDocument(pdfStream);
pageNumber = Math.Max(Math.Min(pageNumber, pdfDocument.Pages.Count - 1), 0);
ImportedPageData pageData = new ImportedPageData(pdfDocument.Pages[pageNumber]);
this.Width = pageData.Width;
this.Height = pageData.Height;
((AppendedPage)page).BackgroundElements.Add(pageData);
}
The template page is in some cases bigger than the original page (that is why I resize the original document (this)). This works quite well, apart from one issue: the original content seems to be placed on the bottom half, or maybe the center of the new, bigger page. This is a problem, how can I set the original contents to the upper left corner? I have tried working with the Dimensions property, but to no avail.
I understand that I can fix this problem by utilizing the PdfDocument and the PdfPage objects, using ImportedPageArea and such. The problem is that the PdfDocument object does not support appending pages or modifying contents, requiring me to rewrite the pdfdocument to a stream every time I have done changes to the document. I have not tested the overhead on this, but I would think it could be come considerable if many changes were made.
Hope you can help :)
How to place page contents
Re: How to place page contents
Support · Mar 29, 2007Hello,
The ImportedPageData will be placed in the center of the page if you do not specify the XOffset, and YOffset in the constructor of the ImportedPageData. You will hvae to specify the XOffset, YOffset in order to add the data at a particulat position in the page. You can refer to the Placing Imported Pages
topic on our help documentation.
Thanks,
ceTe Software Support Team.
Re: How to place page contents
Forum member · Mar 30, 2007Yes, I know, I have read and re-read that article several times :).
The problem is that the existing page is smaller than the ImportedPageData (in this case a template for a letter), thus I want to resize the existing page to fit to the ImportedPageData. And when I resize an existing page, it seems like DynamicPDF is adding space on the top of the existing page, and not the bottom.
In pseudo-code:
bigImportedPage = new ImportedData("template.pdf");
smallExistingPage.Height = bigImportedPage.Height;
smallExistingPage.BackgroundElements.Add(bigImportedPage);
Now the smallExistingPage contents are at the bottom of the new resized document, the existing content does not seem to retain its relative position to the top of the page, and gets pushed downwards.
Re: How to place page contents
Support · Mar 30, 2007Hello,
Yes, you can increase the height and width of an existing small page and move the contents to the corresponding positions as per the new size by using the ExtendedPageDimensions class for an ImportedPageData. Below is the sample code block for that.
MergeDocument document = new MergeDocument(@"C:\Temp\DocA.pdf"); //First page which is smaller than the Datapage size
ImportedPageData data = new ImportedPageData(@"C:\Temp\DocB.pdf",1);
//Create PageDimensions of this data
PageDimensions pageDimensions = new PageDimensions(data.Width,data.Height);
//Adjust the top, bottom, left, Right as per the difference in the sizes of the first and the data page.
pageDimensions.Edge.Top -= (data.Height-document.Pages[0].Dimensions.Height);
pageDimensions.Edge.Bottom -= (data.Height-document.Pages[0].Dimensions.Height);
//Create an ExtendedPageDimensions with the PageDimensions
ExtendedPageDimensions extendedPageDimensions = new ExtendedPageDimensions( pageDimensions.Edge, pageDimensions.Body );
//Assign the extended page dimensions to the first page
document.Pages[0].Dimensions = extendedPageDimensions;
//Add the data to the first page
document.Pages[0].Elements.Add(data);
document.DrawToWeb();
Thanks,
ceTe Software Support Team.
Re: How to place page contents
Forum member · Apr 2, 2007Ah, that really did the trick, thank you very much for your help and patience :)