Getting a mysterious null ref error using itext7. Everything was working fine before. The PDF I'm reading looks normal.
var sr1 = new MemoryStream(data);
rdr = new PdfReader(sr1);
// https://stackoverflow.com/questions/17691013/pdfreader-not-opened-with-owner-password-error-in-itext
rdr.SetUnethicalReading(true);
pdf = new PdfDocument(rdr);
var pages = pdf.GetNumberOfPages();
merger.Merge(pdf, 1, pages);
The only recent change was to add that call to SetUnethicalReading, which fixed one issue.
The PDF is fine, data has plenty of bytes. 'merger' is not null, nor is 'pdf'.
System.NullReferenceException: Object reference not set to an instance of an object.
at iText.Kernel.Pdf.PdfOutline..ctor(String title, PdfDictionary content, PdfOutline parent)
at iText.Kernel.Pdf.PdfCatalog.ConstructOutlines(PdfDictionary outlineRoot, IDictionary`2 names)
at iText.Kernel.Pdf.PdfCatalog.GetOutlines(Boolean updateOutlines)
at iText.Kernel.Pdf.PdfDocument.GetOutlines(Boolean updateOutlines)
at iText.Kernel.Pdf.PdfPage.GetOutlines(Boolean updateOutlines)
at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(IList`1 pagesToCopy, PdfDocument toDocument, Int32 insertBeforePage, IPdfPageExtraCopier copier)
at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(IList`1 pagesToCopy, PdfDocument toDocument, IPdfPageExtraCopier copier)
at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(IList`1 pagesToCopy, PdfDocument toDocument)
at iText.Kernel.Utils.PdfMerger.Merge(PdfDocument from, IList`1 pages)
at iText.Kernel.Utils.PdfMerger.Merge(PdfDocument from, Int32 fromPage, Int32 toPage)
Related
I'm trying to create a pdf using Itext7.
The code in localhost works properly but trying it in the cloud server I retrieve the following error that I don't understand
The type initializer for 'iText.Commons.Actions.EventManager' threw an exception. at iText.Commons.Actions.EventManager.GetInstance()
at iText.Kernel.Pdf.PdfDocument.Open(PdfVersion newPdfVersion)
at iText.Kernel.Pdf.PdfDocument..ctor(PdfWriter writer, DocumentProperties properties)
at iText.Html2pdf.HtmlConverter.ConvertToPdf(String html, PdfWriter pdfWriter, ConverterProperties converterProperties)
at XXXX.Models.XXXXController.PDFReport(String baseuri, String html, String destination)
Can someone give me some advices?
Below the C# code:
ConverterProperties prop = new ConverterProperties();
prop.SetBaseUri(baseuri);
using (var stream = new FileStream(destination, FileMode.Create))
{
HtmlConverter.ConvertToPdf(html, stream, prop);
}
baseuri: is the base web site http address
destination: is the physical path with the pdf name
I'm using Fonet to render XSL to PDF.
In my .xslt I have:
<xsl:value-of select="Text" disable-output-escaping="yes"/>
In my C# I have the method to crate the PDF memory stream from the XML memory stream:
private static MemoryStream Xml2Pdf(MemoryStream msTransform)
{
FonetDriver d = FonetDriver.Make();
msTransform.Position = 0;
MemoryStream msOut = new MemoryStream();
d.Render(msTransform, msOut);
return msOut;
}
The value of Text is:
<h2><strong><span style="color:#0e8a16;">Text 1</span></strong></h2><h2 style="text-align:center"><em><span style="color:#fef2c0;"><span style="background-color:#0052cc;">Text 2</span></span></em></h2><h4 style="text-align:right"><u>Text 3</u></h4>
d.Render throws System.SystemException: Unknown formatting object ^h2
If I use a Text without HTML everything works fine.
And I don't have any clue how to fix it
I'm using IText 7 to convert my HTML file into PDF and auto download the file when users click a button.
Currently I'm planning to insert a barcode using IText7 into the PDF file, however I encountered some error. Btw, it works fine without the 'barcode code'.
This is the error : 'iText.Kernel.PdfException: 'Pdf indirect object belongs to other PDF document. Copy object to current pdf document.''
How can I add a barcode at the end of my pdf file?
public MemoryStream GetCovidFormPdfByAccessionNumber(string htmlFile, string accessionNumber)
{
var workStream = new MemoryStream();
using (var pdfWriter = new PdfWriter(workStream))
{
pdfWriter.SetCloseStream(false);
var pdfDoc = new PdfDocument(pdfWriter);
using (var document = HtmlConverter.ConvertToDocument(htmlFile, pdfWriter))
{
document.Add(CreateBarcode(accessionNumber, pdfDoc));
}
}
workStream.Position = 0;
return workStream;
}
private static Image CreateBarcode(string code, PdfDocument pdfDoc)
{
Barcode39 barcode = new Barcode39(pdfDoc);
barcode.SetCode(code);
//Create barcode object to put it to the cell as image
PdfFormXObject barcodeObject = barcode.CreateFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDoc);
var image = new Image(barcodeObject);
image.SetWidth(250);
return image;
}
I am new to C#. I am attempting to simply use Microsoft.Office.Interop.Word
All is working except that the Merge Fields in the footer are not updating. I continue to get the mergecode text only («pname»).
Here is the important part of my code
private void getDoc()
{
String cdir = Directory.GetCurrentDirectory();
btnGetPoa.Visible = false;
var application = new Word.Application();
Object dir = #"../../templates/";
Directory.SetCurrentDirectory(dir.ToString());
var doc = new Word.Document();
var dirf = Directory.GetCurrentDirectory() + "\\poas.docx";
doc = application.Documents.Add(Template: dirf);
foreach (Word.Field fld in doc.Fields)
{
if(fld.Code.Text.Contains("pname"))
{
fld.Select();
application.Selection.TypeText(txtpName.Text.ToString());
}
}
object what = Word.WdGoToItem.wdGoToPage;
object which = Word.WdGoToDirection.wdGoToFirst;
object count = 3;
object missing = Missing.Value;
application.Selection.GoTo(ref what, ref which, ref count, ref missing);
application.Visible = true;
}
Any ideas why the Fields in the Footer are not merging?
Thank you.
The Document object addresses only the main document "story". So querying fields in that object doesn't "see" any fields in headers, footers, Shapes, etc. You need to specifically address these other objects. For a general method on how to address all StoryRange objects, look at the VBA Language Reference example for StoryRange and related. You'll also find discussions and code on this site.
For your specific request, I'm assuming that the document has only the one section and the standard footer - no first page footer or even page footer. (A Word document can have multiple sections and different types of headers and footers, but a "plain vanilla" document has only one of each.)
Word.Range rngFooter = doc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
foreach (Word.Field fld in rngFooter.Fields)
{
}
I am trying to view my document on a web page using OpenXml. I have searched and found the code to convert document to html and then display that html page to web page. I used following code.
string path = #"C:\Users\spadmin\Desktop\New folder\HelloHTML.docx";
string hmtlFilePath = #"C:\Users\spadmin\Desktop\New folder\HelloHTML.html";
byte[] byteArray = File.ReadAllBytes(path);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "BIA Sample",
};
OpenXmlPart main = doc.MainDocumentPart;
XElement documentElement = main.GetXDocument().Root;
XElement html = HtmlConverter.ConvertToHtml(doc, settings); // here I got error
File.WriteAllText(hmtlFilePath, html.ToStringNewLineOnAttributes());
}
}
But this throws an error of Object Reference Not set to an instance of an Object.
Then I added the OpenXmlPowerTool Project to my main project and then debug the code and I found that, When I pass the doc to the ConvertToHtml() method the properties of the doc where not there. Here are the snap shots of them.
Before passing I check the doc properties:
When the ConvertToHtml() invoke i check the doc property which was as bellow:
The properties of the documents were not there in the passed object. That is why it throws error.
Can anyone help me that what am I doing wrong?
Is there anything wrong in my code?