Hello,
I have been trying to place a barcode on an existing pdf. I have tried using am memory stream with no avail. All the examples I see on the website only work with new pdf files. I was hoping you had an example of integrating a barcode with an pre-existing pdf file. I have been able to accomplish this when using Aspose.Words.
Regards
Gordon Christie
Hi Gordon,
Thanks for your interest in our products.
Please note that we have a class named PdFileMend
present under Aspose.Pdf.Facades
namespace in Aspose.Pdf for .NET and it provides the capability to add Text and Image to an existing PDF document. Please note that once you have created the BarCode image, you can save it into a MemoryStream
object and then use the same object as an argument to the AddImage
method of PdfFileMend
and it will be placed on the pages of the existing PDF file. The AddImage
method also supports the capability to specify the location where the image will be placed over the PDF document. Please try using the following code snippet to accomplish this requirement.
[C#]
// create an instance of BarCodeBuilder
BarCodeBuilder barcode = new BarCodeBuilder();
// set Symbology
barcode.SymbologyType = Aspose.BarCode.Symbology.Code128;
barcode.xDimension = 0.6f;
barcode.BarHeight = 20f;
MemoryStream stream = new MemoryStream();
// set custom Width and Height
// barcode.ImageWidth = 100;
//barcode.ImageHeight = 100;
// set AutoSize to False so custom Width and Height takes effect
//barcode.AutoSize = false;
// set CodeText
barcode.CodeText = "2012-01-00000020";
// Create an instance of Caption
Caption caption = new Caption(barcode.CodeText);
// set Font name and size for Caption
caption.Font = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Bold);
// set caption alignment
caption.TextAlign = System.Drawing.StringAlignment.Center;
// set caption for top of barcode
barcode.CaptionAbove = caption;
// set caption at bottom of barcode
barcode.CaptionBelow = caption;
// hide default CodeText Caption
barcode.CodeLocation = CodeLocation.None;
// save the image
barcode.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
#region Add image to PDf file
// create PdfFileMend object to add text
PdfFileMend mender = new PdfFileMend("d:/pdftest/1page (1).pdf", "d:/pdftest/WithBarCode_Output.pdf");
// open document
PdfFileInfo fileInfo = new PdfFileInfo("d:/pdftest/1page (1).pdf");
// add image in the PDF file
for (Int32 i1 = 1; i1 <= fileInfo.NumberOfPages; i1++)
{
// best default place to place the barcode::
mender.AddImage(stream, i1, 500, 500, 600, 600);
}
mender.Close();
#endregion
stream.Close();
In case you face any problem or you have any further query, please feel free to contact. We are sorry for your inconvenience.
Thanks, i was able to do something similar by saving the barcode to an image file with.
Public Shared Function CreateBarcodeImage(ByVal barcodevalue As String) As String
Dim OutputPDFFileName As String = Parametervalue("PDFTempFolder") & Guid.NewGuid.ToString & "_barcode.tif"
Dim barcode As Aspose.BarCode.BarCodeBuilder = New Aspose.BarCode.BarCodeBuilder()
'Set the Code text for the barcode
barcode.CodeText = barcodevalue
barcode.SymbologyType = Aspose.BarCode.Symbology.Code39Standard
barcode.CodeLocation = Aspose.BarCode.CodeLocation.None
barcode.BarHeight = 16.0F
barcode.Margins.Top = 0.0F
barcode.Margins.Bottom = 0.0F
barcode.Margins.Left = 0.0F
barcode.Margins.Right = 0.0F
barcode.Rotate(90.0F)
'Save the image to your system
'set its image format to Jpeg
barcode.Save(OutputPDFFileName, System.Drawing.Imaging.ImageFormat.Tiff)
Return OutputPDFFileName
End Function
and then loading it in again and stamping it with
Public Shared Sub StampBarcode(ByVal Barcodeimage As Aspose.Pdf.ImageStamp, ByVal pdfDocument As Aspose.Pdf.Document)
ApplyAsposeLicence()
Dim pageCollection As Aspose.Pdf.PageCollection = pdfDocument.Pages
If pageCollection.Count > 0 Then
Dim pdfPage As Aspose.Pdf.Page = pageCollection(1)
Barcodeimage.Background = False
Barcodeimage.Width = CInt(Barcodeimage.Width / 2)
Barcodeimage.Height = CInt(Barcodeimage.Height / 2)
Barcodeimage.XIndent = CInt((pdfPage.Rect.Width) - (Barcodeimage.Width) - 8)
Barcodeimage.YIndent = CInt((pdfPage.Rect.Height / 2) - (Barcodeimage.Height / 2))
Barcodeimage.Rotate = Aspose.Pdf.Rotation.None
Barcodeimage.Opacity = 1
pdfPage.AddStamp(Barcodeimage)
Barcodeimage = Nothing
End If
End Sub
Dim barcodefile As String = DocumentProvider.CreateBarcodeImage(barcodetext)
Dim pdfDocument As New Aspose.Pdf.Document(inputPDF)
Dim imageStamp As New Aspose.Pdf.ImageStamp(barcodefile)
DocumentProvider.StampBarcode(imageStamp, pdfDocument)
pdfDocument.Save(outputPDF)
Hi Gordon,
I am glad to hear that your problem is resolved.
However please note that the reason I have used MemoryStream object to save resultant BarCode is because, sometimes operating system or file system does not allow creation of intermediate files and also once the BarCode image is generated, if its no more required, you will have to programaticaly delete it from system. So in order to prevent above specified issues, its better to use Stream object.
Nevertheless, as shared in my earlier post, the PdfFileMend class present under Aspose.Pdf.Facades namespace offers the capability to add watermark Text or Image to existing PDF document and ImageStamp class present under Aspose.Pdf namespace provides the capability to add Image watermark while creating a new PDF document or when manipulating existing PDF files. Please try using either approach and in case you encounter any issue or you have any further query, please feel free to contact.
1 Like
Thanks again i was able to get the stamping using the streams working.
<p> <p> </p>
Hi Gordon,
Thanks for contacting support.
I am pleased to hear that you have also managed to create BarCode image, saved it in Stream object and have managed to add it over the PDF document using AddImage method of PdfFileMend class. This approach is slight different from the one which I have shared over 395059 but as long as it works for you, we are fine with that. In case we can be of any further assistance, please do let us know.
Your patience and comprehension is greatly appreciated in this regard and thank you for being with us through this long journey towards success and for constantly helping us in achieving our goal to become the best file format company available in the industry.