itextsharp advance pdf generation by configuration xml - asp.net-mvc-4

I am generating PDF using iTextSharp.i got so many sample code in google but how we can configure pdf setting which is required to generate PDF like
var doc = new Document(PageSize.A5);
var doc = new Document(new Rectangle(100f, 300f));
these all setting i wanted to set my config xml file and read xml and generate pdf on fly. is any professional sample example code available?plz help..

Related

Editing the metadata of a PDF file using PDFBox

I am able to edit the metadata for a PDF file using PDFBox3.x. However, at the final step I am being forced to save the changes to a different file.
Is there a way to make changes to the original file itself?
Sample code:
PDDocument doc = Loader.loadPDF(inFileObj);
PDDocumentInformation pdd = doc.getDocumentInformation();
pdd.setAuthor("Mr. Stack Overflow");
// ...
doc.save(outFileObj); // This works, however doc.save(inFileObj) makes the PDF document a blank document

Error converting .docx with HTML to PDF using Graph API

I am trying to convert MS Word (.docx) file to PDF format using Graph API. The file is stored in SharePoint Office 365. I am using below code which works.
var httpClient = await CreateAuthorizedHttpClient();
string path = $"{GraphEndpoint}sites/{SiteId}/drive/items/";
string requestUrl = $"{path}{fileId}/content?format={targetFormat}";
var response = await httpClient.GetAsync(requestUrl);
However, when we try to convert .docx file which contains HTML added using below code converting fails.
string altChunkId = "myId123";
//Create an alternative format import part on the MainDocumentPart
AlternativeFormatImportPart altformatImportPart = wordDoc.MainDocumentPart
.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);
using (MemoryStream htmlMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes($"<html><head></head><body>{value}</body></html>")))
{
//Add the HTML data into the alternative format import part
altformatImportPart.FeedData(htmlMemoryStream);
//create a new altChunk and link it to the id of the AlternativeFormatImportPart
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
//p.InsertAfterSelf(altChunk);
documentBody.Append(altChunk);
break;
}
I get 406 Not Acceptable error when we try to convert the file using Graph API. Also I see that the file is not editable in browser and open in compatibility mode. If I try to open the document in edit mode I get error:
Sorry this document can't be opened because it contains objects that
word doesn't support
I tried removing the HTML part of the document and pasted that in another document and tried converting that document to PDF which worked. When I saw the XML of the document I saw Word App converted that HTML to word compatible XML tags.
Question 1: How can I convert the HTML to word compatible tags? So I can convert the document to PDF.
Also if I try to Download as PDF, the file is converted to PDF without any issue.
This option is using below API call:
https://word-view.officeapps.live.com/wv/WordViewer/request.pdf?WOPIsrc={SiteURL}%2F%5Fvti%5Fbin%2Fwopi%2Eashx%2Ffiles%2F{ID}&access_token=&access_token_ttl=&z=256&type=downloadpdf
Question 2: Is there a way I can use this API to convert .docx file to PDF? I saw the access token's audience value is "wopi/{TenantName}#{TenantID}". If I get the correct access token I think I will be able to use the above API.

iText 7 Chinese characters and merge with existing pdf template

I have to rephrase my question, basically my request is very straight forward, i want to display Asian characters in the generated pdf file from iText7.
As of now i have download the NotoSansCJKsc-Regular.otf file and assign a variable to hold the path, below is my code:
public static string FONT = #"D:\Projects\Resources\NotoSansCJKsc-Regular.otf";
PdfWriter writer = new PdfWriter(#"C:\temp\test.pdf");
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc, PageSize.A4);
PdfFont fontChinese = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
doc.SetFont(fontChinese);
but the issue i am facing now is whenever the code runs to this section:
PdfFont fontChinese = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
i am always getting this error: The request could not be performed because of an I/O device error. and this error doesn't make sense to me and I am struggling to find out the solution, could someone in here had the similar issue plz, the code is in C#.
Many thanks.
I can confirm that above code is working as expected, the .otf file that I was originally downloaded was corrupted, hence I got above error.

Synfusion : Image from url not showing in html to pdf conversion

