Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Mar 09, 2021 5:07 pm

Hello, I've successfully followed your online tutorial to add a header.

However, I want to add a barcode to the header. How can I achieve that since a PdfUnidimensionalBarcode only allows to draw itself onto a PdfPageBase. PdfPageTemplateElement clearly isn't a PdfPageBase.

Thanks,
Tommie

Tommie78
 
Posts: 2
Joined: Tue Mar 09, 2021 5:01 pm

Wed Mar 10, 2021 6:55 am

Hello,

Thanks for your inquiry.
You can convert the barcode to an image and then draw this image to the header. Below is the corresponding code for your reference. Feel free to contact us if you have further questions.
Code: Select all
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page=doc.Pages.Add();

            //set pdf margins
            PdfMargins margins = new PdfMargins(60, 25, 60, 60);
            doc.PageSettings.Margins = margins;

            PdfPageTemplateElement pdfPageTemplateElement = CreateHeaderTemplate(page, margins);
            doc.Template.Top = pdfPageTemplateElement;

            doc.SaveToFile("PdfHeader.pdf");
            System.Diagnostics.Process.Start("PdfHeader.pdf");
        }
     
        static PdfPageTemplateElement CreateHeaderTemplate( PdfPageBase page, PdfMargins margins)
        {
            SizeF pageSize = page.Size;
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;
            //draw picture in headerSpace
            Image image=creatBarcode();
            PdfImage headerImage = PdfImage.FromImage(image);
            float width = headerImage.Width / 2;
            float height = headerImage.Height / 2;
            headerSpace.Graphics.DrawImage(headerImage, margins.Left, 10, width, height);
            return headerSpace;
        }
        static Image creatBarcode()
        {
            PdfCode11Barcode barcode2 = new PdfCode11Barcode("123-4567890");
            barcode2.BarcodeToTextGapHeight = 1f;
            barcode2.TextDisplayLocation = TextLocation.Bottom;
            barcode2.TextColor = Color.Blue;
            return barcode2.ToImage();
        }


Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Mon Mar 15, 2021 9:45 am

Thanks for your response, however when exporting to an image, I lose the vectorization aspect.
I've solved it by stamping the barcode on each page in a post processing step.

Tommie78
 
Posts: 2
Joined: Tue Mar 09, 2021 5:01 pm

Mon Mar 15, 2021 10:09 am

Hello,

Thanks for your feedback.
If you encounter any issues related to our product in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.PDF

cron