You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having trouble adding text color to text fields in my PDF documents using the pdf-lib library in my Angular project. Below is a snippet of my code for context.
Code Snippet:
import{Component,OnInit}from'@angular/core';import{Router}from'@angular/router';import*asXLSXfrom'xlsx';import{degrees,PDFDocument,PDFPage,rgb,TextAlignment}from'pdf-lib';import{CommonModule}from'@angular/common';import{FormsModule}from'@angular/forms';importQRCodefrom'qrcode';importfontkitfrom'@pdf-lib/fontkit';
@Component({selector: 'app-certificate-form',templateUrl: './certificate-form.component.html',styleUrls: ['./certificate-form.component.css'],standalone: true,imports: [CommonModule,FormsModule],})exportclassCertificateFormComponentimplementsOnInit{pdfFile: File|null=null;excelFile: File|null=null;pdfFields: string[]=[];excelFields: string[]=[];fieldMapping: {[key: string]: string}={};dataEntries: any[]=[];textFieldProperties!: any[];constructor(privaterouter: Router){constnavigation=this.router.getCurrentNavigation();conststate=navigation?.extras.stateas{pdfFile: File;excelFile: File;textFieldProperties: any[];};this.pdfFile=state?.pdfFile||null;this.excelFile=state?.excelFile||null;this.textFieldProperties=state?.textFieldProperties||[];}ngOnInit(){if(this.pdfFile){this.loadPDFTextFields();}if(this.excelFile){this.loadExcelFields();}}asyncloadPDFTextFields(){if(this.pdfFile){constreader=newFileReader();reader.onload=async(e: any)=>{constpdfData=newUint8Array(e.target.result);constpdfDoc=awaitPDFDocument.load(pdfData);constform=pdfDoc.getForm();this.pdfFields=form.getFields().map((field)=>field.getName());console.log('PDF Fields:',this.pdfFields);};reader.readAsArrayBuffer(this.pdfFile);}}loadExcelFields(){if(this.excelFile){constreader=newFileReader();reader.onload=(e: any)=>{try{constdata=newUint8Array(e.target.result);constworkbook=XLSX.read(data,{type: 'array'});constsheetName=workbook.SheetNames[0];constworksheet=workbook.Sheets[sheetName];constjsonData=XLSX.utils.sheet_to_json(worksheet,{header: 1});this.excelFields=jsonData[0]asstring[];this.dataEntries=jsonData.slice(1);}catch(error){console.error('Error reading Excel file:',error);}};reader.readAsArrayBuffer(this.excelFile);}}asyncgenerateCertificates(){if(this.pdfFile&&this.dataEntries.length>0){for(constentryofthis.dataEntries){constpdfDoc=awaitPDFDocument.load(awaitthis.pdfFile!.arrayBuffer());constform=pdfDoc.getForm();for(const[pdfField,excelField]ofObject.entries(this.fieldMapping)){constfieldIndex=this.excelFields.indexOf(excelField);if(fieldIndex>=0){letfieldValue=entry[fieldIndex];if(typeoffieldValue!=='string'){fieldValue=String(fieldValue);}consttextField=form.getTextField(pdfField);constfieldProperties=this.textFieldProperties.find((prop)=>prop.name===pdfField);if(fieldProperties){const{ font, fontSize, alignment, textColor, multiline }=fieldProperties;pdfDoc.registerFontkit(fontkit);constfontBytes=awaitfetch(`../../assets/fonts/${font}.ttf`).then((res)=>res.arrayBuffer());constcustomFont=awaitpdfDoc.embedFont(fontBytes);multiline ? textField.enableMultiline() : textField.disableMultiline();textField.setText(fieldValue);const[widget]=textField.acroField.getWidgets();widget.getOrCreateBorderStyle().setWidth(0);textField.setFontSize(fontSize);textField.setColor(textColor);// This is where I'm having issuesif(alignment==='Center'){textField.setAlignment(TextAlignment.Center);}elseif(alignment==='Left'){textField.setAlignment(TextAlignment.Left);}else{textField.setAlignment(TextAlignment.Right);}textField.updateAppearances(customFont);}}}form.flatten();constpdfBytes=awaitpdfDoc.save();constblob=newBlob([pdfBytes],{type: 'application/pdf'});consturl=URL.createObjectURL(blob);consta=document.createElement('a');a.href=url;a.download=`${entry[1]}.pdf`;a.click();URL.revokeObjectURL(url);}}}// Additional methods omitted for brevity}
Issue
I am currently unable to set the text color in text fields using the pdf-lib library. Specifically, I'm trying to apply a color to the text field by calling textField.setColor(textColor);, but it doesn't seem to work as expected. The textColor variable is supposed to be an RGB color value (e.g., rgb(1, 0, 0) for red), but the text color in the output PDF remains unchanged.
Additional Notes
I'm working on an application that generates PDF forms on a web page and needs to customize various properties of text fields, including text color.
Any help or suggestions would be greatly appreciated!
The text was updated successfully, but these errors were encountered:
What are you working on?
I'm having trouble adding text color to text fields in my PDF documents using the
pdf-lib
library in my Angular project. Below is a snippet of my code for context.Code Snippet:
Issue
I am currently unable to set the text color in text fields using the
pdf-lib
library. Specifically, I'm trying to apply a color to the text field by callingtextField.setColor(textColor);
, but it doesn't seem to work as expected. ThetextColor
variable is supposed to be an RGB color value (e.g.,rgb(1, 0, 0)
for red), but the text color in the output PDF remains unchanged.Additional Notes
I'm working on an application that generates PDF forms on a web page and needs to customize various properties of text fields, including text color.
Any help or suggestions would be greatly appreciated!
The text was updated successfully, but these errors were encountered: