I have a DocumentViewer with a fixedDocument (constructed in XAML) I then add content to the fixedDocument in code and it displays perfecty on screen.
My problem is when I try to create an XPS file from the fixedDocument, im getting an 'its already a child of another element' error.
I cant find a DocumentViewer.Children.Clear method, How can I remove/detach the fixedDocument so I can use it to create the file?
for completeness, here's the code where im getting the error:
public void CreateXPSFile()
{
// 1 - create xps_file
string OutputPath = baseDir + pathAdjust + "test.xps";
using (FileStream fs = File.Create(OutputPath))
{
ConvertToXps(fixedDocument, fs);
}
// open the document using the system default xps viewer
Process.Start(OutputPath);
}
public static void ConvertToXps(FixedDocument fd, Stream outputStream)
{
var package = Package.Open(outputStream, FileMode.Create);
var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
// xps documents are built using fixed document sequences
var fixedDocSeq = new FixedDocumentSequence();
var docRef = new DocumentReference();
docRef.BeginInit();
docRef.SetDocument(fd);
docRef.EndInit();
((IAddChild)fixedDocSeq).AddChild(docRef); <<<<<----- Error occurs here
// write out our fixed document to xps
xpsWriter.Write(fixedDocSeq.DocumentPaginator);
xpsDoc.Close();
package.Close();
}
Thanks
You should just be able to set the Document to null.
DocumentViewer dv;
dv.Document = null;
Since you are not loading the XPS into dv then setting the dv.Document = null; might not do the trick. Rather than Process.Start(OutputPath); load the xps into dv. Or you can assign the process to a name so you can close it. But I would explicitly load into dv.
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// ...
myProcess.Close();
Related
I found a FlowDocument conversion that passes to XPS and then puts it in my FixedDocument so I can view and print it, it was working perfectly. But now the images no longer appear in the print what could it be?
Thanks in advance for all the help.
FlowDocument doc = new FlowDocument(); //FlowDocument with a image
var paginator = ((IDocumentPaginatorSource)doc).DocumentPaginator;
var package = Package.Open(new MemoryStream(), FileMode.Create, FileAccess.ReadWrite);
var packUri = new Uri("pack://temp.xps");
PackageStore.RemovePackage(packUri);
PackageStore.AddPackage(packUri, package);
var xps = new XpsDocument(package, CompressionOption.NotCompressed, packUri.ToString());
XpsDocument.CreateXpsDocumentWriter(xps).Write(paginator);
FixedDocument TESTE = xps.GetFixedDocumentSequence().References[0].GetDocument(true);
Microsoft publish a workaround for this issue :
https://support.microsoft.com/en-us/topic/kb5022083-change-in-how-wpf-based-applications-render-xps-documents-a4ae4fa4-bc58-4c37-acdd-5eebc4e34556
I'm using IText 7 to convert my HTML file into PDF and auto download the file when users click a button.
Currently I'm planning to insert a barcode using IText7 into the PDF file, however I encountered some error. Btw, it works fine without the 'barcode code'.
This is the error : 'iText.Kernel.PdfException: 'Pdf indirect object belongs to other PDF document. Copy object to current pdf document.''
How can I add a barcode at the end of my pdf file?
public MemoryStream GetCovidFormPdfByAccessionNumber(string htmlFile, string accessionNumber)
{
var workStream = new MemoryStream();
using (var pdfWriter = new PdfWriter(workStream))
{
pdfWriter.SetCloseStream(false);
var pdfDoc = new PdfDocument(pdfWriter);
using (var document = HtmlConverter.ConvertToDocument(htmlFile, pdfWriter))
{
document.Add(CreateBarcode(accessionNumber, pdfDoc));
}
}
workStream.Position = 0;
return workStream;
}
private static Image CreateBarcode(string code, PdfDocument pdfDoc)
{
Barcode39 barcode = new Barcode39(pdfDoc);
barcode.SetCode(code);
//Create barcode object to put it to the cell as image
PdfFormXObject barcodeObject = barcode.CreateFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDoc);
var image = new Image(barcodeObject);
image.SetWidth(250);
return image;
}
I am attempting to auto-fill/sign/print the federal i-9 form using Spire.PDF and C#. The i9 file is an XFA form and is protected and doesn't allow for signing. However, if I fill the i9 and print to PDF, then I can sign that new file.
The step I'm getting stuck on is printing the filled i9 to a PDF file without actually opening Acrobat or having direct interaction from the end-user to specify a file name. I say 'printing' because if I just save it as a PDF file it never flattens the XFA form and remains locked against signing.
So far I have automated printing of the file using this code:
Process proc = new Process();
proc.StartInfo.Verb = "PrintTo";
proc.StartInfo.FileName = filename;
proc.StartInfo.Arguments = "\"" + printername + "\"";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
and I think I might be able to force use of the Microsoft Print to PDF 'printer' here, but I don't know if there's a way to specify the file name to use so that the user isn't prompted?
If I try printing using the Spire.PDF control, I am only able to get a file with the "Please wait...
If this message is not eventually replaced by the proper contents of the document, your PDF
viewer may not be able to display this type of document.." message as a result.
When the form is opened to print via Acrobat I get a popup of "This form contains incomplete or invalid information. Are you sure you want to print?" If I click Yes then I can successfully print to PDF and then I can sign that file.
So, I believe whatever data-checking is happening is causing the failure to print via code and I'm hoping those wiser than I might have some ideas of ways around this issue.
Thank you in advance for your help! If you just search for Federal i9 you should find the file I'm working with. I didn't see a space to attach a file here.
This is the code that I'm using to try to accomplish my task via the Spire.PDF control.
PdfDocument doc = new PdfDocument();
string i9path = "locationofi9file"
string newi9path = "locationoffilledi9file"
doc.LoadFromFile(i9path);
/*fill form here*/
doc.Form.IsFlatten = true;
doc.SaveToFile(newi9path, FileFormat.PDF);
doc.Close();
doc.Dispose();
doc.LoadFromFile(newi9path);
string file = "printi9";
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
PrinterSettings settings = new PrinterSettings();
PageSettings pages = new PageSettings();
string printername = "Microsoft Print to PDF";
settings.PrinterName = printername;
settings.PrintToFile = true;
settings.PrintFileName = Path.Combine(directory, file + ".pdf");
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrinterSettings = settings;
printDoc.Print();
doc.Close();
doc.Dispose();
I'm trying to add new page to a PdfStamper but this code doesn't add the template pdf fields to the stamper.
private void InsertNewPage(PdfStamper stamper, int pageNumber)
{
var pdfReader = new PdfReader(UrlTemplateBlankPage);
pdfReader.SelectPages("1");
stamper.InsertPage(pageNumber, pdfReader.GetPageSize(1));
stamper.GetOverContent(pageNumber).AddTemplate(stamper.GetImportedPage(pdfReader, 1), 0, 0);
//This code doesn't work because the code before is not adding the form
var pdfFormFields = stamper.AcroFields;
var fieldKeys = pdfReader.AcroFields.Fields.Keys;
foreach (var k in fieldKeys.ToList())
{
pdfFormFields.RenameField(k, k + string.Format("_{0:000}", pageNumber));
}
}
I searched online but I can't find an answer about my problem.
The PDF template I'm adding has some fields added with Acrobat. I can't attach the template but I can give you all informations.
I cannot see how do you instantiate the stamper. This is an example about how to read a PDF template and assign it to the stamper:
var reader = new PdfReader(TEMPLATE_PATH);
var pdfOutput = new FileStream(PDF_OUTPUT_PATH, FileMode.Create)
var stamper = new PdfStamper(reader, pdfOutput);
After that, you can set the fields using the SetField function:
stamper.AcroFields.SetField("FIELD1", "VALUE")
There is an option to make your fillable PDFs non-editable using:
stamper.FormFlattening = true;
Otherwise, your PDF still fillable.
Once you have finished working with your files, close them:
stamper.Close();
reader.Close();
There is an example about how to use iTextSharp's PdfStamper in the next link: http://www.codeproject.com/Tips/679606/Filling-PDF-Form-using-iText-PDF-Library
I would like to fill in form fields in a premade PDF doc, but I'm receiving a Null Refrence error with AcroForm when running.
string fileN4 = TextBox1.Text + " LOG.pdf";
File.Copy(Path.Combine(textBox4.Text + "\\", fileN4),
Path.Combine(Directory.GetCurrentDirectory(), fileN4), true);
// Open the file
PdfDocument document = PdfReader.Open(fileN4, PdfDocumentOpenMode.Modify);
PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields["<CASENUM>"]);
//const
string caseName = TextBox1.Text;
PdfString caseNamePdfStr = new PdfString(caseName);
//set the value of this field
currentField.Value = caseNamePdfStr;
// Save the document...
document.Save(fileN4);
So PdfTextField currentField = (PdfTextField)(document.AcroForm.Fields["<CASENUM>"]); is where the error happens. It seams that AcroForm is not even recognizing the fields.
Another option would be a find and replace text in a PDF (without using itextsharp as cannot use due to licensing).
Any help would be awesome!
You also need this if you are attempting to populate PDF form fields, you also need to set the NeedsAppearances element to true. Otherwise the PDF will "hide" the values on the form. Here is the VB code.
If objPdfSharpDocument.AcroForm.Elements.ContainsKey("/NeedAppearances") = False Then
objPdfSharpDocument.AcroForm.Elements.Add("/NeedAppearances", New PdfSharp.Pdf.PdfBoolean(True))
Else
objPdfSharpDocument.AcroForm.Elements("/NeedAppearances") = New PdfSharp.Pdf.PdfBoolean(True)
End If
I've been working on this today and I've managed to create a working solution. I've pasted my working code below. The only real differences I can see between my code and the OP's is the following:
I included Marc Ferree's code to set NeedAppearances (+1 and Many thanks!!)
I set the Text property of the field using a String variable, and not the Value property using a PdfString.
Hopefully this will be of use to somebody trying to do the same.
string templateDocPath = Server.MapPath("~/Documents/MyTemplate.pdf");
PdfDocument myTemplate = PdfReader.Open(templateDocPath, PdfDocumentOpenMode.Modify);
PdfAcroForm form = myTemplate.AcroForm;
if (form.Elements.ContainsKey("/NeedAppearances"))
{
form.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
}
else
{
form.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
}
PdfTextField testField = (PdfTextField)(form.Fields["TestField"]);
testField.Text = "012345";
myTemplate.Save(Server.MapPath("~/Documents/Amended.pdf")); // Save to new file.
I got stuck with this same problem earlier today. However, I think the source code has ?updated, so if you try the method above you are going to get a NullExceptionError. Instead, for TextField you need to generate a PdfString and use testfield.Value instead of .text. Here's an example.
static PdfAccess()
{
Pdf.PdfDocument doc = Pdf.IO.PdfReader.Open(#"C:\...\ Contract.pdf", Pdf.IO.PdfDocumentOpenMode.Modify);
Pdf.AcroForms.PdfAcroForm form = doc.AcroForm;
if (form.Elements.ContainsKey("/NeedAppearances"))
{
form.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
}
else
{
form.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
}
var name = (Pdf.AcroForms.PdfTextField)(form.Fields["Email"]);
name.Value = new Pdf.PdfString("ramiboy");
doc.Save(#"C:\...\ Contract.pdf");
doc.Close();
I have just experienced something similar to this. The first pdf file I opened did not contain acroform data and resulted in a null exception as described above. The issue is not with the opening of the pdf but the reference to the Acroform member variable having a value of null. You can test your pdf using the following code example:
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
PdfDocument _document = null;
try
{
_document = PdfReader.Open(ofd.FileName, PdfDocumentOpenMode.Modify);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"FATAL");
//do any cleanup and return
return;
}
if (_document != null)
{
if (_document.AcroForm != null)
{
MessageBox.Show("Acroform is object","SUCCEEDED");
//pass acroform to some function for processing
_document.Save(#"C:\temp\newcopy.pdf");
}
else
{
MessageBox.Show("Acroform is null","FAILED");
}
}
else
{
MessageBox.Show("Uknown error opening document","FAILED");
}
}
ADENDUM
I also noticed the key in this line of code should not have angle brackets
document.AcroForm.Fields["<CASENUM>"]
Change it to
document.AcroForm.Fields["CASENUM"]
The solution to overcome the NullReferenceException is to open your pre-made
PDF with Adobe Acrobat and give your form fields a default value, by changing their property-type to be something other than null.
Have you tried putting the current directory in when you try to open it?
Change
PdfDocument document = PdfReader.Open(fileN4, PdfDocumentOpenMode.Modify);
to
PdfDocument document = PdfReader.Open(Path.Combine(Directory.GetCurrentDirectory(), fileN4), PdfDocumentOpenMode.Modify);
I'm pretty sure that PdfReader will need a full file path, although I only use ASPOSE for pdf creation.