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
Related
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 get the stream of a PDF file and I am trying to add this to my current PDF.
byte[] pdfBytes = interview.Application.CandidateResumeFile.File.FileBinary.ToArray();
Stream pdfstream = new MemoryStream(imgBytes);
This gives me the stream of the PDF file that I want to add to my page.
PdfDocument doc = new PdfDocument();
doc.Info.Title = "Test PDFSharp";
PdfPage page = doc.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
////////content
const string filename = "test.pdf";
doc.Save(filename);
Process.Start(filename);
Here is how my page is set up, and the page loads fine. I just want to add the PDF file to the page.
PDFsharp comes with several samples that solve this task in different ways.
See Concatenate Documents:
http://pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx
See Combine Documents:
http://pdfsharp.net/wiki/CombineDocuments-sample.ashx
Basically there are two options: draw an existing page onto a new page, allowing you to resize and scale it. Or add the page to the new document, creating an exact copy.
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.
I am trying to create a pdf document in my windows forms application, in the application I have forms and user controls with several controls inside like textbox, richtexbox, label, button, radiobutton and etc...
I want to create the pdf with the data present in those forms and users controls, the pdf should look like the content in those forms and users controls, the button, textbox, labels, etc, similar like a picture from those forms and users controls.
Sorry if i was not clear, I want to add what the user see in the form like it was a picture, but not do a screen shot, is like do something like this:
pdfDocument.Add(Button);
pdfDocument.Add(textBox);
Maybe this question is not right, but I am new to generate PDF.
To do the screenshot method, you could do something like this:
using (Bitmap b = new Bitmap(this.Width, this.Height))
{
using (Graphics g = Graphics.FromImage(b))
{
g.CopyFromScreen(this.Location, new Point(0, 0), this.Size);
}
Document doc = new Document();
iTextSharp.text.Image i = iTextSharp.text.Image.GetInstance(b, System.Drawing.Imaging.ImageFormat.Bmp);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(#"C:\Temp\output.pdf", FileMode.Create));
doc.SetPageSize(new iTextSharp.text.Rectangle(this.Size.Width + doc.LeftMargin + doc.RightMargin, this.Size.Height + doc.TopMargin + doc.BottomMargin));
doc.Open();
doc.Add(i);
doc.Close();
}
Easy way - screenshot your application on click of a button and embed that image into a PDF using itextsharp.
Hard (but possibly better way) - write some code using i-textsharp to generate a PDF, passing in your data...
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.