Creating a button on an existing pdf using itext sharp - c#

I have been trying for long but no success i have an existing pdf that i wan to load to my current C# app and want to create a simpe pusbutton to it , plaese cite some working code the default directory of the pdf is "C:\abc.pdf".
I am using itextsharp, C# VS 2010
Thanks

The closest solution I can find is something like the following.
static void AddPushbuttonField(string inputFile, iTextSharp.text.Rectangle buttonPosition, string buttonName, string outputFile)
{
using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
{
PushbuttonField buttonField = new PushbuttonField(stamper.Writer, buttonPosition, buttonName);
stamper.AddAnnotation(buttonField.Field, 1);
stamper.Close();
}
}
This came from here, but was not ranked up as a solution. The code looks good and based on my experience with itextsharp I think this will do the trick.
Source:
Adding button in a pdf file using iTextSharp

Rectangle _rect;
_rect = new Rectangle(50, 100, 100, 100);
PushbuttonField button = new PushbuttonField(writer, _rect, "button");
PdfAnnotation widget = button.Field;
button.BackgroundColor = new GrayColor(0.75f);
button.BorderColor = GrayColor.GRAYBLACK;
button.BorderWidth = 1;
button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
button.TextColor = GrayColor.GRAYBLACK;
button.FontSize = 11;
button.Text = "Text";
button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
button.ProportionalIcon = true;
button.IconHorizontalAdjustment = 0;

Related

How to write Arabic text using MigraDoc?

