PDF File Custom Size and Base64 Image

Hello !

I am trying to create a PDF file in my Blazor Webassembly app using Syncfusion.PDF.Net.Core.

I am using the documentation from the following links:
https://help.syncfusion.com/file-formats/pdf/create-pdf-document-in-blazor

https://help.syncfusion.com/file-formats/pdf/working-with-text

  1. Please tell me how to set a custom PDF page width for label printers;
  2. How can I allow the height of a PDF page to be continuous for label printers;
  3. How can I "see" (how can I read the Y position of the last text written to the page) when I must append a new page to the document ?
  4. Please tell me how can I read and insert into a PDF file a base64 image from my SQL table.
Thank you very much and best regards !

9 Replies 1 reply marked as answer

SL Sowmiya Loganathan Syncfusion Team May 29, 2023 12:58 PM UTC


  1. Please tell me how to set a custom PDF page width for label printers.  

We can able to set the custom size to PDF page using the below code example,

 

//Create a PDF document.

PdfDocument pdfDocument = new PdfDocument();

 

//Customize the page size by specifying width and height.

pdfDocument.PageSettings.Size = new SizeF(300,400);

 

 

  1. How can I allow the height of a PDF page to be continuous for label printers;

We do support only for creating PDF document and we are not aware of label printers. So currently we do not have support to set the height of the PDF page to be continuous for label printers. 


Note: We can able to manually set the height of the PDF page by using below code example, 

//Create a PDF document.

PdfDocument pdfDocument = new PdfDocument();

//Set the page height.

pdfDocument.PageSettings.Height = 500;



  1. How can I "see" (how can I read the Y position of the last text written to the page) when I must append a new page to the document ?

To obtain the position of the last text, you can utilize the PdfLayoutResult class.

 

API link: Class PdfLayoutResult (syncfusion.com)

UG documentation link: https://help.syncfusion.com/file-formats/pdf/working-with-text#creating-a-multicolumn-pdf-document

 

Please refer the below code example for your reference,

 

//Create a PDF document.

PdfDocument pdfDocument = new PdfDocument();

//Add a page.

PdfPage page = pdfDocument.Pages.Add();

 

//Assign paragraph text

string paragraph = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";

//Create the fonts

PdfFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10);

 

//Draws the first paragraph.

PdfLayoutResult layoutResult = new PdfTextElement(paragraph, pdfFont).Draw(page, new RectangleF(10, 20, page.Size.Width, page.Size.Height));

 

//Draws the second paragraph based on the first paragraph.

layoutResult = new PdfTextElement(paragraph, pdfFont).Draw(page, new RectangleF(10, layoutResult.Bounds.Bottom+10, page.Size.Width, page.Size.Height));

 

 

KB documentation for Paginate text in PDF document:

https://support.syncfusion.com/kb/article/8190/how-to-paginate-the-text-in-a-pdf-using-c-and-vb-net

  1. Please tell me how can I read and insert into a PDF file a base64 image from my SQL table.

Please refer the below code example to insert base64 image to PDF document.  

 

//Convert base64 string to bytes.

byte[] bytes = Convert.FromBase64String(base64String);

 

//Create a PDF document.

PdfDocument pdfDocument = new PdfDocument();

//Customize the page size.

pdfDocument.PageSettings.Size = new SizeF(300,400);

//Set the page height.

pdfDocument.PageSettings.Height = 500;

//Add a page.

PdfPage page = pdfDocument.Pages.Add();

 

//Create PDF graphics for the page.

PdfGraphics graphics =page.Graphics;

MemoryStream memoryStream = new MemoryStream(bytes);

//Load the image stream.

PdfImage pdfImage = new PdfBitmap(memoryStream);

 

//Draw the image using graphics object.

graphics.DrawImage(pdfImage, 50, 50);

 

 


Please let us know if you need any further assistance.



TJ Tom Johnes May 30, 2023 11:03 PM UTC

Thank you for your answer !

I wand to use Syncfusion.PDF.Net.Core to generate a continuous receipt or a continuous label, that's why a setting to define a  continuous page is required...
I have at least two more questions for you :

  1. How can I resize the loaded base64 image to a smaller size ? I need to draw a smaller image into PDF file.
  2. Please tell me how can I send directly to a specified printer the new downloaded PDF file, when I click the export PDF file button ? I want to automatically print the new generated PDF file to a specified printer.

    THANK YOU !


IJ Irfana Jaffer Sadhik Syncfusion Team May 31, 2023 01:54 PM UTC

I wand to use Syncfusion.PDF.Net.Core to generate a continuous receipt or a continuous label, that's why a setting to define a  continuous page is required...

As we have already said, We do support only for creating PDF document and we are not aware of label printers. So currently we do not have support to set the height of the PDF page to be continuous for label printers.

How can I resize the loaded base64 image to a smaller size ? I need to draw a smaller image into PDF file
We suggest defining bounds while drawing the image to the PDF document. Please refer to the modified code snippet below:

//Load the PDF document

FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);

PdfLoadedDocument doc = new PdfLoadedDocument(docStream);

//Get first page from document

PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;


//Create PDF graphics for the page

PdfGraphics graphics = page.Graphics;

//Load the image from the disk

FileStream imageStream = new FileStream("Autumn Leaves.jpg", FileMode.Open, FileAccess.Read);

PdfBitmap image = new PdfBitmap(imageStream);

//Draw the image

graphics.DrawImage(image, 0, 0,50,50);


//Creating the stream object

MemoryStream stream = new MemoryStream();

//Save the document as stream

doc.Save(stream);

//Close the document

doc.Close(true);



