I am using ITextSharp and PowerShell to create a PDF document.
I want to be able to load an existing template PDF file which ideally has placeholders and then replace the placeholders with values I supply.
Then I want to save the document with the changes as a new PDF.
Is this possible?
Right now here is the code I have for creating a PDF
[System.Reflection.Assembly]::LoadFrom("c:\\itextsharp.dll")
[void][iTextSharp.text.pdf.PdfWriter]::GetInstance($Doc, [System.IO.File]::Create("c:\existing.pdf") )
# Need to edit $Doc (replace values, add elements) then save as new file
$Doc.Close()
Any help is appreciated.
Thanks,
Andrew
You'll use the AcroFields.SetFields method to specify the values you want in each of the fields in your fillable PDF form:
[System.Reflection.Assembly]::LoadFrom($iTextSharpLibFullname)
$reader = New-Object iTextSharp.text.pdf.PdfReader($templateFileFullname)
$stamper = New-Object iTextSharp.text.pdf.PdfStamper($reader,
[System.IO.File]::Create($outputFileFullname))
$stamper.AcroFields.SetField('Field1_Name', 'Field1_Value')
$stamper.AcroFields.SetField('Field2_Name', 'Field2_Value')
#etc. for each field in your form...
$stamper.Close()
Where:
$iTextSharpLibFullname is a reference to iTextSharp.dll
$templateFileFullname is the name of your fillable PDF template form
$outputFileFullname is the name of the PDF you'll create
Related
I have a formular in my JSP, that has an input field for a PDF-file and besides that also other inputs for text and numbers. This is my line for the data upload of the PDF-File and that is how my form starts:
Upload for PDF: <input type="file" name="datei" />
When in my Servlet, I just get the name of the PDF-File, when i request it like this:
String pdf = request.getParameter("datei");
Now how do I upload a pdf-File, submit that to my Servlet, so I can get all the data and Update a row with all this data in my PostGreSQL database?
First the problem is in the form that must have the enctype="multipart/form-data", but so I can't bring over inputs from the type="text". And another form in another one is not possible either. If I leave out the enctype and just pass the to my servlet, I don't know more than to request this parameter e.g. with String pdf = request.getParameter("file");, but there I only get the PDF file name. Additionally I need to store the PDF file in the database. For this I will create another column pdf of type oid, but I don't know how to pass this PDF file through all methods, so that I can finally save it in PostGreSQL and assign it to a seminar.
Thank you
I would like to know, how can i reuse one template (with one page inside and some variables) multiple times a XWPFDocument object.
My idea is:
load the template once in a XWPFDocument as an template-object
clone/create/copy the template-object with all his styles and headers etc
fill the clone with content
add this clone to the destination-XWPFDocument
I got this work for one single page only.
When i try to clone/create/copy the template-object it will lose all his style informations.
How to copy a paragraph of .docx to another .docx withJava and retain the style
How to copy some content in one .docx to another .docx , using POI without losing format?
POI probably does not support this out of the box, but I have done a similar thing in my project poi-mail-merge, it works with the underlying XML to repeatedly replace markers in a template Microsoft Word document and combine the results into one resulting document.
So it basically duplicates the template document multiple times into the resulting document.
See here for how I do it there, basically I work on the XML body text and do replacements/changes there and then append it onto the result document.
POI Mail Merge propably helps in other cases but in my case it doesn't work.
My Workaround is to update my Template-XWPFDocument to the needed structure first, save it temporarily and read it back into a XWPFDocument-object.
Here the steps:
Read the template-file into a XWPFDocument
Read the records from data-file e.g. csv
Calculate the numbers of pages related to the data-records
Get the Bodyelements-Objects from the Template-XWPFDocument
Create new Bodyelements (depending to the numbers of pages) in the Template-XWPFDocument and replace them with the same Objects that we get before
Save the updated Template-XWPFDocument temporarily
Read the temporarily saved Template into a XWPFDocument
Replace all placeholder and fill them with your CSV-Data
Hope this helps somebody
I have a template file that I fill using PHPExcel. But I have terms and conditions that are saved in database with html tags and inline css. Now these terms and conditions are subject to change so I cant put it into template. So only solution is t take it from database and put it inside created template but I have no clue how to open xlsx file and insert .html file inside it perhaps as second sheet.
This is my current code:
$objPHPExcel = new PHPExcel();
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($outputFileName);
And of course there is lot of code that specifically deal with writing data to excel file but that is working perfectly.
Could someone please explain how could I go about doing it.
Thanks
You can't simply insert an HTML file inside an xlsx file
The latest develop branch of PHPExcel does include an HTML to Rich Text wizard that will take a block of HTML markup and convert it to a Rich Text object that can then be stored in a cell, and /Examples/42richText.php demonstrates how it can be used. At present, this only covers basic markup tags (<br />, <font>, <b>, <i>, <em>, <strong>, <sub>, <sup>, <ins>, <del>, etc) and doesn't handle inline style in any way. However, it might provide the basis for what you want with some additional work.
I create a PDF document with EVO PDF library from a HTML page using the code below:
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Merge_HTML_with_Existing_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));
Response.BinaryWrite(outPdfBuffer);
Response.End();
This produces a PDF document but I have another PDF document that I would like to use as cover page in the final PDF document.
One possiblity I was thinking about was to create the PDF document and then to merge my cover page PDF with the PDF produced by converter but this looks like an inefficient solution. Saving the PDF and loading back for merge seems to introduce a unnecessary overhead. I would like to merge the cover page while the PDF document produced by converter is still in memory.
The following line added in your code right after you create the HTML to PDF converter object should do the trick:
// Set the PDF file to be inserted before conversion result
htmlToPdfConverter.PdfDocumentOptions.AddStartDocument("CoverPage.pdf");
I'm using Crystal Reports to generate report definition *.rpt files. I later create reports using those definition files from inside my application.
I have a special image called the logo that sits in my application's file directory. I'd like to reference this logo on my crystal report. The logo is always located a couple directories up from my report, but the root directory and the host computer can change between installations, so the file structure could look like this:
image: c:\files\logo.png
report: c:\files\reports\type1\myreport.rpt
or like this:
image: \\filehost\files\logo.png
report: \\filehost\\files\reports\type1\myreport.rpt
Does Crystal have a way to reference this image in a formula with a relative path? Does the .rpt file contain a reference to it's file location that I could build off of to grab this image?
Edit: #campagnolo_1 mentions that Crystal has a "File Path and Name" special field, but I don't see a way to use that in the image file location script.
Edit 2: #campagnolo_1 has provided a solution in the comments on his answer. Thanks campagnolo_1!
Crystal has a special field for the file path and name. You could use that to find out where the report file is located and extract the path if needed. Then you can use that to dynamically reference your image file. Here is a post with some step-by-step instructions.
I always stream my logos in as part of the underlying data that the report is bound against. I add the column to the dataset by doing something like this:
DataTable dt = GetYourDataTable();
dt.Columns.Add(new DataColumn("imagepath", typeof(string)));
dt.Columns.Add(new DataColumn("image", typeof(System.Byte[])));
ds.Tables.Add(dt);
And populate it with:
using (FileStream fs = new FileStream(szFilePath, FileMode.Open))
{
using (BinaryReader br = new BinaryReader(fs))
{
foreach (DataRow dr in _dtReportData.Rows)
{
dr["imagepath"] = szFilePath;
dr["image"] = br.ReadBytes((int)br.BaseStream.Length);
}
}
}
I know it's redundant to set EVERY ROW, but it does work, and the object can just be added to the report as a Blob.
The big advantage for you would be that the image location is arbitrary.. you only need to know it at data-retrieval time, not at report-rendering time.