I am using ASP for this and I had to generate reports in PDF format and send the file back to clients so they can download it.
I made the reports using MigraDoc library and they were great but after I tried it with Arabic text I found the texts were in LTR and the characters were disjointed so I made this code to test things out
...............
MigraDoc.DocumentObjectModel.Document reportDoc = new MigraDoc.DocumentObjectModel.Document();
reportDoc.Info.Title = "test";
sec = reportDoc.AddSection();
string fileName = "test.pdf";
addformattedText(sec, "العبارة", true);
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
renderer.Document = reportDoc;
renderer.RenderDocument();
MemoryStream pdfStream = new MemoryStream();
renderer.PdfDocument.Save(pdfStream);
byte[] bytes = pdfStream.ToArray();
...............
private void addformattedText(Section sec,string text, bool shouldBeBold = false)
{
var tf = sec.AddTextFrame();
var p = tf.AddParagraph(text);
p.Format.Font.Name = "Tahoma";
if (shouldBeBold) p.Format.Font.Bold = true;
}
I get the output like this
I have tried to encode the text and make it a unicode string using this code
private string getEscapedString(string text)
{
if (true || HasArabicCharacters(text))
{
string uString = "";
byte[] utfBytes = Encoding.Unicode.GetBytes(text);
foreach (var u in utfBytes)
{
if (u != 0)
{
uString += String.Format(#"\u{0:x4}", u);
}
}
return uString;
}
else
return text;
}
and get the returned string into a paragraph and save the PDF documents with unicode parameter set to true
But it is all the same.
I can not figure out how to get it done.
The reports were done using MigraDoc 1.50.5147 library.
The problem is Arabic language font have 4 different shap in begging,last,connected and alone, where Pdfsharp and MigraDoc can not recognize which shap to print farther more you need to reverse the character order to solve this you can use AraibcPdfUnicodeGlyphsResharper to help do such work as following:
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using AraibcPdfUnicodeGlyphsResharper;
namespace MigraDocArabic
{
internal class PrintArabicUsingPdfSharp
{
public PrintArabicUsingPdfSharp(string path)
{
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Arial", 20, XFontStyle.BoldItalic);
var xArabicString = "كتابة اللغة العربية شيئ جميل".ArabicWithFontGlyphsToPfd();
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
gfx.DrawString(xArabicString, font, XBrushes.Black, new XRect(50, 50, page.Width, page.Height), XStringFormats.Center);
// Save the document...
document.Save(path);
}
}
}
Do not Forget the Extension method
By the way this is work with iText7 too
see the image for result
Result
PDFsharp does not support RTL languages yet:
http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx#Does_PDFsharp_support_for_Arabic_Hebrew_CJK_Chinese_Japanese_Korean_6
You can work around this limitation by reversing the string.
PDFsharp does not support font ligatures yet. You are probably able to work around this limitation by replacing letters with the correct glyph (start, middle, end) depending on the position.

bootstrap css file makes smaller my document when i convert html to PDF with itextsharp in c# Winforms

I am trying to build a customized PDF export form with HTML and bootstrap to use in any c# project. I want to use bootstrap designs to make my custom PDF designs. There is no problem with my own custom css file. It is working fine. But when I add the css file of bootstrap, it makes my document zoom out and be much smaller too. I cannot figure out how to fix this. I just want to create an A4 paper size form in PDF and print it.
Here is what I get when I add the bootstrap css file:
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline.css;
using iTextSharp.tool.xml.pipeline.end;
using iTextSharp.tool.xml.pipeline.html;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace pdf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
byte[] result;
Document documment = new Document(PageSize.A4);
string bootstrapCssFilePath = #"C:/Users/sea_h/Desktop/cop/fulhtml/pdf/bootstrap.min.css";
string customCssFilePath = #"C:/Users/sea_h/Desktop/cop/fulhtml/pdf/custom.css";
string htmlText = #"
<html>
<head>
</head>
<body>
<div class='container'>
<col-sm4>
<h1>Deneme H1</h1>
</col-sm4>
<col-sm4>
<h2>deneme h2</h2>
</col-sm4>
<col-sm4>
<h7>deneme h7</h7>
</col-sm4>
</div>
</body>
</html>";
using (var ms=new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(documment, ms);
writer.CloseStream = false;
documment.Open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(bootstrapCssFilePath, true);
cssResolver.AddCssFile(customCssFilePath, true);
IPipeline pipeLine = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(documment, writer)));
XMLWorker worker = new XMLWorker(pipeLine, true);
XMLParser xmlParser = new XMLParser(worker);
xmlParser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(htmlText)));
documment.Close();
result = ms.GetBuffer();
string dest = #"C:\Users\sea_h\Desktop\deneme016.pdf";
System.IO.File.WriteAllBytes(dest, result);
}
}
}
}
My custom css is:
h1{
background-color:red;
}
There is no error message.
First you need a button or something to trigger the print job, then toss on some code like this, essentially this is just going pop a print menu and go ahead with print job when user hits submit (returns 1)
In the printImage method you are going to find the declarations for fonts etc you intend to use. I'm sure there are other ways, but I use rectangles to place my draw strings where I need them. tempPoint.X and tempPoint.Y are followed by rect.location = tempPoint, this allows you to move the rectangle around as needed and keep tracking of coordinates as you go. e.graphics.drawstring() is what actually writes the text, for more specifics I would go ahead and look up some further information. From this you can just keep replicating the tempPoint movement, rect location assignment, and drawstring to customize where things are placed in your print form. As far as turning it into a pdf, windows comes with tools that are in the print menu to automate that part of it all.
private void Button1_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintImage);
PrintDialog printdlg = new PrintDialog();
/*preview the assigned document or you can create a different previewButton for it
printPrvDlg.Document = pd;
printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below
*/
printdlg.Document = pd;
if (printdlg.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
void PrintImage(object o, PrintPageEventArgs e)
{
const int ORIGIN = 150;
var near = new StringFormat() { Alignment = StringAlignment.Near };
var centered = new StringFormat() { Alignment = StringAlignment.Center };
var far = new StringFormat() { Alignment = StringAlignment.Far };
Point tempPoint = new Point();
var rect = new RectangleF(0, 0, 0, 0);
var headingRect = new RectangleF(0, 0, 0, 0);
// Create font and brush.
Font titleDrawFont = new Font("Times New Roman", 16, FontStyle.Bold);
Font subtitleDrawFont = new Font("Times New Roman", 12);
Font drawFont = new Font("Times New Roman", 8);
SolidBrush drawBrush = new SolidBrush(Color.Black);
Pen blackPen = new Pen(Color.Black, 1);
// Draw string to screen.
//***************************************************************
Image logo = Image.FromFile(imageLoc);
e.Graphics.DrawImage(logo, (e.PageBounds.Width - logo.Width) / 2,
10, logo.Width, logo.Height); //Created Centered Image in original size
rect.Width = 150;
rect.Height = 20;
headingRect.Width = e.PageBounds.Width;
headingRect.Height = 40; //Set to 40 to avoid cut off with larger title text
tempPoint.X = 0;
tempPoint.Y = 110;
headingRect.Location = tempPoint;
e.Graphics.DrawString(lblTitle.Text, titleDrawFont, drawBrush, headingRect, centered);
So i just figure out why this is happenning.
Bootstrap css file using rem units for sizing.
But itextsharp using 300ppi resulotion whic means A4 paper have 2480 px X 3508 px resulotion in 300ppi.And bootstrap sizing is so small for this resulotion.
So i can modify bootstrap css file with new higher sizes as manually to fix this problem.
Or if it is possible, i can try the set itextsharp paper ppi for lower as like 70ppi.
I think there is no clear solution for this problem.

Excel Group Text Box and Picture

I found some code to add an image to an Excel-sheet with the SDK 2.0. And this part works fine. Now I want a Text Box under the Image, but I don't know how to get a TextBox in general.
Which classes do I need an what is appands what or which property?
Furthermore it would be nice if it be groupt. So that when you drag one the other is following.
The code look like this (I know it's a bit much, but I couldt cut it more):
private void addImage(Offset offset, Extents extents, string sImagePath, string description)
{
WorksheetPart worksheetPart = this.arbeitsBlatt.WorksheetPart;
DrawingsPart drawingsPart;
ImagePart imagePart;
XDrSp.WorksheetDrawing worksheetDrawing;
ImagePartType imagePartType = getImageType(sImagePath);
{
// --- use the existing DrawingPart
drawingsPart = worksheetPart.DrawingsPart;
imagePart = drawingsPart.AddImagePart(imagePartType);
drawingsPart.CreateRelationshipToPart(imagePart);
worksheetDrawing = drawingsPart.WorksheetDrawing;
}
using (FileStream fileStream = new FileStream(sImagePath, FileMode.Open))
{
imagePart.FeedData(fileStream);
}
int imageNumber = drawingsPart.ImageParts.Count<ImagePart>();
if (imageNumber == 1)
{
Drawing drawing = new Drawing();
drawing.Id = drawingsPart.GetIdOfPart(imagePart);
this.arbeitsBlatt.Append(drawing);
}
XDrSp.NonVisualDrawingProperties noVisualDrawingProps = new XDrSp.NonVisualDrawingProperties();
XDrSp.NonVisualPictureDrawingProperties noVisualPictureDrawingProps = new XDrSp.NonVisualPictureDrawingProperties();
noVisualDrawingProps.Id = new UInt32Value((uint)(1024 + imageNumber));
noVisualDrawingProps.Name = "Picture " + imageNumber.ToString();
noVisualDrawingProps.Description = beschreibung;
PictureLocks picLocks = new PictureLocks();
picLocks.NoChangeAspect = true;
picLocks.NoChangeArrowheads = true;
noVisualPictureDrawingProps.PictureLocks = picLocks;
XDrSp.NonVisualPictureProperties noVisualPictureProps = new XDrSp.NonVisualPictureProperties();
noVisualPictureProps.NonVisualDrawingProperties = noVisualDrawingProps;
noVisualPictureProps.NonVisualPictureDrawingProperties = noVisualPictureDrawingProps;
Stretch stretch = new Stretch();
stretch.FillRectangle = new FillRectangle();
XDrSp.BlipFill blipFill = new XDrSp.BlipFill();
Blip blip = new Blip();
blip.Embed = drawingsPart.GetIdOfPart(imagePart);
blip.CompressionState = BlipCompressionValues.Print;
blipFill.Blip = blip;
blipFill.SourceRectangle = new SourceRectangle();
blipFill.Append(stretch);
Transform2D t2d = new Transform2D();
t2d.Offset = offset;
t2d.Extents = extents;
XDrSp.ShapeProperties sp = new XDrSp.ShapeProperties();
sp.BlackWhiteMode = BlackWhiteModeValues.Auto;
sp.Transform2D = t2d;
PresetGeometry prstGeom = new PresetGeometry();
prstGeom.Preset = ShapeTypeValues.Rectangle;
prstGeom.AdjustValueList = new AdjustValueList();
sp.Append(prstGeom);
sp.Append(new NoFill());
XDrSp.Picture picture = new XDrSp.Picture();
picture.NonVisualPictureProperties = noVisualPictureProps;
picture.BlipFill = blipFill;
picture.ShapeProperties = sp;
XDrSp.OneCellAnchor anchor = this.getCellAnchor();
XDrSp.Extent extent = new XDrSp.Extent();
extent.Cx = extents.Cx;
extent.Cy = extents.Cy;
anchor.Extent = extent;
anchor.Append(picture);
anchor.Append(new XDrSp.ClientData());
worksheetDrawing.Append(anchor);
worksheetDrawing.Save(drawingsPart);
#endregion
}
I think you are new to OpenXml SDK
First of all you need to use the newest version of Open XMl SDK - Version 2.5 [Download - http://www.microsoft.com/en-us/download/details.aspx?id=30425]
Here download BOTH OpenXMLSDKV25.msi , OpenXMLSDKToolV25.msi .. Install BOTH.
Now here is the trick, OpenXML productivity tool is the one you need here. It allows you to brows an existing Excel file and break it down to CODES [watch here - https://www.youtube.com/watch?v=KSSMLR19JWA]
Now what you need to do is create an Excel sheet manually with what you want [In your case add Text Box under the Image] Then open this Excel file with productivity tool and understand the CODE . Note that you will need to understand Spreadsheet file structure to understand this CODE [ Reffer this - https://www.google.com/#q=open+xml+sdk] .. Now write your codes to meet your requirement using codes of Productivity tool
NOTE - Once you analyse the dummy Spreadsheet with Productivity tool you will understand why giving or guiding with CODE examples as an answer is not practical.
- Happy Coding-

issue in exporting image to pdf

m having a grid: companysnapshot.. that grid is exported to disk then its again taken from disk and exported to the pdf.
Below code is working fine and export with the image is done. bt issues is that..
-->image is saved from UI is black background..when its exported is changing in white background( may be its getting converted to png)
--> i want to align the coordinates of the image in the pdf page
is there any way to either increase the width of the image or pdf page.
m a newbie to this ...it would be helpful if someone code me out for this a little.
private void PrepareDocument(RadDocument document)
{
document.SectionDefaultPageOrientation = PageOrientation.Landscape;
document.LayoutMode = DocumentLayoutMode.Paged;
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
}
chart document part:
private void CreateChartDocumentPart(RadDocument document, Grid whGrid, Grid companysnapshot, Grid chartgridimage)
{
Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();
Telerik.Windows.Documents.Model.Span span1;
using (MemoryStream ms = new MemoryStream())
{
companysnapshot.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
int companywidth = (int)Math.Round(companysnapshot.ActualWidth);
int companyheight = (int)Math.Round(companysnapshot.ActualHeight);
companywidth = companywidth == 0 ? 1 : companywidth;
companyheight = companyheight == 0 ? 1 : companyheight;
RenderTargetBitmap rtbmp = new RenderTargetBitmap(companywidth, companyheight, 96d, 96d, PixelFormats.Default);
rtbmp.Render(companysnapshot);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtbmp));
FileStream fs1 = File.Create(#"C:\Users\Admin\Desktop\Chart12xx.bmp");
encoder.Save(fs1);
fs1.Close();
// this.ExportPNGToImage(chart, ms);
paragraph.LeftIndent = 0;
paragraph.RightIndent = 0.0;
FileStream ss = File.Open(#"C:\Users\Admin\Desktop\Chart12xx.bmp", FileMode.Open);
ImageInline image = new ImageInline(ss, new Size(companywidth, companyheight), "bmp");
paragraph.FlowDirection = FlowDirection.LeftToRight;
paragraph.Inlines.Add(image);
ss.Close();
//Span spacespan = new Span(" ");
//paragraph.Inlines.Add(spacespan);
}
try
{
section1.Blocks.Add(paragraph);
document.Sections.Add(section1);
}
catch (Exception)
{
}
// paragraph.Inlines.Add(new Span(FormattingSymbolLayoutBox.LINE_BREAK));
}
issue is solved.. by switching from telerik export to itextsharp for pdf export.

How to create pdfformfields using iTextSharp?

I am using iTextSharp and CSharp for creating the pdf. I need to add formfields like checkbox, radiobutton and dropdown which can not be edited.
I used this..
FileStream pdffile = new FileStream(path + "/Pdf/tes.pdf",FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, pdffile);
doc.Open();
Rectangle rect = new Rectangle(100, 100, 100, 100);
RadioCheckField checkbox = new RadioCheckField(writer, rect, "bhjabsdf", "on");
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField field = checkbox.CheckField;
writer.AddAnnotation(field);
doc.Close();
But it's not working. I also read about PdfStamper. But I am creating a new pdf, not changing the existing one.So I don't know whether I can use PdfStamper?
Thanks..
Edit:
private void CreateRadioButton(PdfWriter writer, PdfContentByte cb,Font font)
{
Rectangle rect;
PdfFormField field;
PdfFormField radiogroup = PdfFormField.CreateRadioButton(writer, true);
radiogroup.FieldName = "language";
RadioCheckField radio;
int x = 20;
for (int i = 0; i < Petrol.Length; i++)
{
rect = new Rectangle(440 + i * x, 692, 450 + i * x, 682);
radio = new RadioCheckField(writer, rect, null, LANGUAGES[i]);
radio.BorderColor = GrayColor.GRAYBLACK;
radio.BackgroundColor = BaseColor.WHITE;
radio.CheckType = RadioCheckField.TYPE_CIRCLE;
if (Petrol[i] == "F")
radio.Checked = true;
field = radio.RadioField;
//Here i am setting readonly..
field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
radiogroup.AddKid(field);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
new Phrase(Petrol[i], font), 451 + i * x, 684, 0);
if (i >= 1) x = 25;
}
writer.AddAnnotation(radiogroup);
}
You're creating a field 'the hard way'. There's a class named RadioCheckField that makes it much easier for you to create a field.
Please take a look at the book examples from Chapter 8. You can find C# versions of the examples here, for instance an example named Buttons.
checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "on");
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField field = checkbox.CheckField;
You can create your custom form template using LiveCycle and then data bind the form fields using iTextSharp like this
string randomFileName = Helpers.GetRandomFileName();
string formTemplate = Server.MapPath("~/FormTemplate.pdf");
string formOutput = Server.MapPath(string.Format("~/downloads/Forms/Form-{0}.pdf", randomFileName));
PdfReader reader = new PdfReader(formTemplate);
PdfStamper stamper = new PdfStamper(reader, new System.IO.FileStream(formOutput, System.IO.FileMode.Create));
AcroFields fields = stamper.AcroFields;
// set form fields
fields.SetField("Date", DateTime.Now.ToShortDateString());
fields.SetField("FirstName", user.FirstName);
fields.SetField("LastName", user.LastName);
fields.SetField("Address1", user.Address1);
fields.SetField("Address2", user.Address2);
fields.SetField("City", user.City);
fields.SetField("State", user.State);
fields.SetField("Zip", user.Zip);
fields.SetField("Email", user.Email);
fields.SetField("Phone", user.Phone);
// set document info
System.Collections.Hashtable info = new System.Collections.Hashtable();
info["Title"] = "User Information Form";
info["Author"] = "My Client";
info["Creator"] = "My Company";
stamper.MoreInfo = info;
// flatten form fields and close document
stamper.FormFlattening = true;
stamper.Close();
You do not need to use a PdfStamper to create AcroForm form fields in a PDF, PdfWriter also allows you to.
Unfortunately you neither said in which way your code didn't work nor what exact requirements you have; still some sample code might bring you on track:
Have a look at chapter 8 of iText in Action, 2nd edition; especially the sample Buttons will give you numerous hints on how to create radio buttons and check boxes. The sample ChoiceFields will show you how to create list and combo boxes.

Categories