How to add custom property/metadata to PDF 2.0 file - c#

I am trying to add custom property/metadata to PDF 2.0 file (like this customprop). I have used PDFSharp, ITextSharp and PDFBox but I couldn't. Is there any free package that can fix the problem.
link for sample pdf 2.0

Have you tried SpirePDF? There is a free Nuget that you can try, it worked fine for me with that snippet:
using Spire.Pdf;
namespace App
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(#"C:\Users\User\Downloads\output.pdf");
doc.DocumentInformation.SetCustomProperty("Name", "Test");
doc.DocumentInformation.SetCustomProperty("Company", "StackOverflow");
doc.SaveToFile(#"C:\Users\User\Downloads\result.pdf");
}
}
}

You can add custom metadata using Docotic.Pdf library like that:
// NOTE:
// When used in trial mode, the library imposes some restrictions.
// Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx for more information.
BitMiracle.Docotic.LicenseManager.AddLicenseData("temporary or permanent license key here");
using (var pdf = new PdfDocument("Simple_pdf_2.0_file.pdf"))
{
pdf.Metadata.Custom.Properties.Add("CustomKey", "CustomValue");
pdf.Save("SetCustomXmpProperties.pdf");
}
Here is the more comprehensive sample: https://github.com/BitMiracle/Docotic.Pdf.Samples/tree/master/Samples/Metadata/SetCustomXmpProperties
Disclaimer: I am the co-author of Docotic.Pdf library.

Related

how to print a pdf file without using adobe reader in C#?

I have a pdf in ticket format of an 80mm invoice. How can I print without using adobe reader.
Is there a library that can help me work?
I tried various tools but none gave me a solution, even using adobe reader there are times when it does not send to print and it is uncomfortable for the client.
You may try Spire.PDF nuget package which supports multiple printing scenarios.
The following code shows how to print a PDF using the default printer:
using Spire.Pdf;
namespace PrintWithDefaultPrinter
{
class Program
{
static void Main(string[] args)
{
//Create PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.LoadFromFile(#"C:\Users\Administrator\Desktop\sample.pdf");
//Print with default printer
doc.Print();
}
}
}
More printing options can be found in this documentation: C#/VB.NET: Print PDF Documents.

How to convert HTML content to PDF in Xamarin forms

I want to convert HTML content to PDF. I have tried using third party API like GemBox, GrapeCity..
these all are working in UWP but in android failing.
Please suggest any solution. I can go with paid API's also.
Thanks!
If you are looking for a one-in-all solution, you can check out the leadtools document converter nuget here: https://www.nuget.org/packages/Leadtools.Document.Sdk/ disclaimer: i am employed by the vendor of this library
LEADTOOLS supports HTML -> PDF as well as a bunch of other formats. You can see more information here: https://www.leadtools.com/sdk/document/document-converter
This library supports Xamarin on Android, UWP, and iOS and is completely offline.
Here is a snippet of code that will work for your use-case:
using (var documentConverter = new DocumentConverter())
{
var htmlString = File.ReadAllText(#"./test.htm");
using (var ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlString)))
{
var htmlDocument = DocumentFactory.LoadFromStream(ms, new LoadDocumentOptions());
var jobData = DocumentConverterJobs.CreateJobData(htmlDocument, "output.pdf", DocumentFormat.Pdf);
var job = documentConverter.Jobs.CreateJob(jobData);
documentConverter.Jobs.RunJob(job);
}
}

Using C# iText 7 to flatten an XFA PDF

