I have a problem with printing data and image in PDF. I use the code below and it print image and data separately. But I want them to be in one PDF, how can I do this?
PrintDialog export = new PrintDialog();
string data = "data";
System.Windows.Documents.FlowDocument flowDoc = new System.Windows.Documents.FlowDocument(new System.Windows.Documents.Paragraph(new System.Windows.Documents.Run(data)));
System.Windows.Documents.IDocumentPaginatorSource source = flowDoc;
var result = export.ShowDialog();
if(result == true)
{
export.PrintVisual(souradnySystem, "Visual");
export.PrintDocument(source.DocumentPaginator, "Text");
}
Add the image to the FlowDocument before proceeding to print.
Assuming "souradnySystem" is your image (System.Windows.Controls.Image), it will be like the following:
string data = "data";
FlowDocument flowDoc = new FlowDocument(new Paragraph(new Run(data)));
flowDoc.Blocks.Add(new BlockUIContainer(souradnySystem));
Related
I want to insert image to an XLSX file (not xls) using NPOI.
I am using XSSFWorkbook and XSSFSheet
byte[] data = File.ReadAllBytes("SomeImage.jpg");
int picInd = workbook.AddPicture(data, XSSFWorkbook.PICTURE_TYPE_JPEG);
XSSFCreationHelper helper = workbook.GetCreationHelper() as XSSFCreationHelper;
XSSFDrawing drawing = _sheet.CreateDrawingPatriarch() as XSSFDrawing;
XSSFClientAnchor anchor = helper.CreateClientAnchor() as XSSFClientAnchor;
anchor.Col1 = 1;
anchor.Row1 = 1;
XSSFPicture pict = drawing.CreatePicture(anchor, picInd) as XSSFPicture;
The file is saved successfully. but while opening it showing the following error and on clicking yes, it does not display the image.
I got the solution:
byte[] data = File.ReadAllBytes("someImage.png");
int pictureIndex = workbook.AddPicture(data, PictureType.PNG);
ICreationHelper helper = workbook.GetCreationHelper();
IDrawing drawing = _sheet.CreateDrawingPatriarch();
IClientAnchor anchor = helper.CreateClientAnchor();
anchor.Col1 = 0;//0 index based column
anchor.Row1 = 0;//0 index based row
IPicture picture = drawing.CreatePicture(anchor, pictureIndex);
picture.Resize();
I have a problem about the foreach instruction and a description for each image.
Right now, when the user selects an image from the list, the next message occurs (see image1)
What I want is to change this text with a different one for each image.
I've also created a class called WineModel and its code is posted below.
What should I do to display a different description for each image?
One of my ideas would be to create a vector like in this link and to display a different message for each image from the position of the vector.
var files = Directory.GetFiles(#".\GalleryImages");
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
WineModel wineModel = new WineModel();
wineModel.Image = new Uri(file, UriKind.Relative);
wineModel.Description = file + "Ana are mere" +
Environment.NewLine + "text text text text text text text text text text text";
wineModel.Price = new Random().NextDouble();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = wineModel.Image;
bi.EndInit();
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi),
Tag = wineModel
};
this.wrapPanel.Children.Add(button);
}
http://i58.tinypic.com/nbvhu8.png
http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx
I am working to replace the specific word inside pdf document using iTextSharp and C#.net, while I am debugging getting the proper value but the outputted pdf getting zero bytes(empty),its not filling with the content.
ReplacePDFText("Mumbai",StringComparison.CurrentCultureIgnoreCase,Application.StartupPath + "\\test.pdf","D:\\test_words_replaced.pdf"); //Do Everything
public void ReplacePDFText(string strSearch, StringComparison scCase, string strSource, string strDest)
{
PdfStamper psStamp = null; //PDF Stamper Object
PdfContentByte pcbContent = null; //Read PDF Content
if (File.Exists(strSource)) //Check If File Exists
{
PdfReader pdfFileReader = new PdfReader(strSource); //Read Our File
psStamp = new PdfStamper(pdfFileReader, new FileStream(strDest, FileMode.Create)); //Read Underlying Content of PDF File
pbProgress.Value = 0; //Set Progressbar Minimum Value
pbProgress.Maximum = pdfFileReader.NumberOfPages; //Set Progressbar Maximum Value
for (int intCurrPage = 1; intCurrPage <= pdfFileReader.NumberOfPages; intCurrPage++) //Loop Through All Pages
{
LocTextExtractionStrategy lteStrategy = new LocTextExtractionStrategy(); //Read PDF File Content Blocks
pcbContent = psStamp.GetUnderContent(intCurrPage); //Look At Current Block
//Determine Spacing of Block To See If It Matches Our Search String
lteStrategy.UndercontentCharacterSpacing = pcbContent.CharacterSpacing;
lteStrategy.UndercontentHorizontalScaling = pcbContent.HorizontalScaling;
//Trigger The Block Reading Process
string currentText = PdfTextExtractor.GetTextFromPage(pdfFileReader, intCurrPage, lteStrategy);
//Determine Match(es)
List<iTextSharp.text.Rectangle> lstMatches = lteStrategy.GetTextLocations(strSearch, scCase);
PdfLayer pdLayer = default(PdfLayer); //Create New Layer
pdLayer = new PdfLayer("Overrite", psStamp.Writer); //Enable Overwriting Capabilities
//Set Fill Colour Of Replacing Layer
pcbContent.SetColorFill(BaseColor.BLACK);
foreach (iTextSharp.text.Rectangle rctRect in lstMatches) //Loop Through Each Match
{
pcbContent.Rectangle(rctRect.Left, rctRect.Bottom, rctRect.Width, rctRect.Height); //Create New Rectangle For Replacing Layer
pcbContent.Fill(); //Fill With Colour Specified
pcbContent.BeginLayer(pdLayer); //Create Layer
pcbContent.SetColorFill(BaseColor.BLACK); //Fill aLyer
pcbContent.Fill(); //Fill Underlying Content
PdfGState pgState = default(PdfGState); //Create GState Object
pgState = new PdfGState();
pcbContent.SetGState(pgState); //Set Current State
pcbContent.SetColorFill(BaseColor.WHITE); //Fill Letters
pcbContent.BeginText(); //Start Text Replace Procedure
pcbContent.SetTextMatrix(rctRect.Left, rctRect.Bottom); //Get Text Location
//Set New Font And Size
pcbContent.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 9);
pcbContent.ShowText("AMAZING!!!!"); //Replacing Text
pcbContent.EndText(); //Stop Text Replace Procedure
pcbContent.EndLayer(); //Stop Layer replace rocedure
}
pbProgress.Value++; //Increase Progressbar Value
pdfFileReader.Close(); //Close File
}
//psStamp.Close(); //Close Stamp Object
}
}
You call
pdfFileReader.Close();
much too early: inside a loop in which the next iteration still requires pdfFileReader and furthermore before closing the stamper.
The stamper requires the PdfReader to still be open when the stamper closes because it copies certain parts of the reader only then.
I'm in the process of trying to print out a FlowDocument that is being viewed by the user. I wrote a routine to create a copy of the original document so that changes (from the PrintDialog) don't get reflected into the DocumentViewer.
Unfortunately, my copy seems to have lost all of the information bound to its fields. I've tried resetting the DataContext but the copy's IsLoaded property is still coming back false, leading me to believe that the binding isn't occuring.
Any ideas?
Here's the code I'm using to copy the document:
private static void AddDocument(FlowDocument from, FlowDocument to)
{
TextRange tr = new TextRange(from.ContentStart, from.ContentEnd);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
TextRange tr2 = null;
System.Windows.Markup.XamlWriter.Save(tr, ms);
tr.Save(ms, DataFormats.XamlPackage, true);
tr2 = new TextRange(to.ContentEnd, to.ContentEnd);
tr2.Load(ms, DataFormats.XamlPackage);
}
}
And here's the code I'm using to print the document:
public static void PrintFlowDocument(FlowDocument fd, string title)
{
PrintDialog pd = new PrintDialog();
IDocumentPaginatorSource idps = null;
FlowDocument flowDoc = new FlowDocument();
AddDocument(fd, flowDoc);
flowDoc.DataContext = fd.DataContext;
flowDoc.PageHeight = pd.PrintableAreaHeight;
flowDoc.PageWidth = pd.PrintableAreaWidth;
flowDoc.PagePadding = new Thickness(50);
flowDoc.ColumnGap = 0;
flowDoc.ColumnWidth = pd.PrintableAreaWidth;
idps = flowDoc;
if (pd.ShowDialog() == true)
{
pd.PrintDocument(idps.DocumentPaginator, title);
}
}
Thanks in advance,
Sonny
Notice some off about this line?
tr2 = new TextRange(to.ContentEnd, to.ContentEnd);
I had similar issues and found the forcing the document creation to a background thread gave the binding an opportunity to fire. Otherwise, it would not happen on the UI thread.
So, if your copy document method were a function it would be something like this:
Dim flowDoc As FlowDocument =
DirectCast(<ViewInstance>.UIDispatcher.Invoke(Function()
AddFlowDocument(fd),
Windows.Threading.DispatcherPriority.Background),
FlowDocument)
i am trying to to generate a XPS Document from a WPF Control. Printing works so far, but i cannot find a way to create the XPS in landscape mode.
My code to create the XPS file, mostly taken from another SO page
public FixedDocument ReturnFixedDoc()
{
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
var ctrl = new controlToPrint();
//Create first page of document
fixedPage.Children.Add(ctrl);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
//Create any other required pages here
return fixedDoc;
}
public void SaveCurrentDocument()
{
// Configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "MyReport"; // Default file name
dlg.DefaultExt = ".xps"; // Default file extension
dlg.Filter = "XPS Documents (.xps)|*.xps"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
FixedDocument doc = ReturnFixedDoc();
XpsDocument xpsd = new XpsDocument(filename, FileAccess.Write);
System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(doc);
xpsd.Close();
}
}
Any help is appreciated.
Try setting the size of your FixedPage in ReturnFixedDoc:
// hard coded for A4
fixedPage.Width = 11.69 * 96;
fixedPage.Height = 8.27 * 96;
The numbers are in the form (inches) x (dots per inch). 96 is the DPI of WPF. I have used the dimensions of an A4 page.