I'm trying to generate pdf from html which is working fine but the issue I am having is that my images are not showing: the images are from a url <img src="https://image.shutterstock.com/image-photo/large-beautiful-drops-transparent-rain-260nw-668593321.jpg" /> I think the images have not been loaded before the conversion. Please how can I delay the conversion or achieve this i.e ensure the image is rendered with the pdf.
WebKitConverterSettings settings = new WebKitConverterSettings();
var baseUrl = url;
settings.PdfHeader = HeaderHTMLtoPDF(url");
settings.PdfFooter = FooterHTMLtoPDF({url});
//Set WebKit path
var contentRoot = _configuration.GetValue<string>(WebHostDefaults.ContentRootKey);
settings.WebKitPath = Path.Combine(contentRoot, "QtBinariesWindows");
settings.Margin.Top = 30;
//Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = settings;
var pdfViewUrl = $"{baseUrl}/api/pdf";
Task<PdfDocument> convertPdfTask = Task<PdfDocument>.Factory.StartNew(() => htmlConverter.Convert(pdfViewUrl));
PdfDocument document = convertPdfTask.Result;
MemoryStream stream = new MemoryStream();
document.Save(stream);
document.Close(true);```
We have checked the conversion with provided details, its working properly. We have creates a simple html with provided image and it is properly converted to PDF document.
The reported image missing issue may occurs due to missing of OPENSSL assemblies. To access the resource from HTTPS site, the HTML converter requires OPENSSL assemblies. So, please make sure the OPENSSL assemblies are available in the machine where the conversion takes place. Please refer below links for more details,
Prerequisites - https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/webkit#openssl
Troubleshooting - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#troubleshooting
However, the below mentioned OPENSSL assemblies can be placed in the Windows system folder of the machine. (for 64-bit machine, it should be place in $SystemDrive\Windows\SysWOW64 and for 32-bit machine, it should be place in $SystemDrive\Windows\System32).
libeay32.dll
libssl32.dll
ssleay32.dll
Note : I work for Syncfusion.
An alternative solution is to convert your image to base64 so the image is inline in the html.

Relative file links in pdf files

I'm creating a single pdf file that I'd like to link to other files in the same directory as the pdf.
ie.
MyFolder
|
|-main.pdf
|-myotherpdf.pdf
|-myotherotherpdf.pdf
I'd like the main.pdf to have links that would cause the default program on the pdf to open the other pdfs.
As I am generating these file on a server and then providing them in a download to the client I cannot use absolute links as these would not exist on the client pc.
So firstly do pdf files actually support relative file links like this, I haven't found much that says they do either way.
Additionally to generate my pdf I'm using abcpdf and providing it html to convert to pdf.
To try and generate the correct out the correct urls in html I have tried the following
<a href='test.pdf'>test pdf link to local file</a>
<a href='#test.pdf'>test pdf link to local file</a>
<a href='/test.pdf'>test pdf link to local file</a>
<a href='file:///test.pdf'>test pdf link to local file</a>
<a href='file://test.pdf'>test pdf link to local file</a>
Most of them either direct to me a point where the pdf document was generated from (temporary file path) or they link hovering shows "file:///test.pdf" in acrobat but clicking it causes a warning dialog to popup asking to allow/deny, upon clicking allow it opens up in firefox with the url "file:///test.pdf" which wouldn't resolve to anything.
Any ideas on how to get this working or if this kind of linking is even possible in pdfs?
I can only answer your question: does PDF files actually support relative file links like this?
Yes, it does. I created a little test with a main.pdf that has two links to two other PDF documents in the same folder. I created the links manually with Acrobat and associated a launch action with the link annotation. See the internal structure here:
Here is the zip with the main plus two secondary PDFs. Note that you can copy them anywhere and the relative links remain valid.
https://www.dropbox.com/s/021tvynkuvr63lv/main.zip
I am not sure how you would accomplish this with abcpdf, especially since you are converting from HTML which probably limits the PDF features available.
So I got it working in the end thanks to #Frank Rem and some help from the abcpdf guys
Code is as follows
foreach (var page in Enumerable.Range(0, doc.PageCount))
{
doc.PageNumber = page;
var annotEnd = doc.GetInfoInt(doc.Page, "Annot Count");
for (var i = 0; i <= annotEnd; ++i)
{
var annotId = doc.GetInfoInt(doc.Page, "Annot " + (i + 1));
if (annotId > 0)
{
var linkText = doc.GetInfo(annotId, "/A*/URI*:Text");
if (!string.IsNullOrWhiteSpace(linkText))
{
var annotationUri = new Uri(linkText);
if (annotationUri.IsFile)
{
// Note abcpdf temp path can be changed in registry so if this changes
// will need to rewrite this to look at the registry
// http://www.websupergoo.com/helppdfnet/source/3-concepts/d-registrykeys.htm
var abcPdfTempPath = Path.GetTempPath() + #"AbcPdf\";
var relativePath = annotationUri.LocalPath.ToLower().Replace(abcPdfTempPath.ToLower(), string.Empty);
// Only consider files that are not directly in the temp path to be valid files
// This is because abcpdf will render the document as html to the temp path
// with a temporary file called something like {GUID}.html
// so it would be difficult to tell which files are the document
// and which are actual file links when trying to do the processing afterwards
// if this becomes and issue this could be swapped out and do a regex on {GUID}.html
// then the only restriction would be that referenced documents cannot be {GUID}.html
if (relativePath.Contains("\\"))
{
doc.SetInfo(annotId, "/A*/S:Name", "Launch");
doc.SetInfo(annotId, "/A*/URI:Del", "");
doc.SetInfo(annotId, "/A*/F:Text", relativePath);
doc.SetInfo(annotId, "/A*/NewWindow:Bool", "true");
}
}
}
}
}
}
This will allow each link to be opened in the viewer that is associated with it on the pc.