|
Rank: Member Groups: Member
Joined: 10/14/2014 Posts: 17
|
Hello, I have an existing PDF that has very small margins. I need to modify the margins so that I can put a barcode in the header. I have used HtmlToPdf.Options to add the barcode to the header and change the margins when I am creating a new document. However, when accessing an existing PDF, it ignores these options. I am generating the barcode using a 3rd party DLL which renders it as a System.Drawing.Image. I am converting this image to bytes and then a base64 string so that it can be displayed in a normal img tag. I have tried to use HtmlToPdf.ConvertHtml for this bar code image and the bar code does show up on the existing PDF, but it covers text because I cannot seem to modify the margins. Is there a way to do this without using ACMRender so that I can still add HTML? If there is not, what would be the best way to accomplish this? Here is the code that I have so far:
Code: C#
List<PdfDocument> attachmentPdfs = new List<PdfDocument>();
string documentPath = Server.MapPath("/downloads/" + pdfPath);
//These are the options I would like to have for the existing PDF document
HtmlToPdf.Options.ZoomLevel = .70f;
HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(1f, 1.25f, 6.5f, 8.5f);
string barcodeText = "*0306403*";
string barcodeHtml = CreateBarcode(barcodeText);
string headerHtml = String.Format("<div style=\"text-align: right; padding-top: 52px; margin-bottom: 25px; letter-spacing: 5px;\"><img src=\"{0}\" /><br /><span style=\"padding-right: 60px;\">{1}</span></div>", barcodeHtml, barcodeText);
//This did not work
//HtmlToPdf.Options.HeaderHtmlFormat = headerHtml;
PdfDocument attachmentPdf = new PdfDocument(documentPath);
foreach (PdfPage page in attachmentPdf.Pages)
{
HtmlToPdf.ConvertHtml(headerHtml, page); //This creates an overlay that covers some text
}
//Add the attachment to the list so they can all be merged into one document at the end
attachmentPdfs.Add(attachmentPdf);
Bob
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi, Generally you can not modify the margin of an existing PDF file. Modifying the margin of an existing PDF file requires the contents of the PDF file to be reformatted. For example, 10 lines of text with a small margin can become 12 lines of text when the margin is increased. As such whatever you do with HtmlToPdf.Options, you are NOT changing anything for the exiting contents. You would only be changing settings for the new output (in this case your bar code). Having that said, it is possible for you to do generic purpose "transforms" with existing contents. You can move/scale existing contents on your page with a transform matrix. For example, if you apply 0.9 scale factor of your page contents, then everything on your page will be 0.9 times of the original size, and this will leave more margins on the side. You would call this method to apply transform matrix on the page: http://www.essentialobjects.com/doc/4/eo.pdf.pdfpage.transform.aspxNote that the default transform origin point is the bottom left corner of the page. So if you just scale down the contents, you will get more blank room on the top, but you will get even less room at the bottom and the left side. As such it may be necessary for you to apply a translate matrix to move the output towards up and right side. If you are not familiar with matrix, you can see this MS article for more details: http://msdn.microsoft.com/en-us/library/ms536397(VS.85).aspx Hope this helps. Please let us know if this works for you. Thanks!
|
|
Rank: Member Groups: Member
Joined: 10/14/2014 Posts: 17
|
Ok that should work. Thank you for the quick reply!
Bob
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
You are very welcome. Please feel free to let us know if there is anything else.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 10/14/2014 Posts: 17
|
Hello, I have run into another issue that I am hoping you can help with. Our specs have changed and I now need to put the barcode in the footer area of an existing PDF. For some reason, when I call ConvertHtml to overlay the barcode in the footer area, it gets printed twice on every page. If I change it back to to print in the header area, I only get one barcode per page. Is this a bug or am I doing something wrong? Here is the code I am using to put the barcode on the existing PDF(note that the PDF I am using for the test has 2 pages):
Code: C#
//Create the bar code along with corresponding HTML
string barcodeText = "*0306403*";
string barcodeHtml = CreateBarcode(barcodeText);
string footerHtml = "<div style=\"text-align: right; padding-top: 6px; letter-spacing: 5px;\"><img src=\"{0}\" width=\"250\" height=\"40\" /><br /><span style=\"padding-right: 60px;\">{1}</span></div>";
documentPath = Server.MapPath("/downloads/" + documentPath);
footerHtml = String.Format(footerHtml, barcodeHtml, barcodeText);
//Load the existing PDF
PdfDocument attachmentPdf = new PdfDocument(documentPath);
//Set options to control where the bar code gets placed
HtmlToPdfOptions options2 = new HtmlToPdfOptions();
options2.FooterHtmlPosition = 10f;
options2.FooterHtmlFormat = footerHtml;
options2.ZoomLevel = .85f;
options2.OutputArea = new RectangleF(1f, 1f, 6.5f, 9f);
//Print the barcode on every page
foreach (PdfPage page in attachmentPdf.Pages)
{
HtmlToPdf.ConvertHtml(footerHtml, page, options2);
}
I can send you a screen shot if that helps. Thank you for your support and time. Bob
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi, Please remove your FooterHtmlFormat. When you call ConvertHtml. It does the following: 1. Convert the HTML you passed to ConvertHtml call. This is considered as the main output; 2. For each page that has the main output, convert FooterHtmlFormat (if set) at FooterHtmlPosition. This is considered as footer; 3. The same as #2 for HeaderHtmlFormat; In your case, you are outputting the barcode as both the main output and as the footer. Thus causing the double barcode. To fix the problem, you will need to remove one. The choices are: 1. Remove FooterHtmlFormat. This leaves you only with the main output produced on step 1. The position of the main output is controlled by OutputArea and StartPosition: http://www.essentialobjects.com/doc/4/eo.pdf.htmltopdfoptions.outputarea.aspxhttp://www.essentialobjects.com/doc/4/eo.pdf.htmltopdfoptions.startposition.aspx --- OR --- 2. Use a blank main output (you can't remove main output since the header/footer output only occurs when there is main output). For example, you can call ConvertHtml(string.Empty, ....). This will produce blank main output but only output header/footer. The position of the footer is controlled by FooterHtmlPosition as you have already discovered. To sum it up, either main content or the footer will work for you, but it is not necessary to use both. Hope this clears up. Please feel free to let us know if you still have any questions. Thanks!
|
|
Rank: Member Groups: Member
Joined: 10/14/2014 Posts: 17
|
Thank you very much, that worked like a charm. I wound up passing an empty string to ConvertHtml and just using the footer. You guys offer the best support for your products - Keep up the good work!
Bob
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Glad to hear that! Please feel free to let us know if there is anything else.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 2/11/2015 Posts: 3
|
I also want to put barcode in the header.How to used HtmlToPdf.Options to add the barcode to the header?I haven't try to use this options.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
andyadams wrote:I also want to put barcode in the header.How to used HtmlToPdf.Options to add the barcode to the header?I haven't try to use this options. Hi Andy, The previous replies have already detailed a number of options. Please let us know if you still have any questions about any one of them. Thanks!
|
|