Hy,
I’m making a barcode as an svg and then insert into a pdf file just created with aspose.pdf, when I analyze the pdf the svg becomes just a raster image, is there any way I can insert into the pdf the barcode as svg, I know PDF has his own vectorial image type, but is there any way I can make the barcode as a vectorial image and add it to the pdf page?
Thanks
To insert an SVG barcode into a PDF created with Aspose.PDF, it’s important to note that while SVG and EMF are vector formats, PDF has its own embedded vector format. Unfortunately, the PDF standard does not allow for the direct embedding of SVG or EMF files; instead, these vector formats are typically converted to raster images (like JPG) when added to a PDF document.
Currently, Aspose does not support the direct embedding of SVG barcodes as vector images in PDF files. However, you can generate a high-resolution raster image (such as PNG) of your barcode and then insert that into your PDF. This approach ensures that the barcode maintains a good quality when printed or viewed.
Here’s a basic outline of how you can achieve this:
- Generate the Barcode: Use Aspose.BarCode to create your barcode and save it as a high-resolution image (e.g., PNG).
- Create a PDF Document: Use Aspose.PDF to create a new PDF document.
- Insert the Barcode Image: Add the generated barcode image to the PDF document.
Here’s a sample code snippet demonstrating this process:
// Step 1: Generate the barcode
using Aspose.BarCode;
using Aspose.BarCode.Generation;
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "123456789");
using (MemoryStream ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
ms.Position = 0;
// Step 2: Create a PDF document
using (Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document())
{
// Step 3: Add a page to the PDF
Aspose.Pdf.Page pdfPage = pdfDocument.Pages.Add();
// Step 4: Add the barcode image to the PDF
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.ImageStream = ms;
pdfPage.Paragraphs.Add(image);
// Save the PDF document
pdfDocument.Save("output.pdf");
}
}
This code generates a barcode, saves it to a memory stream as a PNG image, and then inserts that image into a new PDF document.
For future updates, there are plans to implement direct support for generating barcodes in PDF vector format, but there is no estimated time for this feature yet.
For more detailed guidance on adding images to PDF documents, you can refer to the Aspose documentation on integrating Aspose.BarCode with Aspose.PDF here and the tutorial on adding SVG objects to PDF files here.
Sources:
[1]: Integrate Aspose.BarCode with Aspose.PDF
[2]: Add SVG Object In PDF File | Aspose.PDF for .NET API Reference