- Home
- Forum
- ASP.NET MVC - EJ 2
- Issue while merging one fillable pdf to another
Issue while merging one fillable pdf to another
Hello everyone,
I’m working on an application where I need to fill a fillable PDF with data and merge it with another PDF uploaded by the user. The uploaded PDF also contains fillable fields. The desired workflow involves: Filling the original PDF with data. Merging the user-uploaded PDF (with fillable fields) into the original PDF. Saving the resulting document as a single PDF. However, when I attempt to save the merged PDF using Syncfusion, I encounter the following error:
- Error occurred: Object reference not set to an instance of an object. Stack Trace: at Syncfusion.Pdf.Parsing.PdfLoadedStyledField.ObtainBorderPen() at Syncfusion.Pdf.Parsing.PdfLoadedStyledField.get_BorderPen() at Syncfusion.Pdf.Parsing.PdfLoadedStyledField.GraphicsProperties..ctor(PdfLoadedStyledField field) at Syncfusion.Pdf.Parsing.PdfLoadedStyledField.GetGraphicsProperties(GraphicsProperties& graphicsProperties, PdfLoadedFieldItem item) at Syncfusion.Pdf.Parsing.PdfLoadedTextBoxField.DrawTextBox(PdfGraphics graphics, PdfLoadedFieldItem item) at Syncfusion.Pdf.Parsing.PdfLoadedTextBoxField.ApplyAppearance(PdfDictionary widget, PdfLoadedFieldItem item) at Syncfusion.Pdf.Parsing.PdfLoadedTextBoxField.BeginSave() at Syncfusion.Pdf.Parsing.PdfLoadedForm.FlttenFormFields() at Syncfusion.Pdf.Parsing.PdfLoadedForm.Dictionary_BeginSave(Object sender, SavePdfPrimitiveEventArgs ars) at Syncfusion.Pdf.Primitives.PdfDictionary.OnBeginSave(SavePdfPrimitiveEventArgs args) at Syncfusion.Pdf.Primitives.PdfDictionary.Save(IPdfWriter writer, Boolean bRaiseEvent) at Syncfusion.Pdf.Primitives.PdfDictionary.Save(IPdfWriter writer) at Syncfusion.Pdf.IO.PdfCrossTable.DoSaveObject(IPdfPrimitive obj, PdfReference reference, PdfWriter writer) at Syncfusion.Pdf.IO.PdfCrossTable.SaveIndirectObject(IPdfPrimitive obj, PdfWriter writer) at Syncfusion.Pdf.IO.PdfCrossTable.SavePrimitive(IPdfPrimitive obj, PdfWriter writer) at Syncfusion.Pdf.IO.PdfCrossTable.SaveObjects(PdfWriter writer) at Syncfusion.Pdf.IO.PdfCrossTable.Save(PdfWriter writer) at Syncfusion.Pdf.Parsing.PdfLoadedDocument.AppendDocument(PdfWriter writer) at Syncfusion.Pdf.Parsing.PdfLoadedDocument.Save(Stream stream) at Syncfusion.Pdf.PdfDocumentBase.Save(String filePath)
What I’ve Tried: Loading the PDFs correctly before attempting to merge them. Ensuring that the user-uploaded PDF has fillable fields. Attempting to flatten the form fields before saving the document.
- using Syncfusion.Pdf;
- using Syncfusion.Pdf.Parsing;
- using System;
- using System.IO;
- using System.Net.Http;
- using System.Threading.Tasks;
- public class PdfMerger {
- private PdfLoadedDocument _pdfDocument;
- public PdfMerger(string pdfFilePath) {
- // Load the existing PDF document FileStream pdfStream = new FileStream(pdfFilePath, FileMode.Open, FileAccess.ReadWrite);
- _pdfDocument = new PdfLoadedDocument(pdfStream); }
- public async Task AttachPdfFromPublicUrlToPdfPage(string pdfUrl) {
- try {
- using (HttpClient httpClient = new HttpClient()) {
- // Download the PDF file from the public URL
- byte[] pdfBytes = await httpClient.GetByteArrayAsync(pdfUrl);
- if (pdfBytes == null || pdfBytes.Length == 0) {
- throw new InvalidOperationException("Downloaded PDF is empty.");
- }
- // Create a memory stream from the downloaded PDF bytes
- using (MemoryStream downloadedPdfStream = new MemoryStream(pdfBytes)) {
- // Load the downloaded PDF document
- using (PdfLoadedDocument attachmentPdf = new PdfLoadedDocument(downloadedPdfStream)) {
- // Copy pages from the downloaded PDF into the existing document
- for (int i = 0; i < attachmentPdf.Pages.Count; i++) {
- PdfPageBase importedPage = attachmentPdf.Pages[i];
- _pdfDocument.ImportPage(attachmentPdf, importedPage); // Correctly importing the page
- } // Close the downloaded PDF document
- attachmentPdf.Close(true);
- }
- }
- }
- }
- catch (Exception ex) {
- Console.WriteLine($"Error attaching PDF from public URL: {ex.Message}");
- }
- }
- public void Save(string outputPath) {
- using (FileStream outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) {
- _pdfDocument.Save(outputStream);
- }
- }
- public void Close() {
- _pdfDocument.Close(true);
- }
- }
Request for Help: I would appreciate it if anyone who has experience with Syncfusion’s PDF library could provide guidance on: Why this error might be occurring. How to properly merge a fillable PDF uploaded by the user into another PDF. Any Syncfusion-specific configurations or best practices I might be missing.
Hi dhiman,
We were unable to reproduce the reported behavior with the provided details on our end and we will share the sample we used to replicate the issue on our end. Please share with us the modified sample and the issue seems to be document specific. So please share with us the documents to replicate the issue.
To proceed further, it would be helpful to analyze the specific document to provide more tailored advice. Please rest assured that Syncfusion maintains strict confidentiality regarding all information disclosed by our customers. We have a Non-Disclosure Agreement (NDA) in place, ensuring that any documents shared will be used solely for replicating and resolving the issue. These documents will not be shared with any third party.
Regards,
Irfana J
Attachment: TestWebSample_11b035a.zip
- 1 Reply
- 2 Participants
-
DS dhiman sutradhar
- Apr 7, 2025 11:37 AM UTC
- Apr 8, 2025 10:39 AM UTC