Using asp.net with C# Codebehind and iTextSharp library.
I have a pdf form that I created in LiveCycle, that has text fields and a barcode (code 3 of 9). I use this template to create packing slips. When I run my code, I pull values out of the database and plug them into the text boxes and change the number value for the barcode. In order for the values to show up on the completed pdf, I have to flatten the pdf. It seems that when the pdf is flattened, I lose the barcode image. All that shows is the number that I set.
Does anyone have any Idea how to retain the barcode image when I flatten my pdf?
Here is a snippet of my code.
PdfReader pdfReader = new PdfReader(_pdfFullFilename);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(pdfTemplate, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
//...
foreach (string fieldKey in pdfFormFields.Fields.Keys)
{
if (fieldKey.Contains("BarCode[0]"))
pdfFormFields.SetField(fieldKey, _productNumber);
}
//...
pdfStamper.FormFlattening = true;
pdfStamper.Close();
pdfReader.Close();
Any Help would be much appreciated. Let me know if I need to expound on anything.
Related
I am trying to create an fillable pdf form using iText7 (in C# .Net framework) by passing an HTML using the itext7.pdfhtml library. iText is successful in generating most of the fillable fields from the html except the dropdown.
The dropdown field appears to be read-only and chooses the first value as the default. I cannot click on it for selection. Please see the attached image from the pdf.
PdfWriter pdfWriter = new PdfWriter(dest);
ConverterProperties converterProperties = new ConverterProperties();
iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter);
converterProperties.SetCreateAcroForm(true);
//For setting the PAGE SIZE
pdfDocument.SetDefaultPageSize(new PageSize(PageSize.A3));
iText.Forms.PdfAcroForm form = iText.Forms.PdfAcroForm.GetAcroForm(pdfDocument, true);
Document document = HtmlConverter.ConvertToDocument(htmlString, pdfDocument, converterProperties);
document.Close();
Ok so I have gone ahead and done all my steps. My problem is now the export to pdf. I have gotten it to work using a screenshot and iTextSharp. However, my problem is that what if for some reason that screen is not the main screen when the it takes a screenshot it'll take a screenshot of something else and save that as the report. Is there a way to save directly the form or even go one step further and save certain parts of the form. I found this doing my search but I don't know what my next step would be:
public void SaveToPdf()
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream("E:/a.pdf", FileMode.Create));
document.Open();
Paragraph p = new Paragraph();
document.Add(p);
document.Add("DONT KNOW WHAT THE SYNTAX FOR CHART WOULD BE")
document.Close();
}
I have to print a pdf document, on the click of the print button. Also I need to show the print dialog, but should not display the actual pdf to the user. And based on the print dialog properties selected, the pdf should get printed in the user selected printer.
To the generated pdf , I am able to add the printdialog properties using the below code:-
writer.SetOpenAction(new PdfAction(PdfAction.PRINTDIALOG));
But I am not able to get the pdf printed when the button is selected. can you please provide me some pointers to achieve this.
Full code:- (PDF is passed as a memorystream to the print button view).
using (MemoryStream m1 = new MemoryStream())
{
// MemoryStream m1 = new MemoryStream();
Int32 i = 0;
PdfWriter writer = PdfWriter.GetInstance(document, m1);
document.Open();
PdfContentByte content = writer.DirectContent;
document.NewPage();
PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
content.AddTemplate(page, 0, 0);
writer.SetOpenAction(new PdfAction(PdfAction.PRINTDIALOG));
document.Close();
return m1;
}
You can use Ghostscript to print the pdf file.
If yo need a Ghostscript wrapper for .NET you can take a look at Ghostscript.NET library.
Printing sample via Ghostscript.NET can be found here: https://ghostscriptnet.codeplex.com/discussions/470946
The print dialog is a common dialog, you can find more about how to show print dialog from the .NET here: Show Print Dialog before printing
I have used iTextSharp to generate an editable pdf form using PdfStamper class from iTextSharp.
Everything is fine except that when the cell contains too many characters then on focusing out of the editable cell a plus sign will show up against such cells.
How can I prevent this? May be there is a setting that I may not be aware of at the field level.
No plus sign when cell contents are not too long
Plus sign when cell contents are long
The C# code I am using to generate the editable form is as below.
PdfReader pdfReader = new PdfReader(
new RandomAccessFileOrArray(templateFilePath), null);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputFilePath,
FileMode.Create));
foreach (var kvp in fieldsValuesCollection)
{
acroFields.SetField(kvp.Key, kvp.Value);
}
pdfStamper.FormFlattening = false;
pdfStamper.Close();
That's the "Show text field overflow indicator" setting being triggered. You can turn it off in your client here in Acrobat Reader's Preferences:
Sorry, I don't think you can actually disable it at the field level. Maybe enabling multi-line for the filed will keep it from showing.
I have a PDF file with XFA forms, that I can successfully populate through a dynamically generated XML file.
Now I am trying to insert an image (a hand-made JPG signature file), for which I tried multiple ways, with "partial" luck.
I tried this:
How can i set an image to a pdf field in existing pdf file?
And this:
How can I insert an image with iTextSharp in an existing PDF?
I meant "partial" luck because the image does show in Foxit Reader, but doesn't show in Acrobat Pro.
Any help will be much, much appreciated.
EDIT:
This is the code I'm using to replace a button field with an image.
private void InsertSignatureIntoBOL(string inputFile, string fieldName, byte[] imageFile, string outputFile)
{
using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
{
AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions(fieldName)[0];
PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, fieldName);
imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
imageField.Image = iTextSharp.text.Image.GetInstance(imageFile);
imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
imageField.ProportionalIcon = false;
imageField.Options = BaseField.READ_ONLY;
stamper.AcroFields.RemoveField(fieldName);
stamper.AddAnnotation(imageField.Field, fieldPosition.page);
stamper.Close();
}
}
I also tried this code to add an image to an absolute position"
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(Convert.FromBase64String(SignatureHiddenField.Value));
image.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(image, false);
The only way I can make the images show up in Acrobat Pro is by flattening the form, but I also fill XFA fields in the same form and, when flattened, the XFA fields are shown as empty.
As I was mentioning, it works wonderfully in Foxit Phantom, but my main interest is with Acrobat Pro.
Any help would be much, much appreciated.
I ended up modifying the XDP file (in Adobe LiveCycle) in order to add an image field. Then, I populated that field with the Base64 encoded string representing the image. Many thanks.