How to crop the document before saving as a PDF

    I'm using the WebKit HtmlToPdfConverter in a .net core 3.1 project and results are promising.

I need to crop the HTML or remove some bits like the navbar from appearing in the saved PDF document. Is it possible to specify document options for adding or removing elements, or even starting the print to pdf at a lower height?

I've tried to make use of PdfJavaScriptAction however it doesn't seem to do anything, and I couldn't find any documentation for it, either.

Code for actionresult below:

 public async Task<IActionResult> ExportPdf(int id)
        {
            var model = await _raportService.GetAsync(id);
            model.HideButtons = true;

            HtmlToPdfConverter converter = new HtmlToPdfConverter();
            WebKitConverterSettings settings = new WebKitConverterSettings();

            settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QTBinariesWindows");
            converter.ConverterSettings = settings;

            // replace exportpdf in query string cu view-ul fără butoane.
            string queryString = Request.GetDisplayUrl().Replace("ExportPdf", "Deschide");

            var url = queryString;
            PdfDocument document = converter.Convert(url);
            document.PageSettings.SetMargins(0, -100, 0, 0);
            MemoryStream ms = new MemoryStream();

            //Create JavaScript action
            PdfJavaScriptAction javaScriptAction = new PdfJavaScriptAction("app.alert(\"Hello World!!!\")");
            //Add the JavaScript action
            document.Actions.BeforeSave = javaScriptAction;
            document.Save(ms);
            document.Close(completely: true);

            ms.Position = 0;

            FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf");
            fileStreamResult.FileDownloadName = $"Raport_{model.NrÎnregistrare}.pdf";

            return fileStreamResult;

        }

Thanks in advance.

1 Reply 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team August 10, 2020 10:18 AM UTC

Hi Alex,   
  
Thank you for using Syncfusion products.   

Queries 
Response 
I need to crop the HTML or remove some bits like the navbar from appearing in the saved PDF document. Is it possible to specify document options for adding or removing elements, or even starting the print to pdf at a lower height? 
We can able to crop a PDF page from an existing PDF document in our library. We have created a sample with simple HTML and after converting the PDF document, we can crop the page by using PdfTemplate in that existing PDF document. We could not crop the element from the HTML file in our converter. Please refer the below sample and output document from a link,   

Note : We can set the Top margins to our converter settings to print the HTML content as PDF.    

Please refer the below links for more information about Partial HTML to PDF conversion, 

Please try the above suggestions in your end and let us know the result. 

I've tried to make use of PdfJavaScriptAction however it doesn't seem to do anything, and I couldn't find any documentation for it, either. 
We can add JavaScript action to PDF document by using our library. Please refer the below links for more information about JavaScript Action, 

Please let us know if you need any further assistance with this. 
   

Regards, 
Gowthamraj K 


Marked as answer
Loader.
Up arrow icon