Please tell me how can I send directly to a specified printer the new downloaded PDF file, when I click the export PDF file button ? I want to automatically print the new generated PDF file to a specified printer.

As of now, we do not have support for printing the PDF document by using the Syncfusion PDF Library. But we can achieve this by using a system library. Kindly try the following code snippet to print an existing PDF via Microsoft Print to PDF and let us know the result:

  string file = "../../../Data/input.pdf";

// initialize PrintDocument object

PrintDocument doc = new PrintDocument()

{

PrinterSettings = new PrinterSettings()

{

// set the printer name to the actual printer name

PrinterName = "HP LaserJet Pro MFP M127-M128 PCLMS",

// tell the object this document will print to file

PrintToFile = true,

// set the filename to whatever you like (full path)

PrintFileName = System.IO.Path.Combine(file),

 }

 };

doc.Print();






TJ Tom Johnes May 31, 2023 09:15 PM UTC

Thank you very much for all your help !

Best Regards !




TJ Tom Johnes June 1, 2023 11:34 PM UTC

Hello again !

Please be kind and tell me how to center the title horizontally on the page !

            PdfTextElement title = new PdfTextElement("THIS IS MY TITLE", pdfFont, PdfBrushes.Black);
            PdfLayoutResult result = title.Draw(page, new Syncfusion.Drawing.PointF(0, 0));


Thank you very much and

Best Regards !



IJ Irfana Jaffer Sadhik Syncfusion Team June 2, 2023 02:09 PM UTC

The TextAlignment API is used to set the alignment for the text to be drawn in the PDF document. This API will help to align the text to the center and we need to change the pointer to the middle of the page while drawing the text. we have attached the modified code for your reference. Please check this on your end and let us know the result.

Modified code :

PdfStringFormat format = new PdfStringFormat();

format.Alignment = PdfTextAlignment.Center;


PdfTextElement title = new PdfTextElement("THIS IS MY TITLE", pdfFont,null, PdfBrushes.Black, format);

PdfLayoutResult result = title.Draw(page, new Syncfusion.Drawing.PointF(page.GetClientSize().Width/2,0));


Follow the below links for more information,

https://help.syncfusion.com/cr/aspnet-core/Syncfusion.Pdf.Graphics.PdfTextElement.html#Syncfusion_Pdf_Graphics_PdfTextElement__ctor_System_String_Syncfusion_Pdf_Graphics_PdfFont_Syncfusion_Pdf_Graphics_PdfPen_Syncfusion_Pdf_Graphics_PdfBrush_Syncfusion_Pdf_Graphics_PdfStringFormat_

https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Graphics.PdfTextAlignment.html



TJ Tom Johnes June 3, 2023 08:52 PM UTC

Hello again !
With your help I managed to create a simple PDF file.
Please tell me how to resize the page height after rendering it !

I am using a thermal printer to make some kind of a continuous ticket (like a cash register long ticket) and I want to print my PDF generated file to that printer.

            //Create a PDF document.
            PdfDocument pdfDocument = new PdfDocument();
            //Set the page height.
            pdfDocument.PageSettings.Width = 215;
            pdfDocument.PageSettings.Height = 5000;
            //Set page margins
            pdfDocument.PageSettings.Margins.All = 5;
            //Add Page to the PDF document.
            PdfPage page = pdfDocument.Pages.Add();

Here I fill my PDF page and after the last line is written, I can find out the Height of the entire page based on Syncfusion PdfLayoutResult class

//Draws the last paragraph.
layoutResult = new PdfTextElement( var_last_text_paragraph, pdfFont).Draw(page, new Syncfusion.Drawing.RectangleF(0, layoutResult.Bounds.Bottom + 10, page.Size.Width, page.Size.Height));

Now I know the Height of yhe page is  layoutResult.Bounds.Bottom and I want to resize the page Height to that value and I don't know how !

I tried to set the page Height again before saving it, but no success.
            //Set the page height.
            pdfDocument.PageSettings.Width = 215;
            pdfDocument.PageSettings.Height = 2200;

Can you tell me how can I resize the page Height before saving it ?
I think I must add a new page with the NEW-HEIGHT into the PDF document, to copy all data from the first page into the second one and to remove the first page...

Can you help me, please ?







IJ Irfana Jaffer Sadhik Syncfusion Team June 5, 2023 01:39 PM UTC

It is the default behavior, Page orientation, and size should be set before adding a page to the PDF document we cannot alter the size and orientation of the page after that.

 We suggest you create a PDF document that sizes lesser than that of the input document the draw the template of the existing pdf document into a new document. We have followed the same on our end and it worked well. We will share here the code snippet and sample for your reference. Please try this on your end and let us know the result.

FileStream filestream = new FileStream("Input.pdf",FileMode.Open,FileAccess.ReadWrite);


            PdfLoadedDocument ldoc = new PdfLoadedDocument(filestream);


            PdfDocument doc = new PdfDocument();


            PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage;

            doc.PageSettings.Size = new SizeF(300, 400);

            doc.PageSettings.Margins.All=0;


            PdfPage page = doc.Pages.Add();


            PdfGraphics graphics = page.Graphics;


           PdfTemplate template = lpage.CreateTemplate();



            graphics.DrawPdfTemplate(template, new PointF(0,0), new SizeF(page.GetClientSize().Width,page.GetClientSize().Height));


            MemoryStream input = new MemoryStream();

            doc.Save(input);


            System.IO.File.WriteAllBytes("Output.pdf",input.ToArray());




Marked as answer

TJ Tom Johnes June 7, 2023 11:02 PM UTC

It is working fine now !

I managed to change the page height !

THANK YOU VERY MUCH FOR ALL YOUR HELP 

and Best Regards !



Loader.
Up arrow icon