Page Margins Not Setting

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for .NET (v4)  /  Page Margins Not Setting

DynamicPDF Merger for .NET (v4) Forum

 Aug 30 2011 1:41 PM
I was given the following code by the support team that sets margins at the page level:

            //Arrange

            var pdfDocument = new PdfDocument(@"e:\temp\test_doc.pdf");
            var mergeDocument = new MergeDocument(pdfDocument);

            mergeDocument.Pages[0].Dimensions.LeftMargin = 10;
            mergeDocument.Pages[0].Dimensions.RightMargin = 10;
            mergeDocument.Pages[1].Dimensions.LeftMargin = 30;
            mergeDocument.Pages[1].Dimensions.RightMargin = 30;
            mergeDocument.Pages[2].Dimensions.LeftMargin = 10;
            mergeDocument.Pages[0].Dimensions.RightMargin = 10;

           


            //Act
            mergeDocument.Draw(@"e:\temp\test_docNEW.pdf");

The problem is that the test_docNEW.pdf document page margins are exactly the same as the source document test_doc.pdf

What am I missing?

Thanks,

Danny
 Aug 30 2011 2:25 PM
Posted by a ceTe Software moderator
Hello Danny,

When you change the margins of an existing PDF document using our API the margins are in fact changed, The existing content will not be affected due to the margin change, for example in your document the existing text will not be reformatted based on the new margins. Only the new content that is added to the document will be affected and will be added based on the new margin. In the below code a label is added after the top margin is set to 100 and the label will appear in the PDF document as per the new margin.

            PdfDocument pdfDocument = new PdfDocument(@"test_doc.pdf");

            MergeDocument mergeDocument = new MergeDocument(pdfDocument);

            mergeDocument.Pages[0].Dimensions.TopMargin = 100;

            mergeDocument.Pages[0].Elements.Add(new Label("TEXT STRING ADDED AFTER SETTING THE TOP MARGIN TO    
            100",10,0,400,15,Font.CourierBold,15,CmykColor.Red)); 

            mergeDocument.Draw(@"test.pdf");

Thanks,
ceTe Software Support Team.
 Aug 30 2011 2:39 PM
In our solution we’ll have an HTML file with no margins that we need to convert to PDF and set margins.  This is what I need to do:

Convert HTML to PDF
Set margins for page1
Set margins to last page
Set margins for anything in between
Save pdf for later consumption

We were using another tool for converting the HTML file to PDF and that is why the starting point for me with your utility was after the PDF was created.  Does Dynamic PDf allow for convertion from HTML to PDF?  Would I have to create a document, set the margins and then add pages from another document to it for the margins to apply?

Thanks for your help,
 Aug 30 2011 4:07 PM
Posted by a ceTe Software moderator
Hello Danny,

The DynamicPDF Merger tool you are currently using has only limited HTML to PDF conversion capability supported via FormattedTextArea class and only these HTML tags are supported.

If you need full support for HTML to PDF conversion we have a different product called DynamicPDF Converter for .NET which takes in an HTML string and converts it to PDF. You can also set the margins using the HtmlConversionOptions class. Here is an example on how to use Converter.

            HtmlConversionOptions options = new HtmlConversionOptions(false);
            options.BottomMargin = 100;
            options.TopMargin = 100;
            options.RightMargin = 100;
            options.LeftMargin = 100;

            string sampleHtml = "html string";

            Converter.ConvertHtmlString(sampleHtml,"C:\\temp\\SampleHTML_cSharp.pdf",options);

Converter for .NET is not just a .Net library, it’s a combination of windows service, print driver and .NET library and it needs to be installed on the machine that is performing the conversions. Please refer to this link for more info on Converter.

Thanks,
ceTe Software Support Team.
 Sep 23 2011 2:35 AM
Hi,

Does this mean that we can have margin set for every page when converting from HTML to PDF, if so can you provide a sample for the same?

The Dynamic PDF Generator has the ability to check for the overflow text. But doesn't support the right margin setting. Is there a way to increase or decrease the width of the overflowtext as per the right/top/left/bottom margins.
 Sep 23 2011 5:35 AM
Posted by a ceTe Software moderator
Hello,

Setting margins using the ConversionOptions class of the DynamicPDF Converter API will set margins for all pages in the converted PDF document. It is not possible to set margins for each page using the DynamicPDF Converter for .NET API.

You can set individual page margins for each page while generating the PDF using DynamicPDF Generator for .NET product. You will need to calculate the width and height for the page element which you are adding to the page so that it can fit in the display area of the page. Below is the sample code for it.

            Document document = new Document();
            string text = "This is to test adding text to the FormattedTextArea and setting margins to page";
            text = text + text;
            text = text + text;
            text = text + text;
            text = text + text;
            text = text + text;
            int j = 10;
            for (int i = 0; i < 10; i++)
            {
                Page page = new Page();
                page.Dimensions.LeftMargin = j;
                page.Dimensions.RightMargin = j;
                float width = page.Dimensions.Width -(page.Dimensions.RightMargin + page.Dimensions.LeftMargin);
                float height = page.Dimensions.Height -(page.Dimensions.TopMargin + page.Dimensions.BottomMargin);
                FormattedTextArea area = new FormattedTextArea(text, 0, 0, width, height,FontFamily.Helvetica,12,true);
                page.Elements.Add(area);
                document.Pages.Add(page);
                j += 10;
            }
            document.Draw(@"C:\MyDocument.pdf");

Thanks,
ceTe Software Support Team.
 Sep 23 2011 7:46 AM
Hi,

Here we have the small text that is to displayed in each page. How if the data in the first page overflows and need to have the overflowed data and the text there after in the next page. Can you check the same sample if you run the foreach loop 30 times.

Also let us know if this can be handled
 Sep 23 2011 12:33 PM
Posted by a ceTe Software moderator
Hello,

Below is the sample code that has the scenario you have described where there is a small text at the same location on all the pages and the FormattedTextArea overflows to different pages. When you instantiate the FormattedTextArea the x & y coordinates and the height and width will remain the same with respect to the page that it overflowed to. In case you need to change the location of the text area in the second page you can set the x & y coordinates of the overflow FormattedTextArea accordingly.

            Document document = new Document();            
            string myText = "This is a test of the overflow text functionality. ";
            myText += myText;
            myText += myText;
            myText += myText;
            myText += myText;
            myText += myText;
                       
            FormattedTextArea textArea = new FormattedTextArea(myText, 0, 50, 200, 200, FontFamily.Helvetica, 12, true);
            do
            {
                Page page = new Page();
                page.Elements.Add(new Label("small text",100,10,100,10));
                page.Elements.Add(textArea);
                document.Pages.Add(page);
                textArea = textArea.GetOverflowFormattedTextArea();
            } while (textArea != null);
           
            document.Draw(@"test.pdf");

If you run the loop for 30 times in the previously posted code the text area shrinks as the margins of the page are increasing and the height and width of the text area is adjusted this is expected behavior. This code is just a proof-of-concept code to demonstrate how one can change the page margins and the dimensions of page elements.

Thanks,
ceTe Software Support Team.
 Sep 26 2011 3:39 AM
Hi ,

Here we are not changing the text area width, it is constant. What if we are to change the textarea width as per the page margins. Also is there any support for the page breaks that is encountered in the HTML that is fed to the Dynamic PDF Generator.

Thanks & Regards
Rajesh
 Sep 26 2011 5:03 AM
Posted by a ceTe Software moderator
Hello Rajesh,

The width of the FormattedTextArea cannot be changed if the FormattedTextArea has overflow contents. The HTML page breaks are not supported by our DynamicPDF Generator for .NET product. Please refer the documentation on supported HTML tags at: http://www.DynamicPDF.com/Support/NET_Help_Library_11_01/Formatted%20Text%20Area%20Formatting.html . You will need add a new page dynamically depending on your requirements.

If the above suggestions do not meet your requirements then please send over the sample code which you are using and also send over more details about what you are trying to achieve to our support team at: support@cete.com so that they can look into it further.

Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 1:14 AM.