Is it possible to use iText 7 to flatten an XFA PDF? I'm only seeing Java documentation about it (http://developers.itextpdf.com/content/itext-7-examples/itext-7-form-examples/flatten-xfa-using-pdfxfa).
It seems like you can use iTextSharp, however to do this.
I believe it's not an AcroForm PDF because doing something similar to this answer How to flatten pdf with Itext in c#? simply created a PDF that wouldn't open properly.
It looks like you have to use iTextSharp and not iText7. Looking at the NuGet version it looks like iTextSharp is essentially the iText5 .NET version and like Bruno mentioned in the comments above, the XFA stuff simply hasn't been ported to iText7 for .NET.
The confusion stemmed from having both iText7 and iTextSharp versions in NuGet and also the trial page didn't state that the XFA worker wasn't available for the .NET version of iText7 (yet?)
I did the following to accomplish what I needed at least for a trial:
Request trial copy here: http://demo.itextsupport.com/newslicense/
You'll be emailed an xml license key, you can just place it on your desktop for now.
Create a new console application in Visual Studio
Open the Project Manager Console and type in the following and press ENTER (this will install other dependencies as well)
Install-Package itextsharp.xfaworker
Use the following code:
static void Main(string[] args)
{
ValidateLicense();
var sourcePdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "<your_xfa_pdf_file>");
var destinationPdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "output.pdf");
FlattenPDF(sourcePdfPath, destinationPdfPath);
}
private static void ValidateLicense()
{
var licenseFileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "itextkey.xml");
iTextSharp.license.LicenseKey.LoadLicenseFile(licenseFileLocation);
}
private static void FlattenPDF(string sourcePdfPath, string destinationPdfPath)
{
using (var sourcePdfStream = File.OpenRead(sourcePdfPath))
{
var document = new iTextSharp.text.Document();
var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(destinationPdfPath, FileMode.Create));
var xfaf = new iTextSharp.tool.xml.xtra.xfa.XFAFlattener(document, writer);
sourcePdfStream.Position = 0;
xfaf.Flatten(new iTextSharp.text.pdf.PdfReader(sourcePdfStream));
document.Close();
}
}
The trial will put a huge watermark on the resulting PDF, but at least you can get it working and see how the full license should work.
For IText 7 this could be done in the following way
LicenseKey.LoadLicenseFile(#"Path of the license file");
MemoryStream dest_File = new MemoryStream();
XFAFlattener xfaFlattener = new XFAFlattener();
xfaFlattener.Flatten(new MemoryStream( File.ReadAllBytes(#"C:\\Unflattened file")), dest_File);
File.WriteAllBytes("flatten.pdf", dest_File.ToArray());

Reading doc and docx files using C# without having MS Office installed on server

I'm working on a project (asp.net, c#, vb 2010, .net 4) and I need to read both DOC and DOCX files, that I've previosly uploaded (I've done uploading part). Tricky part is that I don't have MS Office installed on server and that I can't use it.
Is there any public library that I can include into my project without having to install anything?
Both docs are very simple:
NUMBER TAB STRING
NUMBER TAB STRING
NUMBER TAB STRING
...
I need to extract number and string for each row (paragraph).
May someone help with this? I should repeat once again that I'm limited in a way that I can't install anything on a server.
We can now use open source, NPOI (.NET port of Apache POI) library which also supports docx, xls & xlsx.
DocX is also another open source library for creating word docs.
For DOCX I'd suggest Open XML API, though Microsoft developed Open XML to create office files through the XML files communicating with this API, the latest version 2.5 was released in 2013 which is 5 years ago.
you can use Code7248.word_reader.dll
below is the sample code on how to use Code7248.word_reader.dll
add reference to this DLL in your project and copy below code.
using System;
using System.Collections.Generic;
using System.Text;
//add extra namespaces
using Code7248.word_reader;
namespace testWordRead
{
class Program
{
private void readFileContent(string path)
{
TextExtractor extractor = new TextExtractor(path);
string text = extractor.ExtractText();
Console.WriteLine(text);
}
static void Main(string[] args)
{
Program cs = new Program();
string path = "D:\Test\testdoc1.docx";
cs.readFileContent(path);
Console.ReadLine();
}
}
}
Update: NPOI supports docx now. Please try the latest release (NPOI 2.0 beta)
You can do like this:
using System.IO;
using System.Text;
using Spire.Doc;
namespace ReadTextLineByLine{
class Program {
static void Main(string[] args) {
//Create a Document object
Document doc = new Document();
//Load a Word file
doc.LoadFromFile(#"C:\Users\Administrator\Desktop\data.docx");
//Convert the text in Word line by line into a txt file
doc.SaveToTxt("result.text", Encoding.UTF8);
//Read all lines of txt file
string[] lines = File.ReadAllLines("result.text", System.Text.Encoding.Default);
}
}
}

how to programmatically (c#) find and replace in doc and docx file in share point library

My job is to do a find and replace in .doc and .docx files which are saved in sharepoint document library. i have to alter all the documents in the document library by doing find and replace. please help me in this...
Open XML formats are only for word 2007 (.docx).
I need single solution that would do find and replace in both .doc and .docx files.
The library also contains .ppt,.pptx and excel sheets also..
I would recommend you to take a look at a third party tool from Aspose:
http://www.aspose.com/categories/.net-components/aspose.words-for-.net/default.aspx
This supports both .doc and .docx, and they also have a similar product for PowerPoint. My experience with Aspose is that they are of high quality and easy to implement.
The best route to try is to look at "SharePoint Word Automation Services"
http://msdn.microsoft.com/en-us/library/ff742315.aspx
Below is an example from that link that should give you a good idea how to do this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Word.Server.Conversions;
class Program
{
static void Main(string[] args)
{
string siteUrl = "http://localhost";
// If you manually installed Word automation services, then replace the name
// in the following line with the name that you assigned to the service when
// you installed it.
string wordAutomationServiceName = "Word Automation Services";
using (SPSite spSite = new SPSite(siteUrl))
{
ConversionJob job = new ConversionJob(wordAutomationServiceName);
job.UserToken = spSite.UserToken;
job.Settings.UpdateFields = true;
job.Settings.OutputFormat = SaveFormat.PDF;
job.AddFile(siteUrl + "/Shared%20Documents/Test.docx",
siteUrl + "/Shared%20Documents/Test.pdf");
job.Start();
}
}
}

Categories

Resources