Free Support Forum - aspose.com

Adjust Field Height

Recently we upgraded from PDF.Kit to PDF v 18.2. We have a field being populated by a barcode using barcode font and previously the whole thing fit within the space of the field. After we did the upgrade however, the barcode is now being cut off. The PDF template hasn’t changed at all. Is there a way to adjust the field height in code to dynamically fit the size of the data being filled in? There are over 2000 templates that have these barcodes and we’d like to avoid manually altering each one if at all possible.

@alrodrig,

Kindly send a sample PDF document and code. We will investigate your scenario in our environment and share our findings with you.

public class DocPrintingService
{
	public void PrintDocument(...)
	{
		...
		
		File.Copy(sourceFilePath, destinationFilePath);
		using (FileStream fileStream = new FileStream(destinationFilePath, FileMode.Open, FileAccess.Read))
		{
			var asposePdfForm = PdfManager.InitializeForm(fileStream);
			
			foreach(var formField in asposePdfForm.FieldsNames)
			{
				asposePdfForm.FillField(formField, data[formField]);
			}
			
			asposePdfForm.FlattenAllFields();
			asposePdfForm.Save();
		}
		
		...
	}
}

// Wrapper class to make migration to Aspose.PDF from PDF.Kit easier
public class PdfManager
{
	...
	
	public PdfManager InitializeForm(Stream srcStream)
	{
		_form = new Form();
		_form.BindPdf(srcStream);
		_destStream = new MemoryStream();
		
		return this;
	}
	
	public void FillField(string fieldName, string fieldValue)
	{
		_form.FillField(fieldName, fieldValue);
	}
	
	public void FlattenAllFields()
	{
		_form.FlattenAllFields();
	}
	
	public void Save()
	{
		_form.Save(_destStream);
	}    	

	...
}

00COVERPAGE.pdf (229.0 KB)

@alrodrig,

The barcode field is not being recognized by the Acrobat and code snippet is also not in fully compilable form. Please review and send the input PDF along with the complete code. Your response is awaited.

public class DocPrintingService
{
	private const string AsposeLicenseKeyPath = "C:/d/ContractTemplates/Aspose.Pdf.lic";

	public static void TestExecute()
	{
		const string TemplatePath = @"C:\d\ContractTemplates\00COVERPAGE.pdf";
		const string SavePath = @"C:\Users\alrodrig\Desktop\FilledCOVERPAGE.pdf";

		var data = new Dictionary<string, string>
		{
			{ "CPN", "000789456156" },
			{ "DSF", "January 01, 2012" },
			{ "PJN", "UNDER THE TUSCAN SUN STES" },
			{ "ACN", "11-2358" },
			{ "QM1", "Test G. User" }
		};

		var service = new DocPrintingService();
		var file = service.PrintDocument(data, TemplatePath, SavePath);
		using (var fileStream = File.Create(SavePath, file.Length, FileOptions.None))
		{
			var binaryWriter = new BinaryWriter(fileStream);
			binaryWriter.Write(file);
			binaryWriter.Close();
			fileStream.Close();
		}
	}

	public byte[] PrintDocument(Dictionary<string, string> data, string sourceFilePath, string destinationFilePath)
	{
		var license = new License();
		license.SetLicense(AsposeLicenseKeyPath);

		byte[] newFile;
		var tempPath = destinationFilePath + ".temp";
		File.Copy(sourceFilePath, tempPath);
		using (var fileStream = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
		{
			var asposePdfForm = PdfManager.InitializeForm(fileStream);

			foreach (var formField in asposePdfForm.FieldsNames)
			{
				asposePdfForm.FillField(formField, ReturnReplacedFormField(formField, data));
			}

			asposePdfForm.FlattenAllFields();
			asposePdfForm.Save();
			
			newFile = ((MemoryStream)asposePdfForm.DestStream).ToArray();
		}

		File.Delete(tempPath);
		return newFile;
	}

	public string ReturnReplacedFormField(string formField, IDictionary<string, string> data)
	{
		var replacedField = formField;
		var fields = formField.Split('*', '[', ' ');

		foreach (var field in fields.Where(x => !string.IsNullOrWhiteSpace(x)))
		{
			data.TryGetValue(field, out var value);
			replacedField = replacedField.Replace("[" + field, value);
		}

		return replacedField;
	}
}

// Wrapper class to make migration to Aspose.PDF from PDF.Kit easier
public class PdfManager : IDisposable
{
	private Form _form;
	private MemoryStream _destStream;

	public PdfManager(Stream srcStream)
	{
		_form = new Form();
		_form.BindPdf(srcStream);
		_destStream = new MemoryStream();
	}

	public string[] FieldsNames
	{
		get { return _form.FieldNames; }
	}

	public Stream DestStream
	{
		get { return _destStream; }
	}

	public static PdfManager InitializeForm(Stream srcStream)
	{
		return new PdfManager(srcStream);
	}

	public void FillField(string fieldName, string fieldValue)
	{
		_form.FillField(fieldName, fieldValue);
	}

	public void FlattenAllFields()
	{
		_form.FlattenAllFields();
	}

	public void Save()
	{
		_form.Save(_destStream);
	}

	public void Dispose()
	{
		_form?.Dispose();
		_destStream?.Dispose();
		_destStream = null;
	}
}

The barcode field in question is the one for the Contract ID

@alrodrig,

In order to set the height of barcode field, please try the code as follows:
C#

string dataDir = @"C:\Pdf\test791\";
Document pdfDocument = new Document(dataDir + "00COVERPAGE.pdf");
pdfDocument.Form["*[CPN*"].Height = pdfDocument.Form["*[CPN*"].Height + 20;
MemoryStream pdfStream = new MemoryStream();
pdfDocument.Save(pdfStream, SaveFormat.Pdf);
// Input and output file paths are equal, this forces incremental update when form saved.
Aspose.Pdf.Facades.Form form = new Aspose.Pdf.Facades.Form(pdfStream);
// Fill value in form field
form.FillField("*[CPN*", "000789456156");
// Save updated document
form.Save(dataDir + "Output.pdf");

This is the output PDF: Output.pdf (226.6 KB)

Thank you. We’ll try this out