Interactiveforms
55
How to fill out apdf file programmatically? (Dynamic
XFA)
Ihave a dynamic XFAForm thatI can filloutmanuallyusingAdobe Acrobat on my
computer.UsingiTextSharpIcanreadwhattheXFAXMLdataisandseethestructureof
thedata.IamessentiallytryingtomimicthatwithiTextusingthefollowingcode:
PdfReader pdfReader = new PdfReader(sourceFilePath);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(pdfReader, ms)) ) {
XfaForm xfaForm = new XfaForm(pdfReader);
XmlDocument doc = new XmlDocument();
doc.Load(replacementXmlFilePath);
xfaForm.DomDocument = doc;
xfaForm.Changed = true;
XfaForm.SetXfa(xfaForm, stamper.Reader, stamper.Writer);
}
var bytes = = ms.ToArray();
File.WriteAllBytes(destinationtFilePath, bytes);
}
Forsomereasonthiscodedoesn’twork.
PostedonStackOverflowonMay11,2013⁹⁸byjon333⁹⁹
Thisquestionwasansweredbythepersonwhopostedthequestion:
Ifoundtheissue.ThereplacementDomDocumentneedstobetheentiremergedXMLof
thenewdocument,notjustthedataordatasetsportion.
Iupvotedthisanswer,becauseit’snotincorrect,butnowthatIthinkit’sbettertousetheexample
fromthebook¹⁰⁰:
⁹⁸
http://stackoverflow.com/questions/16502427/how-can-i-set-xfa-data-in-a-static-xfa-form-in-itextsharp-and-get-it-to-save
⁹⁹
http://stackoverflow.com/users/511518/jon333
¹⁰⁰
http://itextpdf.com/examples/iia.php?id=165
Convert pdf to tiff quality - control application utility:C# PDF Convert to Tiff SDK: Convert PDF to tiff images in C#.net, ASP.NET MVC, Ajax, WinForms, WPF
Online C# Tutorial for How to Convert PDF File to Tiff Image File
www.rasteredge.com
Convert pdf to tiff quality - control application utility:VB.NET PDF Convert to Tiff SDK: Convert PDF to tiff images in vb.net, ASP.NET MVC, Ajax, WinForms, WPF
Free VB.NET Guide to Render and Convert PDF Document to TIFF
www.rasteredge.com
Interactiveforms
56
public byte[] ManipulatePdf(String src, String xml) {
PdfReader reader = new PdfReader(src);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
AcroFields form = stamper.AcroFields;
XfaForm xfa = form.Xfa;
xfa.FillXfaForm(XmlReader.Create(new StringReader(xml)));
}
return ms.ToArray();
}
}
Asyoucansee,it’snotnecessarytoreplacethewholeXFAXML.Ifyouusethe
FillXfaForm
method,
thedataissufficient.
control application utility:Online Convert PDF file to Tiff. Best free online PDF Tif
Using this .NET PDF to TIFF conversion control, C# developers can render and convert PDF document to TIFF image file with no loss in original file quality.
www.rasteredge.com
control application utility:C# PDF Convert to Jpeg SDK: Convert PDF to JPEG images in C#.net
library component toolkit, C# developers can easily and quickly convert a large-size multi-page PDF document to a group of high-quality separate JPEG image
www.rasteredge.com
Interactiveforms
57
How to fill XFA form using iText without breaking
usage rights?
Thisismycode:
using (FileStream pdf = new FileStream("C:/test.pdf", FileMode.Open))
using (FileStream xml = new FileStream("C:/test.xml", FileMode.Open))
using (FileStream filledPdf f = new FileStream("C:/test_f.pdf", FileMode.Create))
{
PdfReader pdfReader = new PdfReader(pdf);
PdfStamper stamper = new PdfStamper(pdfReader, filledPdf);
stamper.AcroFields.Xfa.FillXfaForm(xml);
stamper.Close();
pdfReader.Close();
}
Thiscodethrowsnoexception andeverythingseems tobeOK,butif Iopen filledpdf,
AdobeReadersayssomethinglikethis:
Thisdocumentenabledextendedfeatures.Thisdocumentwaschangedsince
itwascreatedandusingextendedfeaturesisn’tpossibleanymore.
If I choose xmlmanually by clicking ‘Import data’ from Adobe Reader, form is filled
properly,soIguessthereisnoerrorinxml.
PostedonStackOverflowonOct29,2014¹⁰¹bypaldir¹⁰²
Youarenotcreatingthe
PdfStamper
objectcorrectly.Use:
PdfStamper stamper = new PdfStamper(pdfReader, filledPdf, '\0'true)
Inyourcode,youarenotusing
PdfStamper
inappendmode.ThismeansthatiTextwillreorganize
thedifferentobjectsinyourPDF.Usuallythatisn’taproblem.
However:yourPDFisReader-enabled,whichmeansthatyourPDFisdigitallysignedusingaprivate
keyownedbyAdobe.ByreorganizingtheobjectsinsidethePDF,thatsignatureisbroken.Thisis
madeclearbythemessageyoualreadymentioned:
Thisdocument enabled extended features. This document waschangedsince itwas
createdandusingextendedfeaturesisn’tpossibleanymore.
¹⁰¹
http://stackoverflow.com/questions/26629498/how-to-fill-xfa-form-using-itext
¹⁰²
http://stackoverflow.com/users/4148435/paldir
control application utility:C# PDF File Compress Library: Compress reduce PDF size in C#.net
and decompression method, JPEG2000 compressing & decompression method, TIFF files compression Convert smooth lines to curves. C#.NET DLLs: Compress PDF Document.
www.rasteredge.com
control application utility:VB.NET PDF Convert to Jpeg SDK: Convert PDF to JPEG images in vb.
VB.NET Excel, VB.NET PowerPoint, VB.NET Tiff, VB.NET Able to Convert PDF to JPEG file in .NET WinForms and ASP Export high quality jpeg file from PDF in .NET
www.rasteredge.com
Interactiveforms
58
Toavoidbreakingthesignature,youneedtouse
PdfStamper
inappendmode.Insteadofreorganiz-
ingtheoriginalcontent, iTextwillnowkeeptheoriginalfileintactandappendnewcontentafter
theendoftheoriginalfile.
control application utility:VB.NET PDF File Compress Library: Compress reduce PDF size in vb.
Convert smooth lines to curves. Program.RootPath + "\\" 3_optimized.pdf"; 'create optimizing Compression = PDFCompression.DCTDecode 'set quality level, only
www.rasteredge.com
control application utility:C# Create PDF from Tiff Library to convert tif images to PDF in C#
PDF files are created from tiff with high quality using .NET PDF SDK for C#.NET. Description: Convert to PDF/TIFF with specified zoom value and save it on
www.rasteredge.com
Actions and annotations
Allthingsinteractivearediscussedhere.Exceptforforms,we’vealreadycoveredthese.
How to create alink to a specific page number?
IknowhowtotargetanytextofanyPDFpageusingcode:
Anchor click = new Anchor("Click to o go to o Target");
click.Reference = "#target";
Paragraph p1 = new Paragraph();
p1.Add(click);
doc.Add(p1);
Anchor target = new Anchor("Target");
target.Name = "target";
doc.Add(target);
Myquestionishowtotargetapagebasedon itsnumber.Forexampleif targeted page
numberis6,clickingontheAnchortextshouldtaketo6thpage.
PostedonStackOverflowonFeb20,2014¹⁰³byYogesh¹⁰⁴
Insteadofan
Anchor
,you needa
Chunk
.Tothis
Chunk
youneed to adda
PdfAction
.Theaction
needstobeagotoLocalPage()¹⁰⁵action.
Forinstance:
Chunk chunk = New Chunk("Go to page 5");
PdfAction action = PdfAction.GotoLocalPage(5, New PdfDestination(0), writer);
chunk.SetAction(action);
¹⁰³
http://stackoverflow.com/questions/21907184/itextsharp-how-to-target-pdf-page-number
¹⁰⁴
http://stackoverflow.com/users/532384/yogesh
¹⁰⁵
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfAction.html#gotoLocalPage%28int,%20com.itextpdf.text.pdf.PdfDestination,%20com.
itextpdf.text.pdf.PdfWriter%29
control application utility:VB.NET Image: Robust OCR Recognition SDK for VB.NET, .NET Image
of images and documents. More and more companies are trying to convert printed business Images exported can be Png, Jpeg, Tiff, image-only PDF or Bmp.
www.rasteredge.com
control application utility:VB.NET Image: RasterEdge JBIG2 Codec Image Control for VB.NET
control in VB.NET is capable of embedding compressed bitonal images into PDF files and decompress images from PDF files quickly with the smallest quality loss.
www.rasteredge.com
Actionsandannotations
60
How to insert a “linked rectangle” with iText?
IwanttoinsertahyperlinkintoanexistingPDFatapositionIknowinadvance:Ialready
havethecoordinatesofarectangleonagivenpage.Iwanttolinkthisrectangletoanother
pageofthesamePDF(whichIalsoknowinadvance).HowdoIachievethis?
PostedonStackOverflowonNov7,2013¹⁰⁶byHansStricker¹⁰⁷
PleasetakealookattheAddLinkAnnotation¹⁰⁸example.
Asyou(should) alreadyknow (butyoudidn’tshow whatyou’vealreadytried,which iskind of
mandatoryonStackOverflow),youcanuse
PdfStamper
tomanipulateanexisting PDF.Addinga
rectangularlinkononepagetoanotherpage,isassimpleasaddingalinkannotationtothatpage:
PdfReader reader new PdfReader(src);
PdfStamper stamper new PdfStamper(readernew FileOutputStream(dest));
Rectangle linkLocation new Rectangle(523, 770, 559, 806);
PdfDestination destination new PdfDestination(PdfDestination.FIT);
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
3, destination);
link.setBorder(new PdfBorderArray(0, 0, 0));
stamper.addAnnotation(link, 1);
stamper.close();
The
link
objectiscreatedusing:
• the
writer
instancetiedtothe
stamper
,
• therectangle(thepositionyousayyouknowinadvance,
• a highlighting option (pick one:
HIGHLIGHT_NONE
,
HIGHLIGHT_INVERT
,
HIGHLIGHT_OUTLINE
,
HIGHLIGHT_PUSH
,
HIGHLIGHT_TOGGLE
),
• thepageyouwanttolinkto,
• adestination.
Once you have an instance of
PdfAnnotation
, you can add it to a specific page using the
addAnnotation()
method.
¹⁰⁶
http://stackoverflow.com/questions/22194844/inserting-a-linked-rectangle-with-itext
¹⁰⁷
http://stackoverflow.com/users/363429/hans-stricker
¹⁰⁸
http://itextpdf.com/sandbox/annotations/AddLinkAnnotation
Actionsandannotations
61
How to add a maps with a pointer to a PDF?
IamusingjavaandiTexttocreateapdf.Isitpossibletoaddamapwithapointeronitso
theuserwillknowwherethestartingpointis?
PostedonStackOverflowonNov6,2014¹⁰⁹byuser2487493¹¹⁰
Whatdoyoumeanby“amapwithapointersotheuserknowswherethestartingpointis”?Ifyou
haveamapinyourPDF,youcouldaddanannotationthatlookslikeanarrow.Isthatwhatyou’re
lookingfor?
Sinceyoudidn’tanswermycounter-questionaddedincomment,I’mproviding two examples. If
thesearenotwhatyou’relookingfor,youreallyshouldclarifyyourquestion.
Example1:addacustomshapeasextracontentontopofamap
ThisisdemonstratedintheAddPointer¹¹¹example:
PdfContentByte canvas = writer.getDirectContent();
canvas.setColorStroke(BaseColor.RED);
canvas.setLineWidth(3);
canvas.moveTo(220, 330);
canvas.lineTo(240, 370);
canvas.arc(200, 350, 240, 390, , 0, , (float) 180);
canvas.lineTo(220, 330);
canvas.closePathStroke();
canvas.setColorFill(BaseColor.RED);
canvas.circle(220, 370, 10);
canvas.fill();
Ifweknow thecoordinatesofthepointer, wecan draw linesandcurvesthatresultina thered
pointershownhere(seetheredpinneartheCambridgeInnovationCenter):
¹⁰⁹
http://stackoverflow.com/questions/26752663/adding-maps-at-itext-java
¹¹⁰
http://stackoverflow.com/users/2487493/user2487493
¹¹¹
http://itextpdf.com/sandbox/objects/AddPointer
Actionsandannotations
62
Mapwithapin
Example2:addalineannotationontopofamap
ThisisdemonstratedintheAddPointerAnnotation¹¹²example:
Rectangle rect new Rectangle(220, 350, 475, 595);
PdfAnnotation annotation = PdfAnnotation.createLine(writer, rect,
"Cambridge Innovation Center", 225, 355, 470, 590);
PdfArray le new PdfArray();
le.add(new PdfName("OpenArrow"));
le.add(new PdfName("None"));
annotation.setTitle("You are here:");
annotation.setColor(BaseColor.RED);
annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
annotation.setBorderStyle(
new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
annotation.put(new PdfName("LE"), le);
annotation.put(new PdfName("IT"), new PdfName("LineArrow"));
writer.addAnnotation(annotation);
Theresultisanannotation(whichisn’tpartoftherealcontent,butpartofaninteractivelayeron
topoftherealcontent):
¹¹²
http://itextpdf.com/sandbox/annotations/AddPointerAnnotation
Actionsandannotations
63
Mapwithanannotation
Itisinteractiveinthesensethatextrainfoisshownwhentheuserclickstheannotation:
Mapwithanannotationthathasbeenopened
Manyotheroptionsarepossible,butonceagain:yourquestionwasn’tentirelyclear.
Extracting text from PDFs
iTextcanparsePDFstoextractthecontentofapage.Astherearemanydifferentwaystocreatea
PDFfile,andasthetextonapageusuallyisn’tmorethanabunchofcharactersdrawnonapage,
it’snottrivialtoextracttextcorrectly.
How to read text from aspecificposition?
IhaveaproblemusingiTextSharpwhenreadingdatafromPDFFile.WhatIwanttoachieve
istoreadonlyspecificpartofPDFpage(IwanttoonlyretrieveAddressInformation,which
islocatedatconstantposition).IhaveseenusingiTextSharpforreadingallpagessuchas
following:
StringBuilder text = new StringBuilder();
if (File.Exists(fileName)) {
PdfReader pdfReader = new PdfReader(fileName);
for (int page = 1; page <= pdfReader.NumberOfPages; page++) {
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = = PdfTextExtractor.GetTextFromPage(pdfReader, page, , \
strategy);
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.De\
fault, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
}
pdfReader.Close();
}
return text.ToString();
ButhowcanIonlyrestrictittoaspecificlocation?
PostedonStackOverflowonJun12,2014¹¹³byRobertJ.¹¹⁴
Youareusinga
SimpleTextExtractionStrategy
insteadofa
LocationTextExtractionStrategy
.If
rect
isarectanglebasedonthecoordinatesofyouraddress,youneed:
¹¹³
http://stackoverflow.com/questions/24185066/itextsharp-read-from-specific-position
¹¹⁴
http://stackoverflow.com/users/1539189/robert-j
Documents you may be interested
Documents you may be interested